tls.c 475 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496
  1. /* tls.c
  2. *
  3. * Copyright (C) 2006-2023 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. #include <wolfssl/wolfcrypt/kyber.h>
  46. #ifdef WOLFSSL_WC_KYBER
  47. #include <wolfssl/wolfcrypt/wc_kyber.h>
  48. #elif defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  49. #include <wolfssl/wolfcrypt/ext_kyber.h>
  50. #endif
  51. #endif
  52. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  53. #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
  54. #endif
  55. #include <wolfssl/wolfcrypt/hpke.h>
  56. #ifndef NO_TLS
  57. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  58. static int TLSX_KeyShare_IsSupported(int namedGroup);
  59. static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap);
  60. #endif
  61. #ifdef HAVE_SUPPORTED_CURVES
  62. static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions);
  63. #endif
  64. /* Digest enable checks */
  65. #ifdef NO_OLD_TLS /* TLS 1.2 only */
  66. #if defined(NO_SHA256) && !defined(WOLFSSL_SHA384) && \
  67. !defined(WOLFSSL_SHA512)
  68. #error Must have SHA256, SHA384 or SHA512 enabled for TLS 1.2
  69. #endif
  70. #else /* TLS 1.1 or older */
  71. #if defined(NO_MD5) && defined(NO_SHA)
  72. #error Must have SHA1 and MD5 enabled for old TLS
  73. #endif
  74. #endif
  75. #ifdef WOLFSSL_TLS13
  76. #if !defined(NO_DH) && \
  77. !defined(HAVE_FFDHE_2048) && !defined(HAVE_FFDHE_3072) && \
  78. !defined(HAVE_FFDHE_4096) && !defined(HAVE_FFDHE_6144) && \
  79. !defined(HAVE_FFDHE_8192)
  80. #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
  81. #endif
  82. #if !defined(NO_RSA) && !defined(WC_RSA_PSS)
  83. #error The build option WC_RSA_PSS is required for TLS 1.3 with RSA
  84. #endif
  85. #ifndef HAVE_TLS_EXTENSIONS
  86. #ifndef _MSC_VER
  87. #error "The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3"
  88. #else
  89. #pragma message("Error: The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3")
  90. #endif
  91. #endif
  92. #endif
  93. /* Warn if secrets logging is enabled */
  94. #if (defined(SHOW_SECRETS) || defined(WOLFSSL_SSLKEYLOGFILE)) && \
  95. !defined(WOLFSSL_KEYLOG_EXPORT_WARNED)
  96. #ifndef _MSC_VER
  97. #warning The SHOW_SECRETS and WOLFSSL_SSLKEYLOGFILE options should only be used for debugging and never in a production environment
  98. #else
  99. #pragma message("Warning: The SHOW_SECRETS and WOLFSSL_SSLKEYLOGFILE options should only be used for debugging and never in a production environment")
  100. #endif
  101. #endif
  102. #ifndef WOLFSSL_NO_TLS12
  103. #ifdef WOLFSSL_SHA384
  104. #define HSHASH_SZ WC_SHA384_DIGEST_SIZE
  105. #else
  106. #define HSHASH_SZ FINISHED_SZ
  107. #endif
  108. int BuildTlsHandshakeHash(WOLFSSL* ssl, byte* hash, word32* hashLen)
  109. {
  110. int ret = 0;
  111. word32 hashSz = FINISHED_SZ;
  112. if (ssl == NULL || hash == NULL || hashLen == NULL || *hashLen < HSHASH_SZ)
  113. return BAD_FUNC_ARG;
  114. /* for constant timing perform these even if error */
  115. #ifndef NO_OLD_TLS
  116. ret |= wc_Md5GetHash(&ssl->hsHashes->hashMd5, hash);
  117. ret |= wc_ShaGetHash(&ssl->hsHashes->hashSha, &hash[WC_MD5_DIGEST_SIZE]);
  118. #endif
  119. if (IsAtLeastTLSv1_2(ssl)) {
  120. #ifndef NO_SHA256
  121. if (ssl->specs.mac_algorithm <= sha256_mac ||
  122. ssl->specs.mac_algorithm == blake2b_mac) {
  123. ret |= wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash);
  124. hashSz = WC_SHA256_DIGEST_SIZE;
  125. }
  126. #endif
  127. #ifdef WOLFSSL_SHA384
  128. if (ssl->specs.mac_algorithm == sha384_mac) {
  129. ret |= wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash);
  130. hashSz = WC_SHA384_DIGEST_SIZE;
  131. }
  132. #endif
  133. #ifdef WOLFSSL_SM3
  134. if (ssl->specs.mac_algorithm == sm3_mac) {
  135. ret |= wc_Sm3GetHash(&ssl->hsHashes->hashSm3, hash);
  136. hashSz = WC_SM3_DIGEST_SIZE;
  137. }
  138. #endif
  139. }
  140. *hashLen = hashSz;
  141. #ifdef WOLFSSL_CHECK_MEM_ZERO
  142. wc_MemZero_Add("TLS handshake hash", hash, hashSz);
  143. #endif
  144. if (ret != 0) {
  145. ret = BUILD_MSG_ERROR;
  146. WOLFSSL_ERROR_VERBOSE(ret);
  147. }
  148. return ret;
  149. }
  150. int BuildTlsFinished(WOLFSSL* ssl, Hashes* hashes, const byte* sender)
  151. {
  152. int ret;
  153. const byte* side = NULL;
  154. word32 hashSz = HSHASH_SZ;
  155. #if !defined(WOLFSSL_ASYNC_CRYPT) || defined(WC_ASYNC_NO_HASH)
  156. byte handshake_hash[HSHASH_SZ];
  157. #else
  158. WC_DECLARE_VAR(handshake_hash, byte, HSHASH_SZ, ssl->heap);
  159. WC_ALLOC_VAR(handshake_hash, byte, HSHASH_SZ, ssl->heap);
  160. if (handshake_hash == NULL)
  161. return MEMORY_E;
  162. #endif
  163. XMEMSET(handshake_hash, 0, HSHASH_SZ);
  164. ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
  165. if (ret == 0) {
  166. if (XSTRNCMP((const char*)sender, (const char*)kTlsClientStr,
  167. SIZEOF_SENDER) == 0) {
  168. side = kTlsClientFinStr;
  169. }
  170. else if (XSTRNCMP((const char*)sender, (const char*)kTlsServerStr,
  171. SIZEOF_SENDER) == 0) {
  172. side = kTlsServerFinStr;
  173. }
  174. else {
  175. ret = BAD_FUNC_ARG;
  176. WOLFSSL_MSG("Unexpected sender value");
  177. }
  178. }
  179. if (ret == 0) {
  180. #ifdef WOLFSSL_HAVE_PRF
  181. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  182. if (ssl->ctx->TlsFinishedCb) {
  183. void* ctx = wolfSSL_GetTlsFinishedCtx(ssl);
  184. ret = ssl->ctx->TlsFinishedCb(ssl, side, handshake_hash, hashSz,
  185. (byte*)hashes, ctx);
  186. }
  187. if (!ssl->ctx->TlsFinishedCb || ret == PROTOCOLCB_UNAVAILABLE)
  188. #endif
  189. {
  190. PRIVATE_KEY_UNLOCK();
  191. ret = wc_PRF_TLS((byte*)hashes, TLS_FINISHED_SZ,
  192. ssl->arrays->masterSecret, SECRET_LEN, side,
  193. FINISHED_LABEL_SZ, handshake_hash, hashSz,
  194. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  195. ssl->heap, ssl->devId);
  196. PRIVATE_KEY_LOCK();
  197. }
  198. ForceZero(handshake_hash, hashSz);
  199. #else
  200. /* Pseudo random function must be enabled in the configuration. */
  201. ret = PRF_MISSING;
  202. WOLFSSL_ERROR_VERBOSE(ret);
  203. WOLFSSL_MSG("Pseudo-random function is not enabled");
  204. (void)side;
  205. (void)hashes;
  206. #endif
  207. }
  208. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  209. WC_FREE_VAR(handshake_hash, ssl->heap);
  210. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  211. wc_MemZero_Check(handshake_hash, HSHASH_SZ);
  212. #endif
  213. return ret;
  214. }
  215. #endif /* !WOLFSSL_NO_TLS12 */
  216. #ifndef NO_OLD_TLS
  217. #ifdef WOLFSSL_ALLOW_TLSV10
  218. ProtocolVersion MakeTLSv1(void)
  219. {
  220. ProtocolVersion pv;
  221. pv.major = SSLv3_MAJOR;
  222. pv.minor = TLSv1_MINOR;
  223. return pv;
  224. }
  225. #endif /* WOLFSSL_ALLOW_TLSV10 */
  226. ProtocolVersion MakeTLSv1_1(void)
  227. {
  228. ProtocolVersion pv;
  229. pv.major = SSLv3_MAJOR;
  230. pv.minor = TLSv1_1_MINOR;
  231. return pv;
  232. }
  233. #endif /* !NO_OLD_TLS */
  234. #ifndef WOLFSSL_NO_TLS12
  235. ProtocolVersion MakeTLSv1_2(void)
  236. {
  237. ProtocolVersion pv;
  238. pv.major = SSLv3_MAJOR;
  239. pv.minor = TLSv1_2_MINOR;
  240. return pv;
  241. }
  242. #endif /* !WOLFSSL_NO_TLS12 */
  243. #ifdef WOLFSSL_TLS13
  244. /* The TLS v1.3 protocol version.
  245. *
  246. * returns the protocol version data for TLS v1.3.
  247. */
  248. ProtocolVersion MakeTLSv1_3(void)
  249. {
  250. ProtocolVersion pv;
  251. pv.major = SSLv3_MAJOR;
  252. pv.minor = TLSv1_3_MINOR;
  253. return pv;
  254. }
  255. #endif
  256. #ifndef WOLFSSL_NO_TLS12
  257. #ifdef HAVE_EXTENDED_MASTER
  258. static const byte ext_master_label[EXT_MASTER_LABEL_SZ + 1] =
  259. "extended master secret";
  260. #endif
  261. static const byte master_label[MASTER_LABEL_SZ + 1] = "master secret";
  262. static const byte key_label [KEY_LABEL_SZ + 1] = "key expansion";
  263. static int _DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
  264. const byte* ms, word32 msLen,
  265. const byte* sr, const byte* cr,
  266. int tls1_2, int hash_type,
  267. void* heap, int devId)
  268. {
  269. int ret;
  270. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  271. WC_DECLARE_VAR(seed, byte, SEED_LEN, heap);
  272. WC_ALLOC_VAR(seed, byte, SEED_LEN, heap);
  273. if (seed == NULL)
  274. return MEMORY_E;
  275. #else
  276. byte seed[SEED_LEN];
  277. #endif
  278. XMEMCPY(seed, sr, RAN_LEN);
  279. XMEMCPY(seed + RAN_LEN, cr, RAN_LEN);
  280. #ifdef WOLFSSL_HAVE_PRF
  281. PRIVATE_KEY_UNLOCK();
  282. ret = wc_PRF_TLS(key_dig, key_dig_len, ms, msLen, key_label, KEY_LABEL_SZ,
  283. seed, SEED_LEN, tls1_2, hash_type, heap, devId);
  284. PRIVATE_KEY_LOCK();
  285. #else
  286. /* Pseudo random function must be enabled in the configuration. */
  287. ret = PRF_MISSING;
  288. WOLFSSL_ERROR_VERBOSE(ret);
  289. WOLFSSL_MSG("Pseudo-random function is not enabled");
  290. (void)key_dig;
  291. (void)key_dig_len;
  292. (void)ms;
  293. (void)msLen;
  294. (void)tls1_2;
  295. (void)hash_type;
  296. (void)heap;
  297. (void)devId;
  298. (void)key_label;
  299. (void)master_label;
  300. #ifdef HAVE_EXTENDED_MASTER
  301. (void)ext_master_label;
  302. #endif
  303. #endif
  304. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  305. WC_FREE_VAR(seed, heap);
  306. #endif
  307. return ret;
  308. }
  309. /* External facing wrapper so user can call as well, 0 on success */
  310. int wolfSSL_DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
  311. const byte* ms, word32 msLen,
  312. const byte* sr, const byte* cr,
  313. int tls1_2, int hash_type)
  314. {
  315. return _DeriveTlsKeys(key_dig, key_dig_len, ms, msLen, sr, cr, tls1_2,
  316. hash_type, NULL, INVALID_DEVID);
  317. }
  318. int DeriveTlsKeys(WOLFSSL* ssl)
  319. {
  320. int ret;
  321. int key_dig_len = 2 * ssl->specs.hash_size +
  322. 2 * ssl->specs.key_size +
  323. 2 * ssl->specs.iv_size;
  324. #ifdef WOLFSSL_SMALL_STACK
  325. byte* key_dig;
  326. #else
  327. byte key_dig[MAX_PRF_DIG];
  328. #endif
  329. #ifdef WOLFSSL_SMALL_STACK
  330. key_dig = (byte*)XMALLOC(MAX_PRF_DIG, ssl->heap, DYNAMIC_TYPE_DIGEST);
  331. if (key_dig == NULL) {
  332. return MEMORY_E;
  333. }
  334. #endif
  335. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  336. ret = PROTOCOLCB_UNAVAILABLE;
  337. if (ssl->ctx->GenSessionKeyCb) {
  338. void* ctx = wolfSSL_GetGenSessionKeyCtx(ssl);
  339. ret = ssl->ctx->GenSessionKeyCb(ssl, ctx);
  340. }
  341. if (!ssl->ctx->GenSessionKeyCb || ret == PROTOCOLCB_UNAVAILABLE)
  342. #endif
  343. ret = _DeriveTlsKeys(key_dig, key_dig_len,
  344. ssl->arrays->masterSecret, SECRET_LEN,
  345. ssl->arrays->serverRandom, ssl->arrays->clientRandom,
  346. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  347. ssl->heap, ssl->devId);
  348. if (ret == 0)
  349. ret = StoreKeys(ssl, key_dig, PROVISION_CLIENT_SERVER);
  350. #ifdef WOLFSSL_SMALL_STACK
  351. XFREE(key_dig, ssl->heap, DYNAMIC_TYPE_DIGEST);
  352. #endif
  353. return ret;
  354. }
  355. static int _MakeTlsMasterSecret(byte* ms, word32 msLen,
  356. const byte* pms, word32 pmsLen,
  357. const byte* cr, const byte* sr,
  358. int tls1_2, int hash_type,
  359. void* heap, int devId)
  360. {
  361. int ret;
  362. #if !defined(WOLFSSL_ASYNC_CRYPT) || defined(WC_ASYNC_NO_HASH)
  363. byte seed[SEED_LEN];
  364. #else
  365. WC_DECLARE_VAR(seed, byte, SEED_LEN, heap);
  366. WC_ALLOC_VAR(seed, byte, SEED_LEN, heap);
  367. if (seed == NULL)
  368. return MEMORY_E;
  369. #endif
  370. XMEMCPY(seed, cr, RAN_LEN);
  371. XMEMCPY(seed + RAN_LEN, sr, RAN_LEN);
  372. #ifdef WOLFSSL_HAVE_PRF
  373. PRIVATE_KEY_UNLOCK();
  374. ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, master_label, MASTER_LABEL_SZ,
  375. seed, SEED_LEN, tls1_2, hash_type, heap, devId);
  376. PRIVATE_KEY_LOCK();
  377. #else
  378. /* Pseudo random function must be enabled in the configuration. */
  379. ret = PRF_MISSING;
  380. WOLFSSL_MSG("Pseudo-random function is not enabled");
  381. (void)ms;
  382. (void)msLen;
  383. (void)pms;
  384. (void)pmsLen;
  385. (void)tls1_2;
  386. (void)hash_type;
  387. (void)heap;
  388. (void)devId;
  389. #endif
  390. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  391. WC_FREE_VAR(seed, heap);
  392. #endif
  393. return ret;
  394. }
  395. /* External facing wrapper so user can call as well, 0 on success */
  396. int wolfSSL_MakeTlsMasterSecret(byte* ms, word32 msLen,
  397. const byte* pms, word32 pmsLen,
  398. const byte* cr, const byte* sr,
  399. int tls1_2, int hash_type)
  400. {
  401. return _MakeTlsMasterSecret(ms, msLen, pms, pmsLen, cr, sr, tls1_2,
  402. hash_type, NULL, INVALID_DEVID);
  403. }
  404. #ifdef HAVE_EXTENDED_MASTER
  405. static int _MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
  406. const byte* pms, word32 pmsLen,
  407. const byte* sHash, word32 sHashLen,
  408. int tls1_2, int hash_type,
  409. void* heap, int devId)
  410. {
  411. int ret;
  412. #ifdef WOLFSSL_HAVE_PRF
  413. PRIVATE_KEY_UNLOCK();
  414. ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, ext_master_label, EXT_MASTER_LABEL_SZ,
  415. sHash, sHashLen, tls1_2, hash_type, heap, devId);
  416. PRIVATE_KEY_LOCK();
  417. #else
  418. /* Pseudo random function must be enabled in the configuration. */
  419. ret = PRF_MISSING;
  420. WOLFSSL_MSG("Pseudo-random function is not enabled");
  421. (void)ms;
  422. (void)msLen;
  423. (void)pms;
  424. (void)pmsLen;
  425. (void)sHash;
  426. (void)sHashLen;
  427. (void)tls1_2;
  428. (void)hash_type;
  429. (void)heap;
  430. (void)devId;
  431. #endif
  432. return ret;
  433. }
  434. /* External facing wrapper so user can call as well, 0 on success */
  435. int wolfSSL_MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
  436. const byte* pms, word32 pmsLen,
  437. const byte* sHash, word32 sHashLen,
  438. int tls1_2, int hash_type)
  439. {
  440. return _MakeTlsExtendedMasterSecret(ms, msLen, pms, pmsLen, sHash, sHashLen,
  441. tls1_2, hash_type, NULL, INVALID_DEVID);
  442. }
  443. #endif /* HAVE_EXTENDED_MASTER */
  444. int MakeTlsMasterSecret(WOLFSSL* ssl)
  445. {
  446. int ret;
  447. #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE)
  448. /* If this is called from a sniffer session with keylog file support, obtain
  449. * the master secret from the callback */
  450. if (ssl->snifferSecretCb != NULL) {
  451. ret = ssl->snifferSecretCb(ssl->arrays->clientRandom,
  452. SNIFFER_SECRET_TLS12_MASTER_SECRET,
  453. ssl->arrays->masterSecret);
  454. if (ret != 0) {
  455. return ret;
  456. }
  457. ret = DeriveTlsKeys(ssl);
  458. return ret;
  459. }
  460. #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */
  461. #ifdef HAVE_EXTENDED_MASTER
  462. if (ssl->options.haveEMS) {
  463. word32 hashSz = HSHASH_SZ;
  464. #ifdef WOLFSSL_SMALL_STACK
  465. byte* handshake_hash = (byte*)XMALLOC(HSHASH_SZ, ssl->heap,
  466. DYNAMIC_TYPE_DIGEST);
  467. if (handshake_hash == NULL)
  468. return MEMORY_E;
  469. #else
  470. byte handshake_hash[HSHASH_SZ];
  471. #endif
  472. XMEMSET(handshake_hash, 0, HSHASH_SZ);
  473. ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
  474. if (ret == 0) {
  475. ret = _MakeTlsExtendedMasterSecret(
  476. ssl->arrays->masterSecret, SECRET_LEN,
  477. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
  478. handshake_hash, hashSz,
  479. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  480. ssl->heap, ssl->devId);
  481. ForceZero(handshake_hash, hashSz);
  482. }
  483. #ifdef WOLFSSL_SMALL_STACK
  484. XFREE(handshake_hash, ssl->heap, DYNAMIC_TYPE_DIGEST);
  485. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  486. wc_MemZero_Check(handshake_hash, HSHASH_SZ);
  487. #endif
  488. }
  489. else
  490. #endif /* HAVE_EXTENDED_MASTER */
  491. {
  492. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  493. ret = PROTOCOLCB_UNAVAILABLE;
  494. if (ssl->ctx->GenMasterCb) {
  495. void* ctx = wolfSSL_GetGenMasterSecretCtx(ssl);
  496. ret = ssl->ctx->GenMasterCb(ssl, ctx);
  497. }
  498. if (!ssl->ctx->GenMasterCb || ret == PROTOCOLCB_UNAVAILABLE)
  499. #endif
  500. {
  501. ret = _MakeTlsMasterSecret(ssl->arrays->masterSecret,
  502. SECRET_LEN, ssl->arrays->preMasterSecret,
  503. ssl->arrays->preMasterSz, ssl->arrays->clientRandom,
  504. ssl->arrays->serverRandom, IsAtLeastTLSv1_2(ssl),
  505. ssl->specs.mac_algorithm, ssl->heap, ssl->devId);
  506. }
  507. }
  508. if (ret == 0) {
  509. #ifdef SHOW_SECRETS
  510. /* Wireshark Pre-Master-Secret Format:
  511. * CLIENT_RANDOM <clientrandom> <mastersecret>
  512. */
  513. const char* CLIENT_RANDOM_LABEL = "CLIENT_RANDOM";
  514. int i, pmsPos = 0;
  515. char pmsBuf[13 + 1 + 64 + 1 + 96 + 1 + 1];
  516. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%s ",
  517. CLIENT_RANDOM_LABEL);
  518. pmsPos += XSTRLEN(CLIENT_RANDOM_LABEL) + 1;
  519. for (i = 0; i < RAN_LEN; i++) {
  520. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  521. ssl->arrays->clientRandom[i]);
  522. pmsPos += 2;
  523. }
  524. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, " ");
  525. pmsPos += 1;
  526. for (i = 0; i < SECRET_LEN; i++) {
  527. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  528. ssl->arrays->masterSecret[i]);
  529. pmsPos += 2;
  530. }
  531. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "\n");
  532. pmsPos += 1;
  533. /* print master secret */
  534. puts(pmsBuf);
  535. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
  536. {
  537. FILE* f = XFOPEN(WOLFSSL_SSLKEYLOGFILE_OUTPUT, "a");
  538. if (f != XBADFILE) {
  539. XFWRITE(pmsBuf, 1, pmsPos, f);
  540. XFCLOSE(f);
  541. }
  542. }
  543. #endif
  544. #endif /* SHOW_SECRETS */
  545. ret = DeriveTlsKeys(ssl);
  546. }
  547. return ret;
  548. }
  549. /* Used by EAP-TLS and EAP-TTLS to derive keying material from
  550. * the master_secret. */
  551. int wolfSSL_make_eap_keys(WOLFSSL* ssl, void* msk, unsigned int len,
  552. const char* label)
  553. {
  554. int ret;
  555. #ifdef WOLFSSL_SMALL_STACK
  556. byte* seed;
  557. #else
  558. byte seed[SEED_LEN];
  559. #endif
  560. #ifdef WOLFSSL_SMALL_STACK
  561. seed = (byte*)XMALLOC(SEED_LEN, ssl->heap, DYNAMIC_TYPE_SEED);
  562. if (seed == NULL)
  563. return MEMORY_E;
  564. #endif
  565. /*
  566. * As per RFC-5281, the order of the client and server randoms is reversed
  567. * from that used by the TLS protocol to derive keys.
  568. */
  569. XMEMCPY(seed, ssl->arrays->clientRandom, RAN_LEN);
  570. XMEMCPY(seed + RAN_LEN, ssl->arrays->serverRandom, RAN_LEN);
  571. #ifdef WOLFSSL_HAVE_PRF
  572. PRIVATE_KEY_UNLOCK();
  573. ret = wc_PRF_TLS((byte*)msk, len, ssl->arrays->masterSecret, SECRET_LEN,
  574. (const byte *)label, (word32)XSTRLEN(label), seed, SEED_LEN,
  575. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  576. ssl->heap, ssl->devId);
  577. PRIVATE_KEY_LOCK();
  578. #else
  579. /* Pseudo random function must be enabled in the configuration. */
  580. ret = PRF_MISSING;
  581. WOLFSSL_MSG("Pseudo-random function is not enabled");
  582. (void)msk;
  583. (void)len;
  584. (void)label;
  585. #endif
  586. #ifdef WOLFSSL_SMALL_STACK
  587. XFREE(seed, ssl->heap, DYNAMIC_TYPE_SEED);
  588. #endif
  589. return ret;
  590. }
  591. /* return HMAC digest type in wolfSSL format */
  592. int wolfSSL_GetHmacType(WOLFSSL* ssl)
  593. {
  594. if (ssl == NULL)
  595. return BAD_FUNC_ARG;
  596. return wolfSSL_GetHmacType_ex(&ssl->specs);
  597. }
  598. int wolfSSL_SetTlsHmacInner(WOLFSSL* ssl, byte* inner, word32 sz, int content,
  599. int verify)
  600. {
  601. if (ssl == NULL || inner == NULL)
  602. return BAD_FUNC_ARG;
  603. XMEMSET(inner, 0, WOLFSSL_TLS_HMAC_INNER_SZ);
  604. WriteSEQ(ssl, verify, inner);
  605. inner[SEQ_SZ] = (byte)content;
  606. inner[SEQ_SZ + ENUM_LEN] = ssl->version.major;
  607. inner[SEQ_SZ + ENUM_LEN + ENUM_LEN] = ssl->version.minor;
  608. c16toa((word16)sz, inner + SEQ_SZ + ENUM_LEN + VERSION_SZ);
  609. return 0;
  610. }
  611. #ifndef WOLFSSL_AEAD_ONLY
  612. #if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
  613. !defined(HAVE_SELFTEST)
  614. /* Update the hash in the HMAC.
  615. *
  616. * hmac HMAC object.
  617. * data Data to be hashed.
  618. * sz Size of data to hash.
  619. * returns 0 on success, otherwise failure.
  620. */
  621. static int Hmac_HashUpdate(Hmac* hmac, const byte* data, word32 sz)
  622. {
  623. int ret = BAD_FUNC_ARG;
  624. switch (hmac->macType) {
  625. #ifndef NO_SHA
  626. case WC_SHA:
  627. ret = wc_ShaUpdate(&hmac->hash.sha, data, sz);
  628. break;
  629. #endif /* !NO_SHA */
  630. #ifndef NO_SHA256
  631. case WC_SHA256:
  632. ret = wc_Sha256Update(&hmac->hash.sha256, data, sz);
  633. break;
  634. #endif /* !NO_SHA256 */
  635. #ifdef WOLFSSL_SHA384
  636. case WC_SHA384:
  637. ret = wc_Sha384Update(&hmac->hash.sha384, data, sz);
  638. break;
  639. #endif /* WOLFSSL_SHA384 */
  640. #ifdef WOLFSSL_SHA512
  641. case WC_SHA512:
  642. ret = wc_Sha512Update(&hmac->hash.sha512, data, sz);
  643. break;
  644. #endif /* WOLFSSL_SHA512 */
  645. #ifdef WOLFSSL_SM3
  646. case WC_SM3:
  647. ret = wc_Sm3Update(&hmac->hash.sm3, data, sz);
  648. break;
  649. #endif /* WOLFSSL_SM3 */
  650. default:
  651. break;
  652. }
  653. return ret;
  654. }
  655. /* Finalize the hash but don't put the EOC, padding or length in.
  656. *
  657. * hmac HMAC object.
  658. * hash Hash result.
  659. * returns 0 on success, otherwise failure.
  660. */
  661. static int Hmac_HashFinalRaw(Hmac* hmac, unsigned char* hash)
  662. {
  663. int ret = BAD_FUNC_ARG;
  664. switch (hmac->macType) {
  665. #ifndef NO_SHA
  666. case WC_SHA:
  667. ret = wc_ShaFinalRaw(&hmac->hash.sha, hash);
  668. break;
  669. #endif /* !NO_SHA */
  670. #ifndef NO_SHA256
  671. case WC_SHA256:
  672. ret = wc_Sha256FinalRaw(&hmac->hash.sha256, hash);
  673. break;
  674. #endif /* !NO_SHA256 */
  675. #ifdef WOLFSSL_SHA384
  676. case WC_SHA384:
  677. ret = wc_Sha384FinalRaw(&hmac->hash.sha384, hash);
  678. break;
  679. #endif /* WOLFSSL_SHA384 */
  680. #ifdef WOLFSSL_SHA512
  681. case WC_SHA512:
  682. ret = wc_Sha512FinalRaw(&hmac->hash.sha512, hash);
  683. break;
  684. #endif /* WOLFSSL_SHA512 */
  685. #ifdef WOLFSSL_SM3
  686. case WC_SM3:
  687. ret = wc_Sm3FinalRaw(&hmac->hash.sm3, hash);
  688. break;
  689. #endif /* WOLFSSL_SM3 */
  690. default:
  691. break;
  692. }
  693. return ret;
  694. }
  695. /* Finalize the HMAC by performing outer hash.
  696. *
  697. * hmac HMAC object.
  698. * mac MAC result.
  699. * returns 0 on success, otherwise failure.
  700. */
  701. static int Hmac_OuterHash(Hmac* hmac, unsigned char* mac)
  702. {
  703. int ret = BAD_FUNC_ARG;
  704. wc_HashAlg hash;
  705. enum wc_HashType hashType = (enum wc_HashType)hmac->macType;
  706. int digestSz = wc_HashGetDigestSize(hashType);
  707. int blockSz = wc_HashGetBlockSize(hashType);
  708. if ((digestSz >= 0) && (blockSz >= 0)) {
  709. ret = wc_HashInit(&hash, hashType);
  710. }
  711. if (ret == 0) {
  712. ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->opad,
  713. blockSz);
  714. if (ret == 0)
  715. ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->innerHash,
  716. digestSz);
  717. if (ret == 0)
  718. ret = wc_HashFinal(&hash, hashType, mac);
  719. wc_HashFree(&hash, hashType);
  720. }
  721. return ret;
  722. }
  723. /* Calculate the HMAC of the header + message data.
  724. * Constant time implementation using wc_Sha*FinalRaw().
  725. *
  726. * hmac HMAC object.
  727. * digest MAC result.
  728. * in Message data.
  729. * sz Size of the message data.
  730. * header Constructed record header with length of handshake data.
  731. * returns 0 on success, otherwise failure.
  732. */
  733. static int Hmac_UpdateFinal_CT(Hmac* hmac, byte* digest, const byte* in,
  734. word32 sz, int macLen, byte* header)
  735. {
  736. byte lenBytes[8];
  737. int i, j;
  738. unsigned int k;
  739. int blockBits, blockMask;
  740. int lastBlockLen, extraLen, eocIndex;
  741. int blocks, safeBlocks, lenBlock, eocBlock;
  742. unsigned int maxLen;
  743. int blockSz, padSz;
  744. int ret;
  745. word32 realLen;
  746. byte extraBlock;
  747. switch (hmac->macType) {
  748. #ifndef NO_SHA
  749. case WC_SHA:
  750. blockSz = WC_SHA_BLOCK_SIZE;
  751. blockBits = 6;
  752. padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
  753. break;
  754. #endif /* !NO_SHA */
  755. #ifndef NO_SHA256
  756. case WC_SHA256:
  757. blockSz = WC_SHA256_BLOCK_SIZE;
  758. blockBits = 6;
  759. padSz = WC_SHA256_BLOCK_SIZE - WC_SHA256_PAD_SIZE + 1;
  760. break;
  761. #endif /* !NO_SHA256 */
  762. #ifdef WOLFSSL_SHA384
  763. case WC_SHA384:
  764. blockSz = WC_SHA384_BLOCK_SIZE;
  765. blockBits = 7;
  766. padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
  767. break;
  768. #endif /* WOLFSSL_SHA384 */
  769. #ifdef WOLFSSL_SHA512
  770. case WC_SHA512:
  771. blockSz = WC_SHA512_BLOCK_SIZE;
  772. blockBits = 7;
  773. padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
  774. break;
  775. #endif /* WOLFSSL_SHA512 */
  776. #ifdef WOLFSSL_SM3
  777. case WC_SM3:
  778. blockSz = WC_SM3_BLOCK_SIZE;
  779. blockBits = 6;
  780. padSz = WC_SM3_BLOCK_SIZE - WC_SM3_PAD_SIZE + 1;
  781. break;
  782. #endif /* WOLFSSL_SM3 */
  783. default:
  784. return BAD_FUNC_ARG;
  785. }
  786. blockMask = blockSz - 1;
  787. /* Size of data to HMAC if padding length byte is zero. */
  788. maxLen = WOLFSSL_TLS_HMAC_INNER_SZ + sz - 1 - macLen;
  789. /* Complete data (including padding) has block for EOC and/or length. */
  790. extraBlock = ctSetLTE((maxLen + padSz) & blockMask, padSz);
  791. /* Total number of blocks for data including padding. */
  792. blocks = ((maxLen + blockSz - 1) >> blockBits) + extraBlock;
  793. /* Up to last 6 blocks can be hashed safely. */
  794. safeBlocks = blocks - 6;
  795. /* Length of message data. */
  796. realLen = maxLen - in[sz - 1];
  797. /* Number of message bytes in last block. */
  798. lastBlockLen = realLen & blockMask;
  799. /* Number of padding bytes in last block. */
  800. extraLen = ((blockSz * 2 - padSz - lastBlockLen) & blockMask) + 1;
  801. /* Number of blocks to create for hash. */
  802. lenBlock = (realLen + extraLen) >> blockBits;
  803. /* Block containing EOC byte. */
  804. eocBlock = realLen >> blockBits;
  805. /* Index of EOC byte in block. */
  806. eocIndex = realLen & blockMask;
  807. /* Add length of hmac's ipad to total length. */
  808. realLen += blockSz;
  809. /* Length as bits - 8 bytes bigendian. */
  810. c32toa(realLen >> ((sizeof(word32) * 8) - 3), lenBytes);
  811. c32toa(realLen << 3, lenBytes + sizeof(word32));
  812. ret = Hmac_HashUpdate(hmac, (unsigned char*)hmac->ipad, blockSz);
  813. if (ret != 0)
  814. return ret;
  815. XMEMSET(hmac->innerHash, 0, macLen);
  816. if (safeBlocks > 0) {
  817. ret = Hmac_HashUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
  818. if (ret != 0)
  819. return ret;
  820. ret = Hmac_HashUpdate(hmac, in, safeBlocks * blockSz -
  821. WOLFSSL_TLS_HMAC_INNER_SZ);
  822. if (ret != 0)
  823. return ret;
  824. }
  825. else
  826. safeBlocks = 0;
  827. XMEMSET(digest, 0, macLen);
  828. k = safeBlocks * blockSz;
  829. for (i = safeBlocks; i < blocks; i++) {
  830. unsigned char hashBlock[WC_MAX_BLOCK_SIZE];
  831. unsigned char isEocBlock = ctMaskEq(i, eocBlock);
  832. unsigned char isOutBlock = ctMaskEq(i, lenBlock);
  833. for (j = 0; j < blockSz; j++) {
  834. unsigned char atEoc = ctMaskEq(j, eocIndex) & isEocBlock;
  835. unsigned char pastEoc = ctMaskGT(j, eocIndex) & isEocBlock;
  836. unsigned char b = 0;
  837. if (k < WOLFSSL_TLS_HMAC_INNER_SZ)
  838. b = header[k];
  839. else if (k < maxLen)
  840. b = in[k - WOLFSSL_TLS_HMAC_INNER_SZ];
  841. k++;
  842. b = ctMaskSel(atEoc, 0x80, b);
  843. b &= (unsigned char)~(word32)pastEoc;
  844. b &= ((unsigned char)~(word32)isOutBlock) | isEocBlock;
  845. if (j >= blockSz - 8) {
  846. b = ctMaskSel(isOutBlock, lenBytes[j - (blockSz - 8)], b);
  847. }
  848. hashBlock[j] = b;
  849. }
  850. ret = Hmac_HashUpdate(hmac, hashBlock, blockSz);
  851. if (ret != 0)
  852. return ret;
  853. ret = Hmac_HashFinalRaw(hmac, hashBlock);
  854. if (ret != 0)
  855. return ret;
  856. for (j = 0; j < macLen; j++)
  857. ((unsigned char*)hmac->innerHash)[j] |= hashBlock[j] & isOutBlock;
  858. }
  859. ret = Hmac_OuterHash(hmac, digest);
  860. return ret;
  861. }
  862. #endif
  863. #if defined(WOLFSSL_NO_HASH_RAW) || defined(HAVE_FIPS) || \
  864. defined(HAVE_SELFTEST) || defined(HAVE_BLAKE2)
  865. /* Calculate the HMAC of the header + message data.
  866. * Constant time implementation using normal hashing operations.
  867. * Update-Final need to be constant time.
  868. *
  869. * hmac HMAC object.
  870. * digest MAC result.
  871. * in Message data.
  872. * sz Size of the message data.
  873. * header Constructed record header with length of handshake data.
  874. * returns 0 on success, otherwise failure.
  875. */
  876. static int Hmac_UpdateFinal(Hmac* hmac, byte* digest, const byte* in,
  877. word32 sz, byte* header)
  878. {
  879. byte dummy[WC_MAX_BLOCK_SIZE] = {0};
  880. int ret = 0;
  881. word32 msgSz, blockSz, macSz, padSz, maxSz, realSz;
  882. word32 offset = 0;
  883. int msgBlocks, blocks, blockBits;
  884. int i;
  885. switch (hmac->macType) {
  886. #ifndef NO_SHA
  887. case WC_SHA:
  888. blockSz = WC_SHA_BLOCK_SIZE;
  889. blockBits = 6;
  890. macSz = WC_SHA_DIGEST_SIZE;
  891. padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
  892. break;
  893. #endif /* !NO_SHA */
  894. #ifndef NO_SHA256
  895. case WC_SHA256:
  896. blockSz = WC_SHA256_BLOCK_SIZE;
  897. blockBits = 6;
  898. macSz = WC_SHA256_DIGEST_SIZE;
  899. padSz = WC_SHA256_BLOCK_SIZE - WC_SHA256_PAD_SIZE + 1;
  900. break;
  901. #endif /* !NO_SHA256 */
  902. #ifdef WOLFSSL_SHA384
  903. case WC_SHA384:
  904. blockSz = WC_SHA384_BLOCK_SIZE;
  905. blockBits = 7;
  906. macSz = WC_SHA384_DIGEST_SIZE;
  907. padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
  908. break;
  909. #endif /* WOLFSSL_SHA384 */
  910. #ifdef WOLFSSL_SHA512
  911. case WC_SHA512:
  912. blockSz = WC_SHA512_BLOCK_SIZE;
  913. blockBits = 7;
  914. macSz = WC_SHA512_DIGEST_SIZE;
  915. padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
  916. break;
  917. #endif /* WOLFSSL_SHA512 */
  918. #ifdef HAVE_BLAKE2
  919. case WC_HASH_TYPE_BLAKE2B:
  920. blockSz = BLAKE2B_BLOCKBYTES;
  921. blockBits = 7;
  922. macSz = BLAKE2B_256;
  923. padSz = 0;
  924. break;
  925. #endif /* HAVE_BLAKE2 */
  926. #ifdef WOLFSSL_SM3
  927. case WC_SM3:
  928. blockSz = WC_SM3_BLOCK_SIZE;
  929. blockBits = 6;
  930. macSz = WC_SM3_DIGEST_SIZE;
  931. padSz = WC_SM3_BLOCK_SIZE - WC_SM3_PAD_SIZE + 1;
  932. break;
  933. #endif
  934. default:
  935. WOLFSSL_MSG("ERROR: Hmac_UpdateFinal failed, no hmac->macType");
  936. return BAD_FUNC_ARG;
  937. }
  938. msgSz = sz - (1 + in[sz - 1] + macSz);
  939. /* Make negative result 0 */
  940. msgSz &= ~(0 - (msgSz >> 31));
  941. realSz = WOLFSSL_TLS_HMAC_INNER_SZ + msgSz;
  942. maxSz = WOLFSSL_TLS_HMAC_INNER_SZ + (sz - 1) - macSz;
  943. /* Make negative result 0 */
  944. maxSz &= ~(0 - (maxSz >> 31));
  945. /* Calculate #blocks processed in HMAC for max and real data. */
  946. blocks = maxSz >> blockBits;
  947. blocks += ((maxSz + padSz) % blockSz) < padSz;
  948. msgBlocks = realSz >> blockBits;
  949. /* #Extra blocks to process. */
  950. blocks -= msgBlocks + ((((realSz + padSz) % blockSz) < padSz) ? 1 : 0);
  951. /* Calculate whole blocks. */
  952. msgBlocks--;
  953. ret = wc_HmacUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
  954. if (ret == 0) {
  955. /* Fill the rest of the block with any available data. */
  956. word32 currSz = ctMaskLT(msgSz, blockSz) & msgSz;
  957. currSz |= ctMaskGTE(msgSz, blockSz) & blockSz;
  958. currSz -= WOLFSSL_TLS_HMAC_INNER_SZ;
  959. currSz &= ~(0 - (currSz >> 31));
  960. ret = wc_HmacUpdate(hmac, in, currSz);
  961. offset = currSz;
  962. }
  963. if (ret == 0) {
  964. /* Do the hash operations on a block basis. */
  965. for (i = 0; i < msgBlocks; i++, offset += blockSz) {
  966. ret = wc_HmacUpdate(hmac, in + offset, blockSz);
  967. if (ret != 0)
  968. break;
  969. }
  970. }
  971. if (ret == 0)
  972. ret = wc_HmacUpdate(hmac, in + offset, msgSz - offset);
  973. if (ret == 0)
  974. ret = wc_HmacFinal(hmac, digest);
  975. if (ret == 0) {
  976. /* Do the dummy hash operations. Do at least one. */
  977. for (i = 0; i < blocks + 1; i++) {
  978. ret = wc_HmacUpdate(hmac, dummy, blockSz);
  979. if (ret != 0)
  980. break;
  981. }
  982. }
  983. return ret;
  984. }
  985. #endif
  986. int TLS_hmac(WOLFSSL* ssl, byte* digest, const byte* in, word32 sz, int padSz,
  987. int content, int verify, int epochOrder)
  988. {
  989. Hmac hmac;
  990. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  991. int ret = 0;
  992. const byte* macSecret = NULL;
  993. word32 hashSz = 0;
  994. if (ssl == NULL)
  995. return BAD_FUNC_ARG;
  996. #ifdef HAVE_TRUNCATED_HMAC
  997. hashSz = ssl->truncated_hmac ? (byte)TRUNCATED_HMAC_SZ
  998. : ssl->specs.hash_size;
  999. #else
  1000. hashSz = ssl->specs.hash_size;
  1001. #endif
  1002. #ifdef HAVE_FUZZER
  1003. /* Fuzz "in" buffer with sz to be used in HMAC algorithm */
  1004. if (ssl->fuzzerCb) {
  1005. if (verify && padSz >= 0) {
  1006. ssl->fuzzerCb(ssl, in, sz + hashSz + padSz + 1, FUZZ_HMAC,
  1007. ssl->fuzzerCtx);
  1008. }
  1009. else {
  1010. ssl->fuzzerCb(ssl, in, sz, FUZZ_HMAC, ssl->fuzzerCtx);
  1011. }
  1012. }
  1013. #endif
  1014. if (!ssl->options.dtls)
  1015. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, verify);
  1016. else
  1017. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, epochOrder);
  1018. ret = wc_HmacInit(&hmac, ssl->heap, ssl->devId);
  1019. if (ret != 0)
  1020. return ret;
  1021. #ifdef WOLFSSL_DTLS
  1022. if (ssl->options.dtls)
  1023. macSecret = wolfSSL_GetDtlsMacSecret(ssl, verify, epochOrder);
  1024. else
  1025. macSecret = wolfSSL_GetMacSecret(ssl, verify);
  1026. #else
  1027. macSecret = wolfSSL_GetMacSecret(ssl, verify);
  1028. #endif
  1029. ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
  1030. macSecret,
  1031. ssl->specs.hash_size);
  1032. if (ret == 0) {
  1033. /* Constant time verification required. */
  1034. if (verify && padSz >= 0) {
  1035. #if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
  1036. !defined(HAVE_SELFTEST)
  1037. #ifdef HAVE_BLAKE2
  1038. if (wolfSSL_GetHmacType(ssl) == WC_HASH_TYPE_BLAKE2B) {
  1039. ret = Hmac_UpdateFinal(&hmac, digest, in,
  1040. sz + hashSz + padSz + 1, myInner);
  1041. }
  1042. else
  1043. #endif
  1044. {
  1045. ret = Hmac_UpdateFinal_CT(&hmac, digest, in,
  1046. sz + hashSz + padSz + 1, hashSz, myInner);
  1047. }
  1048. #else
  1049. ret = Hmac_UpdateFinal(&hmac, digest, in, sz + hashSz + padSz + 1,
  1050. myInner);
  1051. #endif
  1052. }
  1053. else {
  1054. ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
  1055. if (ret == 0)
  1056. ret = wc_HmacUpdate(&hmac, in, sz); /* content */
  1057. if (ret == 0)
  1058. ret = wc_HmacFinal(&hmac, digest);
  1059. }
  1060. }
  1061. wc_HmacFree(&hmac);
  1062. return ret;
  1063. }
  1064. #endif /* WOLFSSL_AEAD_ONLY */
  1065. #endif /* !WOLFSSL_NO_TLS12 */
  1066. int wolfSSL_GetHmacType_ex(CipherSpecs* specs)
  1067. {
  1068. if (specs == NULL)
  1069. return BAD_FUNC_ARG;
  1070. switch (specs->mac_algorithm) {
  1071. #ifndef NO_MD5
  1072. case md5_mac:
  1073. {
  1074. return WC_MD5;
  1075. }
  1076. #endif
  1077. #ifndef NO_SHA256
  1078. case sha256_mac:
  1079. {
  1080. return WC_SHA256;
  1081. }
  1082. #endif
  1083. #ifdef WOLFSSL_SHA384
  1084. case sha384_mac:
  1085. {
  1086. return WC_SHA384;
  1087. }
  1088. #endif
  1089. #ifdef WOLFSSL_SM3
  1090. case sm3_mac:
  1091. {
  1092. return WC_SM3;
  1093. }
  1094. #endif
  1095. #ifndef NO_SHA
  1096. case sha_mac:
  1097. {
  1098. return WC_SHA;
  1099. }
  1100. #endif
  1101. #ifdef HAVE_BLAKE2
  1102. case blake2b_mac:
  1103. {
  1104. return BLAKE2B_ID;
  1105. }
  1106. #endif
  1107. default:
  1108. {
  1109. return WOLFSSL_FATAL_ERROR;
  1110. }
  1111. }
  1112. }
  1113. #ifdef HAVE_TLS_EXTENSIONS
  1114. /**
  1115. * The TLSX semaphore is used to calculate the size of the extensions to be sent
  1116. * from one peer to another.
  1117. */
  1118. /** Supports up to 72 flags. Increase as needed. */
  1119. #define SEMAPHORE_SIZE 9
  1120. /**
  1121. * Converts the extension type (id) to an index in the semaphore.
  1122. *
  1123. * Official reference for TLS extension types:
  1124. * http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xml
  1125. *
  1126. * Motivation:
  1127. * Previously, we used the extension type itself as the index of that
  1128. * extension in the semaphore as the extension types were declared
  1129. * sequentially, but maintain a semaphore as big as the number of available
  1130. * extensions is no longer an option since the release of renegotiation_info.
  1131. *
  1132. * How to update:
  1133. * Assign extension types that extrapolate the number of available semaphores
  1134. * to the first available index going backwards in the semaphore array.
  1135. * When adding a new extension type that don't extrapolate the number of
  1136. * available semaphores, check for a possible collision with with a
  1137. * 'remapped' extension type.
  1138. *
  1139. * Update TLSX_Parse for duplicate detection if more added above 62.
  1140. */
  1141. static WC_INLINE word16 TLSX_ToSemaphore(word16 type)
  1142. {
  1143. switch (type) {
  1144. case TLSX_RENEGOTIATION_INFO: /* 0xFF01 */
  1145. return 63;
  1146. #ifdef WOLFSSL_QUIC
  1147. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT: /* 0xffa5 */
  1148. return 64;
  1149. #endif
  1150. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  1151. case TLSX_ECH: /* 0xfe0d */
  1152. return 65;
  1153. #endif
  1154. #ifdef WOLFSSL_DUAL_ALG_CERTS
  1155. case TLSX_CKS:
  1156. return 66;
  1157. #endif
  1158. default:
  1159. if (type > 62) {
  1160. /* This message SHOULD only happens during the adding of
  1161. new TLS extensions in which its IANA number overflows
  1162. the current semaphore's range, or if its number already
  1163. is assigned to be used by another extension.
  1164. Use this check value for the new extension and decrement
  1165. the check value by one. */
  1166. WOLFSSL_MSG("### TLSX semaphore collision or overflow detected!");
  1167. }
  1168. }
  1169. return type;
  1170. }
  1171. /** Checks if a specific light (tls extension) is not set in the semaphore. */
  1172. #define IS_OFF(semaphore, light) \
  1173. (!(((semaphore)[(light) / 8] & (byte) (0x01 << ((light) % 8)))))
  1174. /** Turn on a specific light (tls extension) in the semaphore. */
  1175. /* the semaphore marks the extensions already written to the message */
  1176. #define TURN_ON(semaphore, light) \
  1177. ((semaphore)[(light) / 8] |= (byte) (0x01 << ((light) % 8)))
  1178. /** Turn off a specific light (tls extension) in the semaphore. */
  1179. #define TURN_OFF(semaphore, light) \
  1180. ((semaphore)[(light) / 8] &= (byte) ~(0x01 << ((light) % 8)))
  1181. /** Creates a new extension. */
  1182. static TLSX* TLSX_New(TLSX_Type type, const void* data, void* heap)
  1183. {
  1184. TLSX* extension = (TLSX*)XMALLOC(sizeof(TLSX), heap, DYNAMIC_TYPE_TLSX);
  1185. (void)heap;
  1186. if (extension) {
  1187. extension->type = type;
  1188. extension->data = (void*)data;
  1189. extension->resp = 0;
  1190. extension->next = NULL;
  1191. }
  1192. return extension;
  1193. }
  1194. /**
  1195. * Creates a new extension and appends it to the provided list.
  1196. * Checks for duplicate extensions, keeps the newest.
  1197. */
  1198. int TLSX_Append(TLSX** list, TLSX_Type type, const void* data, void* heap)
  1199. {
  1200. TLSX* extension = TLSX_New(type, data, heap);
  1201. TLSX* cur;
  1202. TLSX** prevNext = list;
  1203. if (extension == NULL)
  1204. return MEMORY_E;
  1205. for (cur = *list; cur != NULL;) {
  1206. if (cur->type == type) {
  1207. *prevNext = cur->next;
  1208. cur->next = NULL;
  1209. TLSX_FreeAll(cur, heap);
  1210. cur = *prevNext;
  1211. }
  1212. else {
  1213. prevNext = &cur->next;
  1214. cur = cur->next;
  1215. }
  1216. }
  1217. /* Append the extension to the list */
  1218. *prevNext = extension;
  1219. return 0;
  1220. }
  1221. /**
  1222. * Creates a new extension and pushes it to the provided list.
  1223. * Checks for duplicate extensions, keeps the newest.
  1224. */
  1225. int TLSX_Push(TLSX** list, TLSX_Type type, const void* data, void* heap)
  1226. {
  1227. TLSX* extension = TLSX_New(type, data, heap);
  1228. if (extension == NULL)
  1229. return MEMORY_E;
  1230. /* pushes the new extension on the list. */
  1231. extension->next = *list;
  1232. *list = extension;
  1233. /* remove duplicate extensions, there should be only one of each type. */
  1234. do {
  1235. if (extension->next && extension->next->type == type) {
  1236. TLSX *next = extension->next;
  1237. extension->next = next->next;
  1238. next->next = NULL;
  1239. TLSX_FreeAll(next, heap);
  1240. /* there is no way to occur more than
  1241. * two extensions of the same type.
  1242. */
  1243. break;
  1244. }
  1245. } while ((extension = extension->next));
  1246. return 0;
  1247. }
  1248. #ifndef NO_WOLFSSL_CLIENT
  1249. int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type);
  1250. int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type)
  1251. {
  1252. TLSX *extension = TLSX_Find(ssl->extensions, type);
  1253. if (!extension)
  1254. extension = TLSX_Find(ssl->ctx->extensions, type);
  1255. return extension == NULL;
  1256. }
  1257. int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl);
  1258. int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl)
  1259. {
  1260. SendAlert(ssl, alert_fatal, unsupported_extension);
  1261. WOLFSSL_ERROR_VERBOSE(UNSUPPORTED_EXTENSION);
  1262. return UNSUPPORTED_EXTENSION;
  1263. }
  1264. #else
  1265. #define TLSX_CheckUnsupportedExtension(ssl, type) 0
  1266. #define TLSX_HandleUnsupportedExtension(ssl) 0
  1267. #endif
  1268. #if !defined(NO_WOLFSSL_SERVER) || defined(WOLFSSL_TLS13)
  1269. void TLSX_SetResponse(WOLFSSL* ssl, TLSX_Type type);
  1270. /** Mark an extension to be sent back to the client. */
  1271. void TLSX_SetResponse(WOLFSSL* ssl, TLSX_Type type)
  1272. {
  1273. TLSX *extension = TLSX_Find(ssl->extensions, type);
  1274. if (extension)
  1275. extension->resp = 1;
  1276. }
  1277. #endif
  1278. /******************************************************************************/
  1279. /* Application-Layer Protocol Negotiation */
  1280. /******************************************************************************/
  1281. #ifdef HAVE_ALPN
  1282. /** Creates a new ALPN object, providing protocol name to use. */
  1283. static ALPN* TLSX_ALPN_New(char *protocol_name, word16 protocol_nameSz,
  1284. void* heap)
  1285. {
  1286. ALPN *alpn;
  1287. WOLFSSL_ENTER("TLSX_ALPN_New");
  1288. if (protocol_name == NULL ||
  1289. protocol_nameSz > WOLFSSL_MAX_ALPN_PROTO_NAME_LEN) {
  1290. WOLFSSL_MSG("Invalid arguments");
  1291. return NULL;
  1292. }
  1293. alpn = (ALPN*)XMALLOC(sizeof(ALPN), heap, DYNAMIC_TYPE_TLSX);
  1294. if (alpn == NULL) {
  1295. WOLFSSL_MSG("Memory failure");
  1296. return NULL;
  1297. }
  1298. alpn->next = NULL;
  1299. alpn->negotiated = 0;
  1300. alpn->options = 0;
  1301. alpn->protocol_name = (char*)XMALLOC(protocol_nameSz + 1,
  1302. heap, DYNAMIC_TYPE_TLSX);
  1303. if (alpn->protocol_name == NULL) {
  1304. WOLFSSL_MSG("Memory failure");
  1305. XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
  1306. return NULL;
  1307. }
  1308. XMEMCPY(alpn->protocol_name, protocol_name, protocol_nameSz);
  1309. alpn->protocol_name[protocol_nameSz] = 0;
  1310. (void)heap;
  1311. return alpn;
  1312. }
  1313. /** Releases an ALPN object. */
  1314. static void TLSX_ALPN_Free(ALPN *alpn, void* heap)
  1315. {
  1316. (void)heap;
  1317. if (alpn == NULL)
  1318. return;
  1319. XFREE(alpn->protocol_name, heap, DYNAMIC_TYPE_TLSX);
  1320. XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
  1321. }
  1322. /** Releases all ALPN objects in the provided list. */
  1323. static void TLSX_ALPN_FreeAll(ALPN *list, void* heap)
  1324. {
  1325. ALPN* alpn;
  1326. while ((alpn = list)) {
  1327. list = alpn->next;
  1328. TLSX_ALPN_Free(alpn, heap);
  1329. }
  1330. }
  1331. /** Tells the buffered size of the ALPN objects in a list. */
  1332. static word16 TLSX_ALPN_GetSize(ALPN *list)
  1333. {
  1334. ALPN* alpn;
  1335. word16 length = OPAQUE16_LEN; /* list length */
  1336. while ((alpn = list)) {
  1337. list = alpn->next;
  1338. length++; /* protocol name length is on one byte */
  1339. length += (word16)XSTRLEN(alpn->protocol_name);
  1340. }
  1341. return length;
  1342. }
  1343. /** Writes the ALPN objects of a list in a buffer. */
  1344. static word16 TLSX_ALPN_Write(ALPN *list, byte *output)
  1345. {
  1346. ALPN* alpn;
  1347. word16 length = 0;
  1348. word16 offset = OPAQUE16_LEN; /* list length offset */
  1349. while ((alpn = list)) {
  1350. list = alpn->next;
  1351. length = (word16)XSTRLEN(alpn->protocol_name);
  1352. /* protocol name length */
  1353. output[offset++] = (byte)length;
  1354. /* protocol name value */
  1355. XMEMCPY(output + offset, alpn->protocol_name, length);
  1356. offset += length;
  1357. }
  1358. /* writing list length */
  1359. c16toa(offset - OPAQUE16_LEN, output);
  1360. return offset;
  1361. }
  1362. /** Finds a protocol name in the provided ALPN list */
  1363. static ALPN* TLSX_ALPN_Find(ALPN *list, char *protocol_name, word16 size)
  1364. {
  1365. ALPN *alpn;
  1366. if (list == NULL || protocol_name == NULL)
  1367. return NULL;
  1368. alpn = list;
  1369. while (alpn != NULL && (
  1370. (word16)XSTRLEN(alpn->protocol_name) != size ||
  1371. XSTRNCMP(alpn->protocol_name, protocol_name, size)))
  1372. alpn = alpn->next;
  1373. return alpn;
  1374. }
  1375. /** Set the ALPN matching client and server requirements */
  1376. static int TLSX_SetALPN(TLSX** extensions, const void* data, word16 size,
  1377. void* heap)
  1378. {
  1379. ALPN *alpn;
  1380. int ret;
  1381. if (extensions == NULL || data == NULL)
  1382. return BAD_FUNC_ARG;
  1383. alpn = TLSX_ALPN_New((char *)data, size, heap);
  1384. if (alpn == NULL) {
  1385. WOLFSSL_MSG("Memory failure");
  1386. return MEMORY_E;
  1387. }
  1388. alpn->negotiated = 1;
  1389. ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL, (void*)alpn,
  1390. heap);
  1391. if (ret != 0) {
  1392. TLSX_ALPN_Free(alpn, heap);
  1393. return ret;
  1394. }
  1395. return WOLFSSL_SUCCESS;
  1396. }
  1397. static int ALPN_find_match(WOLFSSL *ssl, TLSX **pextension,
  1398. const byte **psel, byte *psel_len,
  1399. const byte *alpn_val, word16 alpn_val_len)
  1400. {
  1401. TLSX *extension;
  1402. ALPN *alpn, *list;
  1403. const byte *sel = NULL, *s;
  1404. byte sel_len = 0, wlen;
  1405. extension = TLSX_Find(ssl->extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1406. if (extension == NULL)
  1407. extension = TLSX_Find(ssl->ctx->extensions,
  1408. TLSX_APPLICATION_LAYER_PROTOCOL);
  1409. /* No ALPN configured here */
  1410. if (extension == NULL || extension->data == NULL) {
  1411. *pextension = NULL;
  1412. *psel = NULL;
  1413. *psel_len = 0;
  1414. return 0;
  1415. }
  1416. list = (ALPN*)extension->data;
  1417. for (s = alpn_val;
  1418. (s - alpn_val) < alpn_val_len;
  1419. s += wlen) {
  1420. wlen = *s++; /* bounds already checked on save */
  1421. alpn = TLSX_ALPN_Find(list, (char*)s, wlen);
  1422. if (alpn != NULL) {
  1423. WOLFSSL_MSG("ALPN protocol match");
  1424. sel = s,
  1425. sel_len = wlen;
  1426. break;
  1427. }
  1428. }
  1429. if (sel == NULL) {
  1430. WOLFSSL_MSG("No ALPN protocol match");
  1431. /* do nothing if no protocol match between client and server and option
  1432. is set to continue (like OpenSSL) */
  1433. if (list->options & WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) {
  1434. WOLFSSL_MSG("Continue on mismatch");
  1435. }
  1436. else {
  1437. SendAlert(ssl, alert_fatal, no_application_protocol);
  1438. WOLFSSL_ERROR_VERBOSE(UNKNOWN_ALPN_PROTOCOL_NAME_E);
  1439. return UNKNOWN_ALPN_PROTOCOL_NAME_E;
  1440. }
  1441. }
  1442. *pextension = extension;
  1443. *psel = sel;
  1444. *psel_len = sel_len;
  1445. return 0;
  1446. }
  1447. int ALPN_Select(WOLFSSL *ssl)
  1448. {
  1449. TLSX *extension;
  1450. const byte *sel = NULL;
  1451. byte sel_len = 0;
  1452. int r = 0;
  1453. WOLFSSL_ENTER("ALPN_Select");
  1454. if (ssl->alpn_peer_requested == NULL)
  1455. return 0;
  1456. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
  1457. if (ssl->alpnSelect != NULL && ssl->options.side == WOLFSSL_SERVER_END) {
  1458. r = ssl->alpnSelect(ssl, &sel, &sel_len, ssl->alpn_peer_requested,
  1459. ssl->alpn_peer_requested_length, ssl->alpnSelectArg);
  1460. switch (r) {
  1461. case SSL_TLSEXT_ERR_OK:
  1462. WOLFSSL_MSG("ALPN protocol match");
  1463. break;
  1464. case SSL_TLSEXT_ERR_NOACK:
  1465. WOLFSSL_MSG("ALPN cb no match but not fatal");
  1466. sel = NULL;
  1467. sel_len = 0;
  1468. break;
  1469. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1470. default:
  1471. WOLFSSL_MSG("ALPN cb no match and fatal");
  1472. SendAlert(ssl, alert_fatal, no_application_protocol);
  1473. WOLFSSL_ERROR_VERBOSE(UNKNOWN_ALPN_PROTOCOL_NAME_E);
  1474. return UNKNOWN_ALPN_PROTOCOL_NAME_E;
  1475. }
  1476. }
  1477. else
  1478. #endif
  1479. {
  1480. r = ALPN_find_match(ssl, &extension, &sel, &sel_len,
  1481. ssl->alpn_peer_requested,
  1482. ssl->alpn_peer_requested_length);
  1483. if (r != 0)
  1484. return r;
  1485. }
  1486. if (sel != NULL) {
  1487. /* set the matching negotiated protocol */
  1488. r = TLSX_SetALPN(&ssl->extensions, sel, sel_len, ssl->heap);
  1489. if (r != WOLFSSL_SUCCESS) {
  1490. WOLFSSL_MSG("TLSX_SetALPN failed");
  1491. return BUFFER_ERROR;
  1492. }
  1493. /* reply to ALPN extension sent from peer */
  1494. #ifndef NO_WOLFSSL_SERVER
  1495. TLSX_SetResponse(ssl, TLSX_APPLICATION_LAYER_PROTOCOL);
  1496. #endif
  1497. }
  1498. return 0;
  1499. }
  1500. /** Parses a buffer of ALPN extensions and set the first one matching
  1501. * client and server requirements */
  1502. static int TLSX_ALPN_ParseAndSet(WOLFSSL *ssl, const byte *input, word16 length,
  1503. byte isRequest)
  1504. {
  1505. word16 size = 0, offset = 0, wlen;
  1506. int r = BUFFER_ERROR;
  1507. const byte *s;
  1508. if (OPAQUE16_LEN > length)
  1509. return BUFFER_ERROR;
  1510. ato16(input, &size);
  1511. offset += OPAQUE16_LEN;
  1512. /* validating alpn list length */
  1513. if (size == 0 || length != OPAQUE16_LEN + size)
  1514. return BUFFER_ERROR;
  1515. /* validating length of entries before accepting */
  1516. for (s = input + offset; (s - input) < size; s += wlen) {
  1517. wlen = *s++;
  1518. if (wlen == 0 || (s + wlen - input) > length)
  1519. return BUFFER_ERROR;
  1520. }
  1521. if (isRequest) {
  1522. /* keep the list sent by peer, if this is from a request. We
  1523. * use it later in ALPN_Select() for evaluation. */
  1524. if (ssl->alpn_peer_requested != NULL) {
  1525. XFREE(ssl->alpn_peer_requested, ssl->heap, DYNAMIC_TYPE_ALPN);
  1526. ssl->alpn_peer_requested_length = 0;
  1527. }
  1528. ssl->alpn_peer_requested = (byte *)XMALLOC(size, ssl->heap,
  1529. DYNAMIC_TYPE_ALPN);
  1530. if (ssl->alpn_peer_requested == NULL) {
  1531. return MEMORY_ERROR;
  1532. }
  1533. ssl->alpn_peer_requested_length = size;
  1534. XMEMCPY(ssl->alpn_peer_requested, (char*)input + offset, size);
  1535. }
  1536. else {
  1537. /* a response, we should find the value in our config */
  1538. const byte *sel = NULL;
  1539. byte sel_len = 0;
  1540. TLSX *extension = NULL;
  1541. r = ALPN_find_match(ssl, &extension, &sel, &sel_len, input + offset, size);
  1542. if (r != 0)
  1543. return r;
  1544. if (sel != NULL) {
  1545. /* set the matching negotiated protocol */
  1546. r = TLSX_SetALPN(&ssl->extensions, sel, sel_len, ssl->heap);
  1547. if (r != WOLFSSL_SUCCESS) {
  1548. WOLFSSL_MSG("TLSX_SetALPN failed");
  1549. return BUFFER_ERROR;
  1550. }
  1551. }
  1552. /* If we had nothing configured, the response is unexpected */
  1553. else if (extension == NULL) {
  1554. r = TLSX_HandleUnsupportedExtension(ssl);
  1555. if (r != 0)
  1556. return r;
  1557. }
  1558. }
  1559. return 0;
  1560. }
  1561. /** Add a protocol name to the list of accepted usable ones */
  1562. int TLSX_UseALPN(TLSX** extensions, const void* data, word16 size, byte options,
  1563. void* heap)
  1564. {
  1565. ALPN *alpn;
  1566. TLSX *extension;
  1567. int ret;
  1568. if (extensions == NULL || data == NULL)
  1569. return BAD_FUNC_ARG;
  1570. alpn = TLSX_ALPN_New((char *)data, size, heap);
  1571. if (alpn == NULL) {
  1572. WOLFSSL_MSG("Memory failure");
  1573. return MEMORY_E;
  1574. }
  1575. /* Set Options of ALPN */
  1576. alpn->options = options;
  1577. extension = TLSX_Find(*extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1578. if (extension == NULL) {
  1579. ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL,
  1580. (void*)alpn, heap);
  1581. if (ret != 0) {
  1582. TLSX_ALPN_Free(alpn, heap);
  1583. return ret;
  1584. }
  1585. }
  1586. else {
  1587. /* push new ALPN object to extension data. */
  1588. alpn->next = (ALPN*)extension->data;
  1589. extension->data = (void*)alpn;
  1590. }
  1591. return WOLFSSL_SUCCESS;
  1592. }
  1593. /** Get the protocol name set by the server */
  1594. int TLSX_ALPN_GetRequest(TLSX* extensions, void** data, word16 *dataSz)
  1595. {
  1596. TLSX *extension;
  1597. ALPN *alpn;
  1598. if (extensions == NULL || data == NULL || dataSz == NULL)
  1599. return BAD_FUNC_ARG;
  1600. *data = NULL;
  1601. *dataSz = 0;
  1602. extension = TLSX_Find(extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1603. if (extension == NULL) {
  1604. WOLFSSL_MSG("TLS extension not found");
  1605. WOLFSSL_ERROR_VERBOSE(WOLFSSL_ALPN_NOT_FOUND);
  1606. return WOLFSSL_ALPN_NOT_FOUND;
  1607. }
  1608. alpn = (ALPN *)extension->data;
  1609. if (alpn == NULL) {
  1610. WOLFSSL_MSG("ALPN extension not found");
  1611. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1612. return WOLFSSL_FATAL_ERROR;
  1613. }
  1614. if (alpn->negotiated != 1) {
  1615. /* consider as an error */
  1616. if (alpn->options & WOLFSSL_ALPN_FAILED_ON_MISMATCH) {
  1617. WOLFSSL_MSG("No protocol match with peer -> Failed");
  1618. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1619. return WOLFSSL_FATAL_ERROR;
  1620. }
  1621. /* continue without negotiated protocol */
  1622. WOLFSSL_MSG("No protocol match with peer -> Continue");
  1623. WOLFSSL_ERROR_VERBOSE(WOLFSSL_ALPN_NOT_FOUND);
  1624. return WOLFSSL_ALPN_NOT_FOUND;
  1625. }
  1626. if (alpn->next != NULL) {
  1627. WOLFSSL_MSG("Only one protocol name must be accepted");
  1628. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1629. return WOLFSSL_FATAL_ERROR;
  1630. }
  1631. *data = alpn->protocol_name;
  1632. *dataSz = (word16)XSTRLEN((char*)*data);
  1633. return WOLFSSL_SUCCESS;
  1634. }
  1635. #define ALPN_FREE_ALL TLSX_ALPN_FreeAll
  1636. #define ALPN_GET_SIZE TLSX_ALPN_GetSize
  1637. #define ALPN_WRITE TLSX_ALPN_Write
  1638. #define ALPN_PARSE TLSX_ALPN_ParseAndSet
  1639. #else /* HAVE_ALPN */
  1640. #define ALPN_FREE_ALL(list, heap) WC_DO_NOTHING
  1641. #define ALPN_GET_SIZE(list) 0
  1642. #define ALPN_WRITE(a, b) 0
  1643. #define ALPN_PARSE(a, b, c, d) 0
  1644. #endif /* HAVE_ALPN */
  1645. /******************************************************************************/
  1646. /* Server Name Indication */
  1647. /******************************************************************************/
  1648. #ifdef HAVE_SNI
  1649. /** Creates a new SNI object. */
  1650. static SNI* TLSX_SNI_New(byte type, const void* data, word16 size, void* heap)
  1651. {
  1652. SNI* sni = (SNI*)XMALLOC(sizeof(SNI), heap, DYNAMIC_TYPE_TLSX);
  1653. (void)heap;
  1654. if (sni) {
  1655. sni->type = type;
  1656. sni->next = NULL;
  1657. #ifndef NO_WOLFSSL_SERVER
  1658. sni->options = 0;
  1659. sni->status = WOLFSSL_SNI_NO_MATCH;
  1660. #endif
  1661. switch (sni->type) {
  1662. case WOLFSSL_SNI_HOST_NAME:
  1663. sni->data.host_name = (char*)XMALLOC(size + 1, heap,
  1664. DYNAMIC_TYPE_TLSX);
  1665. if (sni->data.host_name) {
  1666. XSTRNCPY(sni->data.host_name, (const char*)data, size);
  1667. sni->data.host_name[size] = '\0';
  1668. } else {
  1669. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1670. sni = NULL;
  1671. }
  1672. break;
  1673. default: /* invalid type */
  1674. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1675. sni = NULL;
  1676. }
  1677. }
  1678. return sni;
  1679. }
  1680. /** Releases a SNI object. */
  1681. static void TLSX_SNI_Free(SNI* sni, void* heap)
  1682. {
  1683. if (sni) {
  1684. switch (sni->type) {
  1685. case WOLFSSL_SNI_HOST_NAME:
  1686. XFREE(sni->data.host_name, heap, DYNAMIC_TYPE_TLSX);
  1687. break;
  1688. }
  1689. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1690. }
  1691. (void)heap;
  1692. }
  1693. /** Releases all SNI objects in the provided list. */
  1694. static void TLSX_SNI_FreeAll(SNI* list, void* heap)
  1695. {
  1696. SNI* sni;
  1697. while ((sni = list)) {
  1698. list = sni->next;
  1699. TLSX_SNI_Free(sni, heap);
  1700. }
  1701. }
  1702. /** Tells the buffered size of the SNI objects in a list. */
  1703. static word16 TLSX_SNI_GetSize(SNI* list)
  1704. {
  1705. SNI* sni;
  1706. word16 length = OPAQUE16_LEN; /* list length */
  1707. while ((sni = list)) {
  1708. list = sni->next;
  1709. length += ENUM_LEN + OPAQUE16_LEN; /* sni type + sni length */
  1710. switch (sni->type) {
  1711. case WOLFSSL_SNI_HOST_NAME:
  1712. length += (word16)XSTRLEN((char*)sni->data.host_name);
  1713. break;
  1714. }
  1715. }
  1716. return length;
  1717. }
  1718. /** Writes the SNI objects of a list in a buffer. */
  1719. static word16 TLSX_SNI_Write(SNI* list, byte* output)
  1720. {
  1721. SNI* sni;
  1722. word16 length = 0;
  1723. word16 offset = OPAQUE16_LEN; /* list length offset */
  1724. while ((sni = list)) {
  1725. list = sni->next;
  1726. output[offset++] = sni->type; /* sni type */
  1727. switch (sni->type) {
  1728. case WOLFSSL_SNI_HOST_NAME:
  1729. length = (word16)XSTRLEN((char*)sni->data.host_name);
  1730. c16toa(length, output + offset); /* sni length */
  1731. offset += OPAQUE16_LEN;
  1732. XMEMCPY(output + offset, sni->data.host_name, length);
  1733. offset += length;
  1734. break;
  1735. }
  1736. }
  1737. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  1738. return offset;
  1739. }
  1740. /** Finds a SNI object in the provided list. */
  1741. static SNI* TLSX_SNI_Find(SNI *list, byte type)
  1742. {
  1743. SNI* sni = list;
  1744. while (sni && sni->type != type)
  1745. sni = sni->next;
  1746. return sni;
  1747. }
  1748. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  1749. /** Sets the status of a SNI object. */
  1750. static void TLSX_SNI_SetStatus(TLSX* extensions, byte type, byte status)
  1751. {
  1752. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1753. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1754. if (sni)
  1755. sni->status = status;
  1756. }
  1757. #endif
  1758. /** Gets the status of a SNI object. */
  1759. byte TLSX_SNI_Status(TLSX* extensions, byte type)
  1760. {
  1761. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1762. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1763. if (sni)
  1764. return sni->status;
  1765. return 0;
  1766. }
  1767. /** Parses a buffer of SNI extensions. */
  1768. static int TLSX_SNI_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  1769. byte isRequest)
  1770. {
  1771. #ifndef NO_WOLFSSL_SERVER
  1772. word16 size = 0;
  1773. word16 offset = 0;
  1774. int cacheOnly = 0;
  1775. SNI *sni = NULL;
  1776. byte type;
  1777. byte matched;
  1778. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  1779. WOLFSSL_ECH* ech = NULL;
  1780. WOLFSSL_EchConfig* workingConfig;
  1781. TLSX* echX;
  1782. #endif
  1783. #endif /* !NO_WOLFSSL_SERVER */
  1784. TLSX *extension = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  1785. if (!extension)
  1786. extension = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  1787. if (!isRequest) {
  1788. #ifndef NO_WOLFSSL_CLIENT
  1789. if (!extension || !extension->data)
  1790. return TLSX_HandleUnsupportedExtension(ssl);
  1791. if (length > 0)
  1792. return BUFFER_ERROR; /* SNI response MUST be empty. */
  1793. /* This call enables wolfSSL_SNI_GetRequest() to be called in the
  1794. * client side to fetch the used SNI. It will only work if the SNI
  1795. * was set at the SSL object level. Right now we only support one
  1796. * name type, WOLFSSL_SNI_HOST_NAME, but in the future, the
  1797. * inclusion of other name types will turn this method inaccurate,
  1798. * as the extension response doesn't contains information of which
  1799. * name was accepted.
  1800. */
  1801. TLSX_SNI_SetStatus(ssl->extensions, WOLFSSL_SNI_HOST_NAME,
  1802. WOLFSSL_SNI_REAL_MATCH);
  1803. return 0;
  1804. #endif
  1805. }
  1806. #ifndef NO_WOLFSSL_SERVER
  1807. if (!extension || !extension->data) {
  1808. /* This will keep SNI even though TLSX_UseSNI has not been called.
  1809. * Enable it so that the received sni is available to functions
  1810. * that use a custom callback when SNI is received.
  1811. */
  1812. #ifdef WOLFSSL_ALWAYS_KEEP_SNI
  1813. cacheOnly = 1;
  1814. #endif
  1815. if (ssl->ctx->sniRecvCb) {
  1816. cacheOnly = 1;
  1817. }
  1818. if (cacheOnly) {
  1819. WOLFSSL_MSG("Forcing SSL object to store SNI parameter");
  1820. }
  1821. else {
  1822. /* Skipping, SNI not enabled at server side. */
  1823. return 0;
  1824. }
  1825. }
  1826. if (OPAQUE16_LEN > length)
  1827. return BUFFER_ERROR;
  1828. ato16(input, &size);
  1829. offset += OPAQUE16_LEN;
  1830. /* validating sni list length */
  1831. if (length != OPAQUE16_LEN + size || size == 0)
  1832. return BUFFER_ERROR;
  1833. /* SNI was badly specified and only one type is now recognized and allowed.
  1834. * Only one SNI value per type (RFC6066), so, no loop. */
  1835. type = input[offset++];
  1836. if (type != WOLFSSL_SNI_HOST_NAME)
  1837. return BUFFER_ERROR;
  1838. if (offset + OPAQUE16_LEN > length)
  1839. return BUFFER_ERROR;
  1840. ato16(input + offset, &size);
  1841. offset += OPAQUE16_LEN;
  1842. if (offset + size != length || size == 0)
  1843. return BUFFER_ERROR;
  1844. if (!cacheOnly && !(sni = TLSX_SNI_Find((SNI*)extension->data, type)))
  1845. return 0; /* not using this type of SNI. */
  1846. #ifdef WOLFSSL_TLS13
  1847. /* Don't process the second ClientHello SNI extension if there
  1848. * was problems with the first.
  1849. */
  1850. if (!cacheOnly && sni->status != 0)
  1851. return 0;
  1852. #endif
  1853. matched = cacheOnly || (XSTRLEN(sni->data.host_name) == size &&
  1854. XSTRNCMP(sni->data.host_name, (const char*)input + offset, size) == 0);
  1855. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  1856. echX = TLSX_Find(ssl->extensions, TLSX_ECH);
  1857. if (echX != NULL)
  1858. ech = (WOLFSSL_ECH*)(echX->data);
  1859. if (!matched && ech != NULL) {
  1860. workingConfig = ech->echConfig;
  1861. while (workingConfig != NULL) {
  1862. matched = XSTRLEN(workingConfig->publicName) == size &&
  1863. XSTRNCMP(workingConfig->publicName,
  1864. (const char*)input + offset, size) == 0;
  1865. if (matched)
  1866. break;
  1867. workingConfig = workingConfig->next;
  1868. }
  1869. }
  1870. #endif
  1871. if (matched || sni->options & WOLFSSL_SNI_ANSWER_ON_MISMATCH) {
  1872. int matchStat;
  1873. int r = TLSX_UseSNI(&ssl->extensions, type, input + offset, size,
  1874. ssl->heap);
  1875. if (r != WOLFSSL_SUCCESS)
  1876. return r; /* throws error. */
  1877. if (cacheOnly) {
  1878. WOLFSSL_MSG("Forcing storage of SNI, Fake match");
  1879. matchStat = WOLFSSL_SNI_FORCE_KEEP;
  1880. }
  1881. else if (matched) {
  1882. WOLFSSL_MSG("SNI did match!");
  1883. matchStat = WOLFSSL_SNI_REAL_MATCH;
  1884. }
  1885. else {
  1886. WOLFSSL_MSG("fake SNI match from ANSWER_ON_MISMATCH");
  1887. matchStat = WOLFSSL_SNI_FAKE_MATCH;
  1888. }
  1889. TLSX_SNI_SetStatus(ssl->extensions, type, (byte)matchStat);
  1890. if (!cacheOnly)
  1891. TLSX_SetResponse(ssl, TLSX_SERVER_NAME);
  1892. }
  1893. else if (!(sni->options & WOLFSSL_SNI_CONTINUE_ON_MISMATCH)) {
  1894. SendAlert(ssl, alert_fatal, unrecognized_name);
  1895. WOLFSSL_ERROR_VERBOSE(UNKNOWN_SNI_HOST_NAME_E);
  1896. return UNKNOWN_SNI_HOST_NAME_E;
  1897. }
  1898. #else
  1899. (void)input;
  1900. #endif /* !NO_WOLFSSL_SERVER */
  1901. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1902. (void)length;
  1903. #endif
  1904. return 0;
  1905. }
  1906. static int TLSX_SNI_VerifyParse(WOLFSSL* ssl, byte isRequest)
  1907. {
  1908. (void)ssl;
  1909. if (isRequest) {
  1910. #ifndef NO_WOLFSSL_SERVER
  1911. TLSX* ctx_ext = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  1912. TLSX* ssl_ext = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  1913. SNI* ctx_sni = ctx_ext ? (SNI*)ctx_ext->data : NULL;
  1914. SNI* ssl_sni = ssl_ext ? (SNI*)ssl_ext->data : NULL;
  1915. SNI* sni = NULL;
  1916. for (; ctx_sni; ctx_sni = ctx_sni->next) {
  1917. if (ctx_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
  1918. sni = TLSX_SNI_Find(ssl_sni, ctx_sni->type);
  1919. if (sni) {
  1920. if (sni->status != WOLFSSL_SNI_NO_MATCH)
  1921. continue;
  1922. /* if ssl level overrides ctx level, it is ok. */
  1923. if ((sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) == 0)
  1924. continue;
  1925. }
  1926. SendAlert(ssl, alert_fatal, handshake_failure);
  1927. WOLFSSL_ERROR_VERBOSE(SNI_ABSENT_ERROR);
  1928. return SNI_ABSENT_ERROR;
  1929. }
  1930. }
  1931. for (; ssl_sni; ssl_sni = ssl_sni->next) {
  1932. if (ssl_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
  1933. if (ssl_sni->status != WOLFSSL_SNI_NO_MATCH)
  1934. continue;
  1935. SendAlert(ssl, alert_fatal, handshake_failure);
  1936. WOLFSSL_ERROR_VERBOSE(SNI_ABSENT_ERROR);
  1937. return SNI_ABSENT_ERROR;
  1938. }
  1939. }
  1940. #endif /* NO_WOLFSSL_SERVER */
  1941. }
  1942. return 0;
  1943. }
  1944. int TLSX_UseSNI(TLSX** extensions, byte type, const void* data, word16 size,
  1945. void* heap)
  1946. {
  1947. TLSX* extension;
  1948. SNI* sni = NULL;
  1949. if (extensions == NULL || data == NULL)
  1950. return BAD_FUNC_ARG;
  1951. if ((sni = TLSX_SNI_New(type, data, size, heap)) == NULL)
  1952. return MEMORY_E;
  1953. extension = TLSX_Find(*extensions, TLSX_SERVER_NAME);
  1954. if (!extension) {
  1955. int ret = TLSX_Push(extensions, TLSX_SERVER_NAME, (void*)sni, heap);
  1956. if (ret != 0) {
  1957. TLSX_SNI_Free(sni, heap);
  1958. return ret;
  1959. }
  1960. }
  1961. else {
  1962. /* push new SNI object to extension data. */
  1963. sni->next = (SNI*)extension->data;
  1964. extension->data = (void*)sni;
  1965. /* remove duplicate SNI, there should be only one of each type. */
  1966. do {
  1967. if (sni->next && sni->next->type == type) {
  1968. SNI* next = sni->next;
  1969. sni->next = next->next;
  1970. TLSX_SNI_Free(next, heap);
  1971. /* there is no way to occur more than
  1972. * two SNIs of the same type.
  1973. */
  1974. break;
  1975. }
  1976. } while ((sni = sni->next));
  1977. }
  1978. return WOLFSSL_SUCCESS;
  1979. }
  1980. #ifndef NO_WOLFSSL_SERVER
  1981. /** Tells the SNI requested by the client. */
  1982. word16 TLSX_SNI_GetRequest(TLSX* extensions, byte type, void** data)
  1983. {
  1984. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1985. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1986. if (sni && sni->status != WOLFSSL_SNI_NO_MATCH) {
  1987. switch (sni->type) {
  1988. case WOLFSSL_SNI_HOST_NAME:
  1989. if (data) {
  1990. *data = sni->data.host_name;
  1991. return (word16)XSTRLEN((char*)*data);
  1992. }
  1993. }
  1994. }
  1995. return 0;
  1996. }
  1997. /** Sets the options for a SNI object. */
  1998. void TLSX_SNI_SetOptions(TLSX* extensions, byte type, byte options)
  1999. {
  2000. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  2001. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  2002. if (sni)
  2003. sni->options = options;
  2004. }
  2005. /** Retrieves a SNI request from a client hello buffer. */
  2006. int TLSX_SNI_GetFromBuffer(const byte* clientHello, word32 helloSz,
  2007. byte type, byte* sni, word32* inOutSz)
  2008. {
  2009. word32 offset = 0;
  2010. word32 len32 = 0;
  2011. word16 len16 = 0;
  2012. if (helloSz < RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + CLIENT_HELLO_FIRST)
  2013. return INCOMPLETE_DATA;
  2014. /* TLS record header */
  2015. if ((enum ContentType) clientHello[offset++] != handshake) {
  2016. /* checking for SSLv2.0 client hello according to: */
  2017. /* http://tools.ietf.org/html/rfc4346#appendix-E.1 */
  2018. if ((enum HandShakeType) clientHello[++offset] == client_hello) {
  2019. offset += ENUM_LEN + VERSION_SZ; /* skip version */
  2020. ato16(clientHello + offset, &len16);
  2021. offset += OPAQUE16_LEN;
  2022. if (len16 % 3) /* cipher_spec_length must be multiple of 3 */
  2023. return BUFFER_ERROR;
  2024. ato16(clientHello + offset, &len16);
  2025. /* Returning SNI_UNSUPPORTED do not increment offset here */
  2026. if (len16 != 0) /* session_id_length must be 0 */
  2027. return BUFFER_ERROR;
  2028. WOLFSSL_ERROR_VERBOSE(SNI_UNSUPPORTED);
  2029. return SNI_UNSUPPORTED;
  2030. }
  2031. return BUFFER_ERROR;
  2032. }
  2033. if (clientHello[offset++] != SSLv3_MAJOR)
  2034. return BUFFER_ERROR;
  2035. if (clientHello[offset++] < TLSv1_MINOR) {
  2036. WOLFSSL_ERROR_VERBOSE(SNI_UNSUPPORTED);
  2037. return SNI_UNSUPPORTED;
  2038. }
  2039. ato16(clientHello + offset, &len16);
  2040. offset += OPAQUE16_LEN;
  2041. if (offset + len16 > helloSz)
  2042. return INCOMPLETE_DATA;
  2043. /* Handshake header */
  2044. if ((enum HandShakeType) clientHello[offset] != client_hello)
  2045. return BUFFER_ERROR;
  2046. c24to32(clientHello + offset + 1, &len32);
  2047. offset += HANDSHAKE_HEADER_SZ;
  2048. if (offset + len32 > helloSz)
  2049. return BUFFER_ERROR;
  2050. /* client hello */
  2051. offset += VERSION_SZ + RAN_LEN; /* version, random */
  2052. if (helloSz < offset + clientHello[offset])
  2053. return BUFFER_ERROR;
  2054. offset += ENUM_LEN + clientHello[offset]; /* skip session id */
  2055. /* cypher suites */
  2056. if (helloSz < offset + OPAQUE16_LEN)
  2057. return BUFFER_ERROR;
  2058. ato16(clientHello + offset, &len16);
  2059. offset += OPAQUE16_LEN;
  2060. if (helloSz < offset + len16)
  2061. return BUFFER_ERROR;
  2062. offset += len16; /* skip cypher suites */
  2063. /* compression methods */
  2064. if (helloSz < offset + 1)
  2065. return BUFFER_ERROR;
  2066. if (helloSz < offset + clientHello[offset])
  2067. return BUFFER_ERROR;
  2068. offset += ENUM_LEN + clientHello[offset]; /* skip compression methods */
  2069. /* extensions */
  2070. if (helloSz < offset + OPAQUE16_LEN)
  2071. return 0; /* no extensions in client hello. */
  2072. ato16(clientHello + offset, &len16);
  2073. offset += OPAQUE16_LEN;
  2074. if (helloSz < offset + len16)
  2075. return BUFFER_ERROR;
  2076. while (len16 >= OPAQUE16_LEN + OPAQUE16_LEN) {
  2077. word16 extType;
  2078. word16 extLen;
  2079. ato16(clientHello + offset, &extType);
  2080. offset += OPAQUE16_LEN;
  2081. ato16(clientHello + offset, &extLen);
  2082. offset += OPAQUE16_LEN;
  2083. if (helloSz < offset + extLen)
  2084. return BUFFER_ERROR;
  2085. if (extType != TLSX_SERVER_NAME) {
  2086. offset += extLen; /* skip extension */
  2087. } else {
  2088. word16 listLen;
  2089. ato16(clientHello + offset, &listLen);
  2090. offset += OPAQUE16_LEN;
  2091. if (helloSz < offset + listLen)
  2092. return BUFFER_ERROR;
  2093. while (listLen > ENUM_LEN + OPAQUE16_LEN) {
  2094. byte sniType = clientHello[offset++];
  2095. word16 sniLen;
  2096. ato16(clientHello + offset, &sniLen);
  2097. offset += OPAQUE16_LEN;
  2098. if (helloSz < offset + sniLen)
  2099. return BUFFER_ERROR;
  2100. if (sniType != type) {
  2101. offset += sniLen;
  2102. listLen -= min(ENUM_LEN + OPAQUE16_LEN + sniLen, listLen);
  2103. continue;
  2104. }
  2105. *inOutSz = min(sniLen, *inOutSz);
  2106. XMEMCPY(sni, clientHello + offset, *inOutSz);
  2107. return WOLFSSL_SUCCESS;
  2108. }
  2109. }
  2110. len16 -= min(2 * OPAQUE16_LEN + extLen, len16);
  2111. }
  2112. return len16 ? BUFFER_ERROR : 0;
  2113. }
  2114. #endif
  2115. #define SNI_FREE_ALL TLSX_SNI_FreeAll
  2116. #define SNI_GET_SIZE TLSX_SNI_GetSize
  2117. #define SNI_WRITE TLSX_SNI_Write
  2118. #define SNI_PARSE TLSX_SNI_Parse
  2119. #define SNI_VERIFY_PARSE TLSX_SNI_VerifyParse
  2120. #else
  2121. #define SNI_FREE_ALL(list, heap) WC_DO_NOTHING
  2122. #define SNI_GET_SIZE(list) 0
  2123. #define SNI_WRITE(a, b) 0
  2124. #define SNI_PARSE(a, b, c, d) 0
  2125. #define SNI_VERIFY_PARSE(a, b) 0
  2126. #endif /* HAVE_SNI */
  2127. /******************************************************************************/
  2128. /* Trusted CA Key Indication */
  2129. /******************************************************************************/
  2130. #ifdef HAVE_TRUSTED_CA
  2131. /** Creates a new TCA object. */
  2132. static TCA* TLSX_TCA_New(byte type, const byte* id, word16 idSz, void* heap)
  2133. {
  2134. TCA* tca = (TCA*)XMALLOC(sizeof(TCA), heap, DYNAMIC_TYPE_TLSX);
  2135. if (tca) {
  2136. XMEMSET(tca, 0, sizeof(TCA));
  2137. tca->type = type;
  2138. switch (type) {
  2139. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2140. break;
  2141. #ifndef NO_SHA
  2142. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2143. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2144. if (idSz == WC_SHA_DIGEST_SIZE &&
  2145. (tca->id =
  2146. (byte*)XMALLOC(idSz, heap, DYNAMIC_TYPE_TLSX))) {
  2147. XMEMCPY(tca->id, id, idSz);
  2148. tca->idSz = idSz;
  2149. }
  2150. else {
  2151. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2152. tca = NULL;
  2153. }
  2154. break;
  2155. #endif
  2156. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2157. if (idSz > 0 &&
  2158. (tca->id =
  2159. (byte*)XMALLOC(idSz, heap, DYNAMIC_TYPE_TLSX))) {
  2160. XMEMCPY(tca->id, id, idSz);
  2161. tca->idSz = idSz;
  2162. }
  2163. else {
  2164. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2165. tca = NULL;
  2166. }
  2167. break;
  2168. default: /* invalid type */
  2169. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2170. tca = NULL;
  2171. }
  2172. }
  2173. (void)heap;
  2174. return tca;
  2175. }
  2176. /** Releases a TCA object. */
  2177. static void TLSX_TCA_Free(TCA* tca, void* heap)
  2178. {
  2179. (void)heap;
  2180. if (tca) {
  2181. if (tca->id)
  2182. XFREE(tca->id, heap, DYNAMIC_TYPE_TLSX);
  2183. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2184. }
  2185. }
  2186. /** Releases all TCA objects in the provided list. */
  2187. static void TLSX_TCA_FreeAll(TCA* list, void* heap)
  2188. {
  2189. TCA* tca;
  2190. while ((tca = list)) {
  2191. list = tca->next;
  2192. TLSX_TCA_Free(tca, heap);
  2193. }
  2194. }
  2195. /** Tells the buffered size of the TCA objects in a list. */
  2196. static word16 TLSX_TCA_GetSize(TCA* list)
  2197. {
  2198. TCA* tca;
  2199. word16 length = OPAQUE16_LEN; /* list length */
  2200. while ((tca = list)) {
  2201. list = tca->next;
  2202. length += ENUM_LEN; /* tca type */
  2203. switch (tca->type) {
  2204. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2205. break;
  2206. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2207. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2208. length += tca->idSz;
  2209. break;
  2210. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2211. length += OPAQUE16_LEN + tca->idSz;
  2212. break;
  2213. }
  2214. }
  2215. return length;
  2216. }
  2217. /** Writes the TCA objects of a list in a buffer. */
  2218. static word16 TLSX_TCA_Write(TCA* list, byte* output)
  2219. {
  2220. TCA* tca;
  2221. word16 offset = OPAQUE16_LEN; /* list length offset */
  2222. while ((tca = list)) {
  2223. list = tca->next;
  2224. output[offset++] = tca->type; /* tca type */
  2225. switch (tca->type) {
  2226. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2227. break;
  2228. #ifndef NO_SHA
  2229. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2230. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2231. if (tca->id != NULL) {
  2232. XMEMCPY(output + offset, tca->id, tca->idSz);
  2233. offset += tca->idSz;
  2234. }
  2235. else {
  2236. /* ID missing. Set to an empty string. */
  2237. c16toa(0, output + offset);
  2238. offset += OPAQUE16_LEN;
  2239. }
  2240. break;
  2241. #endif
  2242. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2243. if (tca->id != NULL) {
  2244. c16toa(tca->idSz, output + offset); /* tca length */
  2245. offset += OPAQUE16_LEN;
  2246. XMEMCPY(output + offset, tca->id, tca->idSz);
  2247. offset += tca->idSz;
  2248. }
  2249. else {
  2250. /* ID missing. Set to an empty string. */
  2251. c16toa(0, output + offset);
  2252. offset += OPAQUE16_LEN;
  2253. }
  2254. break;
  2255. default:
  2256. /* ID unknown. Set to an empty string. */
  2257. c16toa(0, output + offset);
  2258. offset += OPAQUE16_LEN;
  2259. }
  2260. }
  2261. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  2262. return offset;
  2263. }
  2264. #ifndef NO_WOLFSSL_SERVER
  2265. static TCA* TLSX_TCA_Find(TCA *list, byte type, const byte* id, word16 idSz)
  2266. {
  2267. TCA* tca = list;
  2268. while (tca && tca->type != type && type != WOLFSSL_TRUSTED_CA_PRE_AGREED &&
  2269. idSz != tca->idSz && !XMEMCMP(id, tca->id, idSz))
  2270. tca = tca->next;
  2271. return tca;
  2272. }
  2273. #endif /* NO_WOLFSSL_SERVER */
  2274. /** Parses a buffer of TCA extensions. */
  2275. static int TLSX_TCA_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2276. byte isRequest)
  2277. {
  2278. #ifndef NO_WOLFSSL_SERVER
  2279. word16 size = 0;
  2280. word16 offset = 0;
  2281. #endif
  2282. TLSX *extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
  2283. if (!extension)
  2284. extension = TLSX_Find(ssl->ctx->extensions, TLSX_TRUSTED_CA_KEYS);
  2285. if (!isRequest) {
  2286. #ifndef NO_WOLFSSL_CLIENT
  2287. if (!extension || !extension->data)
  2288. return TLSX_HandleUnsupportedExtension(ssl);
  2289. if (length > 0)
  2290. return BUFFER_ERROR; /* TCA response MUST be empty. */
  2291. /* Set the flag that we're good for keys */
  2292. TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
  2293. return 0;
  2294. #endif
  2295. }
  2296. #ifndef NO_WOLFSSL_SERVER
  2297. if (!extension || !extension->data) {
  2298. /* Skipping, TCA not enabled at server side. */
  2299. return 0;
  2300. }
  2301. if (OPAQUE16_LEN > length)
  2302. return BUFFER_ERROR;
  2303. ato16(input, &size);
  2304. offset += OPAQUE16_LEN;
  2305. /* validating tca list length */
  2306. if (length != OPAQUE16_LEN + size)
  2307. return BUFFER_ERROR;
  2308. for (size = 0; offset < length; offset += size) {
  2309. TCA *tca = NULL;
  2310. byte type;
  2311. const byte* id = NULL;
  2312. word16 idSz = 0;
  2313. if (offset + ENUM_LEN > length)
  2314. return BUFFER_ERROR;
  2315. type = input[offset++];
  2316. switch (type) {
  2317. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2318. break;
  2319. #ifndef NO_SHA
  2320. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2321. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2322. if (offset + WC_SHA_DIGEST_SIZE > length)
  2323. return BUFFER_ERROR;
  2324. idSz = WC_SHA_DIGEST_SIZE;
  2325. id = input + offset;
  2326. offset += idSz;
  2327. break;
  2328. #endif
  2329. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2330. if (offset + OPAQUE16_LEN > length)
  2331. return BUFFER_ERROR;
  2332. ato16(input + offset, &idSz);
  2333. offset += OPAQUE16_LEN;
  2334. if ((offset > length) || (idSz > length - offset))
  2335. return BUFFER_ERROR;
  2336. id = input + offset;
  2337. offset += idSz;
  2338. break;
  2339. default:
  2340. WOLFSSL_ERROR_VERBOSE(TCA_INVALID_ID_TYPE);
  2341. return TCA_INVALID_ID_TYPE;
  2342. }
  2343. /* Find the type/ID in the TCA list. */
  2344. tca = TLSX_TCA_Find((TCA*)extension->data, type, id, idSz);
  2345. if (tca != NULL) {
  2346. /* Found it. Set the response flag and break out of the loop. */
  2347. TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
  2348. break;
  2349. }
  2350. }
  2351. #else
  2352. (void)input;
  2353. #endif
  2354. return 0;
  2355. }
  2356. /* Checks to see if the server sent a response for the TCA. */
  2357. static int TLSX_TCA_VerifyParse(WOLFSSL* ssl, byte isRequest)
  2358. {
  2359. (void)ssl;
  2360. if (!isRequest) {
  2361. #ifndef NO_WOLFSSL_CLIENT
  2362. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
  2363. if (extension && !extension->resp) {
  2364. SendAlert(ssl, alert_fatal, handshake_failure);
  2365. WOLFSSL_ERROR_VERBOSE(TCA_ABSENT_ERROR);
  2366. return TCA_ABSENT_ERROR;
  2367. }
  2368. #endif /* NO_WOLFSSL_CLIENT */
  2369. }
  2370. return 0;
  2371. }
  2372. int TLSX_UseTrustedCA(TLSX** extensions, byte type,
  2373. const byte* id, word16 idSz, void* heap)
  2374. {
  2375. TLSX* extension;
  2376. TCA* tca = NULL;
  2377. if (extensions == NULL)
  2378. return BAD_FUNC_ARG;
  2379. if ((tca = TLSX_TCA_New(type, id, idSz, heap)) == NULL)
  2380. return MEMORY_E;
  2381. extension = TLSX_Find(*extensions, TLSX_TRUSTED_CA_KEYS);
  2382. if (!extension) {
  2383. int ret = TLSX_Push(extensions, TLSX_TRUSTED_CA_KEYS, (void*)tca, heap);
  2384. if (ret != 0) {
  2385. TLSX_TCA_Free(tca, heap);
  2386. return ret;
  2387. }
  2388. }
  2389. else {
  2390. /* push new TCA object to extension data. */
  2391. tca->next = (TCA*)extension->data;
  2392. extension->data = (void*)tca;
  2393. }
  2394. return WOLFSSL_SUCCESS;
  2395. }
  2396. #define TCA_FREE_ALL TLSX_TCA_FreeAll
  2397. #define TCA_GET_SIZE TLSX_TCA_GetSize
  2398. #define TCA_WRITE TLSX_TCA_Write
  2399. #define TCA_PARSE TLSX_TCA_Parse
  2400. #define TCA_VERIFY_PARSE TLSX_TCA_VerifyParse
  2401. #else /* HAVE_TRUSTED_CA */
  2402. #define TCA_FREE_ALL(list, heap) WC_DO_NOTHING
  2403. #define TCA_GET_SIZE(list) 0
  2404. #define TCA_WRITE(a, b) 0
  2405. #define TCA_PARSE(a, b, c, d) 0
  2406. #define TCA_VERIFY_PARSE(a, b) 0
  2407. #endif /* HAVE_TRUSTED_CA */
  2408. /******************************************************************************/
  2409. /* Max Fragment Length Negotiation */
  2410. /******************************************************************************/
  2411. #ifdef HAVE_MAX_FRAGMENT
  2412. static word16 TLSX_MFL_Write(byte* data, byte* output)
  2413. {
  2414. output[0] = data[0];
  2415. return ENUM_LEN;
  2416. }
  2417. static int TLSX_MFL_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2418. byte isRequest)
  2419. {
  2420. if (length != ENUM_LEN)
  2421. return BUFFER_ERROR;
  2422. #ifdef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
  2423. (void) isRequest;
  2424. #else
  2425. if (!isRequest)
  2426. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_MAX_FRAGMENT_LENGTH))
  2427. return TLSX_HandleUnsupportedExtension(ssl);
  2428. #endif
  2429. switch (*input) {
  2430. case WOLFSSL_MFL_2_8 : ssl->max_fragment = 256; break;
  2431. case WOLFSSL_MFL_2_9 : ssl->max_fragment = 512; break;
  2432. case WOLFSSL_MFL_2_10: ssl->max_fragment = 1024; break;
  2433. case WOLFSSL_MFL_2_11: ssl->max_fragment = 2048; break;
  2434. case WOLFSSL_MFL_2_12: ssl->max_fragment = 4096; break;
  2435. case WOLFSSL_MFL_2_13: ssl->max_fragment = 8192; break;
  2436. default:
  2437. SendAlert(ssl, alert_fatal, illegal_parameter);
  2438. WOLFSSL_ERROR_VERBOSE(UNKNOWN_MAX_FRAG_LEN_E);
  2439. return UNKNOWN_MAX_FRAG_LEN_E;
  2440. }
  2441. #ifndef NO_WOLFSSL_SERVER
  2442. if (isRequest) {
  2443. int ret = TLSX_UseMaxFragment(&ssl->extensions, *input, ssl->heap);
  2444. if (ret != WOLFSSL_SUCCESS)
  2445. return ret; /* throw error */
  2446. TLSX_SetResponse(ssl, TLSX_MAX_FRAGMENT_LENGTH);
  2447. }
  2448. #endif
  2449. return 0;
  2450. }
  2451. int TLSX_UseMaxFragment(TLSX** extensions, byte mfl, void* heap)
  2452. {
  2453. byte* data = NULL;
  2454. int ret = 0;
  2455. if (extensions == NULL || mfl < WOLFSSL_MFL_MIN || mfl > WOLFSSL_MFL_MAX)
  2456. return BAD_FUNC_ARG;
  2457. data = (byte*)XMALLOC(ENUM_LEN, heap, DYNAMIC_TYPE_TLSX);
  2458. if (data == NULL)
  2459. return MEMORY_E;
  2460. data[0] = mfl;
  2461. ret = TLSX_Push(extensions, TLSX_MAX_FRAGMENT_LENGTH, data, heap);
  2462. if (ret != 0) {
  2463. XFREE(data, heap, DYNAMIC_TYPE_TLSX);
  2464. return ret;
  2465. }
  2466. return WOLFSSL_SUCCESS;
  2467. }
  2468. #define MFL_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
  2469. #define MFL_GET_SIZE(data) ENUM_LEN
  2470. #define MFL_WRITE TLSX_MFL_Write
  2471. #define MFL_PARSE TLSX_MFL_Parse
  2472. #else
  2473. #define MFL_FREE_ALL(a, b) WC_DO_NOTHING
  2474. #define MFL_GET_SIZE(a) 0
  2475. #define MFL_WRITE(a, b) 0
  2476. #define MFL_PARSE(a, b, c, d) 0
  2477. #endif /* HAVE_MAX_FRAGMENT */
  2478. /******************************************************************************/
  2479. /* Truncated HMAC */
  2480. /******************************************************************************/
  2481. #ifdef HAVE_TRUNCATED_HMAC
  2482. static int TLSX_THM_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2483. byte isRequest)
  2484. {
  2485. if (length != 0 || input == NULL)
  2486. return BUFFER_ERROR;
  2487. if (!isRequest) {
  2488. #ifndef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
  2489. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_TRUNCATED_HMAC))
  2490. return TLSX_HandleUnsupportedExtension(ssl);
  2491. #endif
  2492. }
  2493. else {
  2494. #ifndef NO_WOLFSSL_SERVER
  2495. int ret = TLSX_UseTruncatedHMAC(&ssl->extensions, ssl->heap);
  2496. if (ret != WOLFSSL_SUCCESS)
  2497. return ret; /* throw error */
  2498. TLSX_SetResponse(ssl, TLSX_TRUNCATED_HMAC);
  2499. #endif
  2500. }
  2501. ssl->truncated_hmac = 1;
  2502. return 0;
  2503. }
  2504. int TLSX_UseTruncatedHMAC(TLSX** extensions, void* heap)
  2505. {
  2506. int ret = 0;
  2507. if (extensions == NULL)
  2508. return BAD_FUNC_ARG;
  2509. ret = TLSX_Push(extensions, TLSX_TRUNCATED_HMAC, NULL, heap);
  2510. if (ret != 0)
  2511. return ret;
  2512. return WOLFSSL_SUCCESS;
  2513. }
  2514. #define THM_PARSE TLSX_THM_Parse
  2515. #else
  2516. #define THM_PARSE(a, b, c, d) 0
  2517. #endif /* HAVE_TRUNCATED_HMAC */
  2518. /******************************************************************************/
  2519. /* Certificate Status Request */
  2520. /******************************************************************************/
  2521. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  2522. static void TLSX_CSR_Free(CertificateStatusRequest* csr, void* heap)
  2523. {
  2524. switch (csr->status_type) {
  2525. case WOLFSSL_CSR_OCSP:
  2526. FreeOcspRequest(&csr->request.ocsp);
  2527. break;
  2528. }
  2529. #ifdef WOLFSSL_TLS13
  2530. if (csr->response.buffer != NULL) {
  2531. XFREE(csr->response.buffer, csr->ssl->heap,
  2532. DYNAMIC_TYPE_TMP_BUFFER);
  2533. }
  2534. #endif
  2535. XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
  2536. (void)heap;
  2537. }
  2538. static word16 TLSX_CSR_GetSize(CertificateStatusRequest* csr, byte isRequest)
  2539. {
  2540. word16 size = 0;
  2541. /* shut up compiler warnings */
  2542. (void) csr; (void) isRequest;
  2543. #ifndef NO_WOLFSSL_CLIENT
  2544. if (isRequest) {
  2545. switch (csr->status_type) {
  2546. case WOLFSSL_CSR_OCSP:
  2547. size += ENUM_LEN + 2 * OPAQUE16_LEN;
  2548. if (csr->request.ocsp.nonceSz)
  2549. size += OCSP_NONCE_EXT_SZ;
  2550. break;
  2551. }
  2552. }
  2553. #endif
  2554. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  2555. if (!isRequest && csr->ssl->options.tls1_3)
  2556. return OPAQUE8_LEN + OPAQUE24_LEN + csr->response.length;
  2557. #endif
  2558. return size;
  2559. }
  2560. static int TLSX_CSR_Write(CertificateStatusRequest* csr, byte* output,
  2561. byte isRequest)
  2562. {
  2563. /* shut up compiler warnings */
  2564. (void) csr; (void) output; (void) isRequest;
  2565. #ifndef NO_WOLFSSL_CLIENT
  2566. if (isRequest) {
  2567. int ret = 0;
  2568. word16 offset = 0;
  2569. word16 length = 0;
  2570. /* type */
  2571. output[offset++] = csr->status_type;
  2572. switch (csr->status_type) {
  2573. case WOLFSSL_CSR_OCSP:
  2574. /* responder id list */
  2575. c16toa(0, output + offset);
  2576. offset += OPAQUE16_LEN;
  2577. /* request extensions */
  2578. if (csr->request.ocsp.nonceSz) {
  2579. ret = (int)EncodeOcspRequestExtensions(&csr->request.ocsp,
  2580. output + offset + OPAQUE16_LEN,
  2581. OCSP_NONCE_EXT_SZ);
  2582. if (ret > 0) {
  2583. length = (word16)ret;
  2584. }
  2585. else {
  2586. return ret;
  2587. }
  2588. }
  2589. c16toa(length, output + offset);
  2590. offset += OPAQUE16_LEN + length;
  2591. break;
  2592. }
  2593. return (int)offset;
  2594. }
  2595. #endif
  2596. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  2597. if (!isRequest && csr->ssl->options.tls1_3) {
  2598. word16 offset = 0;
  2599. output[offset++] = csr->status_type;
  2600. c32to24(csr->response.length, output + offset);
  2601. offset += OPAQUE24_LEN;
  2602. XMEMCPY(output + offset, csr->response.buffer, csr->response.length);
  2603. offset += csr->response.length;
  2604. return offset;
  2605. }
  2606. #endif
  2607. return 0;
  2608. }
  2609. static int TLSX_CSR_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2610. byte isRequest)
  2611. {
  2612. int ret;
  2613. #if !defined(NO_WOLFSSL_SERVER)
  2614. byte status_type;
  2615. word16 size = 0;
  2616. #if defined(WOLFSSL_TLS13)
  2617. DecodedCert* cert;
  2618. #endif
  2619. #endif
  2620. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) \
  2621. && defined(WOLFSSL_TLS13)
  2622. OcspRequest* request;
  2623. TLSX* extension;
  2624. CertificateStatusRequest* csr;
  2625. #endif
  2626. #if !defined(NO_WOLFSSL_CLIENT) && defined(WOLFSSL_TLS13) \
  2627. || !defined(NO_WOLFSSL_SERVER)
  2628. word32 offset = 0;
  2629. #endif
  2630. #if !defined(NO_WOLFSSL_CLIENT) && defined(WOLFSSL_TLS13)
  2631. word32 resp_length = 0;
  2632. #endif
  2633. /* shut up compiler warnings */
  2634. (void) ssl; (void) input;
  2635. if (!isRequest) {
  2636. #ifndef NO_WOLFSSL_CLIENT
  2637. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2638. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2639. if (!csr) {
  2640. /* look at context level */
  2641. extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST);
  2642. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2643. if (!csr) /* unexpected extension */
  2644. return TLSX_HandleUnsupportedExtension(ssl);
  2645. /* enable extension at ssl level */
  2646. ret = TLSX_UseCertificateStatusRequest(&ssl->extensions,
  2647. csr->status_type, csr->options, ssl,
  2648. ssl->heap, ssl->devId);
  2649. if (ret != WOLFSSL_SUCCESS)
  2650. return ret == 0 ? -1 : ret;
  2651. switch (csr->status_type) {
  2652. case WOLFSSL_CSR_OCSP:
  2653. /* propagate nonce */
  2654. if (csr->request.ocsp.nonceSz) {
  2655. request =
  2656. (OcspRequest*)TLSX_CSR_GetRequest(ssl->extensions);
  2657. if (request) {
  2658. XMEMCPY(request->nonce, csr->request.ocsp.nonce,
  2659. csr->request.ocsp.nonceSz);
  2660. request->nonceSz = csr->request.ocsp.nonceSz;
  2661. }
  2662. }
  2663. break;
  2664. }
  2665. }
  2666. ssl->status_request = 1;
  2667. #ifdef WOLFSSL_TLS13
  2668. if (ssl->options.tls1_3) {
  2669. /* Get the new extension potentially created above. */
  2670. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2671. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2672. if (csr == NULL)
  2673. return MEMORY_ERROR;
  2674. ret = 0;
  2675. if (OPAQUE8_LEN + OPAQUE24_LEN > length)
  2676. ret = BUFFER_ERROR;
  2677. if (ret == 0 && input[offset++] != WOLFSSL_CSR_OCSP) {
  2678. ret = BAD_CERTIFICATE_STATUS_ERROR;
  2679. WOLFSSL_ERROR_VERBOSE(ret);
  2680. }
  2681. if (ret == 0) {
  2682. c24to32(input + offset, &resp_length);
  2683. offset += OPAQUE24_LEN;
  2684. if (offset + resp_length != length)
  2685. ret = BUFFER_ERROR;
  2686. }
  2687. if (ret == 0) {
  2688. csr->response.buffer = (byte*)XMALLOC(resp_length, ssl->heap,
  2689. DYNAMIC_TYPE_TMP_BUFFER);
  2690. if (csr->response.buffer == NULL)
  2691. ret = MEMORY_ERROR;
  2692. }
  2693. if (ret == 0) {
  2694. XMEMCPY(csr->response.buffer, input + offset, resp_length);
  2695. csr->response.length = resp_length;
  2696. }
  2697. return ret;
  2698. }
  2699. else
  2700. #endif
  2701. {
  2702. /* extension_data MUST be empty. */
  2703. return length ? BUFFER_ERROR : 0;
  2704. }
  2705. #endif
  2706. }
  2707. else {
  2708. #ifndef NO_WOLFSSL_SERVER
  2709. if (length == 0)
  2710. return 0;
  2711. status_type = input[offset++];
  2712. switch (status_type) {
  2713. case WOLFSSL_CSR_OCSP: {
  2714. /* skip responder_id_list */
  2715. if ((int)(length - offset) < OPAQUE16_LEN)
  2716. return BUFFER_ERROR;
  2717. ato16(input + offset, &size);
  2718. offset += OPAQUE16_LEN + size;
  2719. /* skip request_extensions */
  2720. if ((int)(length - offset) < OPAQUE16_LEN)
  2721. return BUFFER_ERROR;
  2722. ato16(input + offset, &size);
  2723. offset += OPAQUE16_LEN + size;
  2724. if (offset > length)
  2725. return BUFFER_ERROR;
  2726. /* is able to send OCSP response? */
  2727. if (SSL_CM(ssl) == NULL || !SSL_CM(ssl)->ocspStaplingEnabled)
  2728. return 0;
  2729. }
  2730. break;
  2731. /* unknown status type */
  2732. default:
  2733. return 0;
  2734. }
  2735. /* if using status_request and already sending it, skip this one */
  2736. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2737. if (ssl->status_request_v2)
  2738. return 0;
  2739. #endif
  2740. /* accept the first good status_type and return */
  2741. ret = TLSX_UseCertificateStatusRequest(&ssl->extensions, status_type,
  2742. 0, ssl, ssl->heap, ssl->devId);
  2743. if (ret != WOLFSSL_SUCCESS)
  2744. return ret == 0 ? -1 : ret; /* throw error */
  2745. #if defined(WOLFSSL_TLS13)
  2746. if (ssl->options.tls1_3) {
  2747. if (ssl->buffers.certificate == NULL) {
  2748. WOLFSSL_MSG("Certificate buffer not set!");
  2749. return BUFFER_ERROR;
  2750. }
  2751. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), ssl->heap,
  2752. DYNAMIC_TYPE_DCERT);
  2753. if (cert == NULL) {
  2754. return MEMORY_E;
  2755. }
  2756. InitDecodedCert(cert, ssl->buffers.certificate->buffer,
  2757. ssl->buffers.certificate->length, ssl->heap);
  2758. ret = ParseCert(cert, CERT_TYPE, 1, SSL_CM(ssl));
  2759. if (ret != 0) {
  2760. FreeDecodedCert(cert);
  2761. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2762. /* Let's not error out the connection if we can't verify our
  2763. * cert */
  2764. if (ret == ASN_SELF_SIGNED_E || ret == ASN_NO_SIGNER_E)
  2765. ret = 0;
  2766. return ret;
  2767. }
  2768. ret = TLSX_CSR_InitRequest(ssl->extensions, cert, ssl->heap);
  2769. if (ret != 0 ) {
  2770. FreeDecodedCert(cert);
  2771. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2772. return ret;
  2773. }
  2774. FreeDecodedCert(cert);
  2775. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2776. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2777. csr = extension ?
  2778. (CertificateStatusRequest*)extension->data : NULL;
  2779. if (csr == NULL)
  2780. return MEMORY_ERROR;
  2781. request = &csr->request.ocsp;
  2782. ret = CreateOcspResponse(ssl, &request, &csr->response);
  2783. if (ret != 0)
  2784. return ret;
  2785. if (csr->response.buffer)
  2786. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
  2787. }
  2788. else
  2789. #endif
  2790. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
  2791. ssl->status_request = status_type;
  2792. #endif
  2793. }
  2794. return 0;
  2795. }
  2796. int TLSX_CSR_InitRequest(TLSX* extensions, DecodedCert* cert, void* heap)
  2797. {
  2798. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
  2799. CertificateStatusRequest* csr = extension ?
  2800. (CertificateStatusRequest*)extension->data : NULL;
  2801. int ret = 0;
  2802. if (csr) {
  2803. switch (csr->status_type) {
  2804. case WOLFSSL_CSR_OCSP: {
  2805. byte nonce[MAX_OCSP_NONCE_SZ];
  2806. int nonceSz = csr->request.ocsp.nonceSz;
  2807. /* preserve nonce */
  2808. XMEMCPY(nonce, csr->request.ocsp.nonce, nonceSz);
  2809. if ((ret = InitOcspRequest(&csr->request.ocsp, cert, 0, heap))
  2810. != 0)
  2811. return ret;
  2812. /* restore nonce */
  2813. XMEMCPY(csr->request.ocsp.nonce, nonce, nonceSz);
  2814. csr->request.ocsp.nonceSz = nonceSz;
  2815. }
  2816. break;
  2817. }
  2818. }
  2819. return ret;
  2820. }
  2821. void* TLSX_CSR_GetRequest(TLSX* extensions)
  2822. {
  2823. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
  2824. CertificateStatusRequest* csr = extension ?
  2825. (CertificateStatusRequest*)extension->data : NULL;
  2826. if (csr) {
  2827. switch (csr->status_type) {
  2828. case WOLFSSL_CSR_OCSP:
  2829. return &csr->request.ocsp;
  2830. }
  2831. }
  2832. return NULL;
  2833. }
  2834. int TLSX_CSR_ForceRequest(WOLFSSL* ssl)
  2835. {
  2836. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2837. CertificateStatusRequest* csr = extension ?
  2838. (CertificateStatusRequest*)extension->data : NULL;
  2839. if (csr) {
  2840. switch (csr->status_type) {
  2841. case WOLFSSL_CSR_OCSP:
  2842. if (SSL_CM(ssl)->ocspEnabled) {
  2843. csr->request.ocsp.ssl = ssl;
  2844. return CheckOcspRequest(SSL_CM(ssl)->ocsp,
  2845. &csr->request.ocsp, NULL, NULL);
  2846. }
  2847. else {
  2848. WOLFSSL_ERROR_VERBOSE(OCSP_LOOKUP_FAIL);
  2849. return OCSP_LOOKUP_FAIL;
  2850. }
  2851. }
  2852. }
  2853. return 0;
  2854. }
  2855. int TLSX_UseCertificateStatusRequest(TLSX** extensions, byte status_type,
  2856. byte options, WOLFSSL* ssl, void* heap,
  2857. int devId)
  2858. {
  2859. CertificateStatusRequest* csr = NULL;
  2860. int ret = 0;
  2861. if (!extensions || status_type != WOLFSSL_CSR_OCSP)
  2862. return BAD_FUNC_ARG;
  2863. csr = (CertificateStatusRequest*)
  2864. XMALLOC(sizeof(CertificateStatusRequest), heap, DYNAMIC_TYPE_TLSX);
  2865. if (!csr)
  2866. return MEMORY_E;
  2867. ForceZero(csr, sizeof(CertificateStatusRequest));
  2868. csr->status_type = status_type;
  2869. csr->options = options;
  2870. csr->ssl = ssl;
  2871. switch (csr->status_type) {
  2872. case WOLFSSL_CSR_OCSP:
  2873. if (options & WOLFSSL_CSR_OCSP_USE_NONCE) {
  2874. WC_RNG rng;
  2875. #ifndef HAVE_FIPS
  2876. ret = wc_InitRng_ex(&rng, heap, devId);
  2877. #else
  2878. ret = wc_InitRng(&rng);
  2879. (void)devId;
  2880. #endif
  2881. if (ret == 0) {
  2882. if (wc_RNG_GenerateBlock(&rng, csr->request.ocsp.nonce,
  2883. MAX_OCSP_NONCE_SZ) == 0)
  2884. csr->request.ocsp.nonceSz = MAX_OCSP_NONCE_SZ;
  2885. wc_FreeRng(&rng);
  2886. }
  2887. }
  2888. break;
  2889. }
  2890. if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST, csr, heap)) != 0) {
  2891. XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
  2892. return ret;
  2893. }
  2894. return WOLFSSL_SUCCESS;
  2895. }
  2896. #define CSR_FREE_ALL TLSX_CSR_Free
  2897. #define CSR_GET_SIZE TLSX_CSR_GetSize
  2898. #define CSR_WRITE TLSX_CSR_Write
  2899. #define CSR_PARSE TLSX_CSR_Parse
  2900. #else
  2901. #define CSR_FREE_ALL(data, heap) WC_DO_NOTHING
  2902. #define CSR_GET_SIZE(a, b) 0
  2903. #define CSR_WRITE(a, b, c) 0
  2904. #define CSR_PARSE(a, b, c, d) 0
  2905. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
  2906. /******************************************************************************/
  2907. /* Certificate Status Request v2 */
  2908. /******************************************************************************/
  2909. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2910. static void TLSX_CSR2_FreeAll(CertificateStatusRequestItemV2* csr2, void* heap)
  2911. {
  2912. CertificateStatusRequestItemV2* next;
  2913. for (; csr2; csr2 = next) {
  2914. next = csr2->next;
  2915. switch (csr2->status_type) {
  2916. case WOLFSSL_CSR2_OCSP:
  2917. case WOLFSSL_CSR2_OCSP_MULTI:
  2918. while(csr2->requests--)
  2919. FreeOcspRequest(&csr2->request.ocsp[csr2->requests]);
  2920. break;
  2921. }
  2922. XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
  2923. }
  2924. (void)heap;
  2925. }
  2926. static word16 TLSX_CSR2_GetSize(CertificateStatusRequestItemV2* csr2,
  2927. byte isRequest)
  2928. {
  2929. word16 size = 0;
  2930. /* shut up compiler warnings */
  2931. (void) csr2; (void) isRequest;
  2932. #ifndef NO_WOLFSSL_CLIENT
  2933. if (isRequest) {
  2934. CertificateStatusRequestItemV2* next;
  2935. for (size = OPAQUE16_LEN; csr2; csr2 = next) {
  2936. next = csr2->next;
  2937. switch (csr2->status_type) {
  2938. case WOLFSSL_CSR2_OCSP:
  2939. case WOLFSSL_CSR2_OCSP_MULTI:
  2940. size += ENUM_LEN + 3 * OPAQUE16_LEN;
  2941. if (csr2->request.ocsp[0].nonceSz)
  2942. size += OCSP_NONCE_EXT_SZ;
  2943. break;
  2944. }
  2945. }
  2946. }
  2947. #endif
  2948. return size;
  2949. }
  2950. static int TLSX_CSR2_Write(CertificateStatusRequestItemV2* csr2,
  2951. byte* output, byte isRequest)
  2952. {
  2953. /* shut up compiler warnings */
  2954. (void) csr2; (void) output; (void) isRequest;
  2955. #ifndef NO_WOLFSSL_CLIENT
  2956. if (isRequest) {
  2957. int ret = 0;
  2958. word16 offset;
  2959. word16 length;
  2960. for (offset = OPAQUE16_LEN; csr2 != NULL; csr2 = csr2->next) {
  2961. /* status_type */
  2962. output[offset++] = csr2->status_type;
  2963. /* request */
  2964. switch (csr2->status_type) {
  2965. case WOLFSSL_CSR2_OCSP:
  2966. case WOLFSSL_CSR2_OCSP_MULTI:
  2967. /* request_length */
  2968. length = 2 * OPAQUE16_LEN;
  2969. if (csr2->request.ocsp[0].nonceSz)
  2970. length += OCSP_NONCE_EXT_SZ;
  2971. c16toa(length, output + offset);
  2972. offset += OPAQUE16_LEN;
  2973. /* responder id list */
  2974. c16toa(0, output + offset);
  2975. offset += OPAQUE16_LEN;
  2976. /* request extensions */
  2977. length = 0;
  2978. if (csr2->request.ocsp[0].nonceSz) {
  2979. ret = (int)EncodeOcspRequestExtensions(
  2980. &csr2->request.ocsp[0],
  2981. output + offset + OPAQUE16_LEN,
  2982. OCSP_NONCE_EXT_SZ);
  2983. if (ret > 0) {
  2984. length = (word16)ret;
  2985. }
  2986. else {
  2987. return ret;
  2988. }
  2989. }
  2990. c16toa(length, output + offset);
  2991. offset += OPAQUE16_LEN + length;
  2992. break;
  2993. }
  2994. }
  2995. /* list size */
  2996. c16toa(offset - OPAQUE16_LEN, output);
  2997. return (int)offset;
  2998. }
  2999. #endif
  3000. return 0;
  3001. }
  3002. static int TLSX_CSR2_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  3003. byte isRequest)
  3004. {
  3005. int ret;
  3006. /* shut up compiler warnings */
  3007. (void) ssl; (void) input;
  3008. if (!isRequest) {
  3009. #ifndef NO_WOLFSSL_CLIENT
  3010. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
  3011. CertificateStatusRequestItemV2* csr2 = extension ?
  3012. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3013. if (!csr2) {
  3014. /* look at context level */
  3015. extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST_V2);
  3016. csr2 = extension ?
  3017. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3018. if (!csr2) /* unexpected extension */
  3019. return TLSX_HandleUnsupportedExtension(ssl);
  3020. /* enable extension at ssl level */
  3021. for (; csr2; csr2 = csr2->next) {
  3022. ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
  3023. csr2->status_type, csr2->options, ssl->heap,
  3024. ssl->devId);
  3025. if (ret != WOLFSSL_SUCCESS)
  3026. return ret;
  3027. switch (csr2->status_type) {
  3028. case WOLFSSL_CSR2_OCSP:
  3029. /* followed by */
  3030. case WOLFSSL_CSR2_OCSP_MULTI:
  3031. /* propagate nonce */
  3032. if (csr2->request.ocsp[0].nonceSz) {
  3033. OcspRequest* request =
  3034. (OcspRequest*)TLSX_CSR2_GetRequest(ssl->extensions,
  3035. csr2->status_type, 0);
  3036. if (request) {
  3037. XMEMCPY(request->nonce,
  3038. csr2->request.ocsp[0].nonce,
  3039. csr2->request.ocsp[0].nonceSz);
  3040. request->nonceSz =
  3041. csr2->request.ocsp[0].nonceSz;
  3042. }
  3043. }
  3044. break;
  3045. }
  3046. }
  3047. }
  3048. ssl->status_request_v2 = 1;
  3049. return length ? BUFFER_ERROR : 0; /* extension_data MUST be empty. */
  3050. #endif
  3051. }
  3052. else {
  3053. #ifndef NO_WOLFSSL_SERVER
  3054. byte status_type;
  3055. word16 request_length;
  3056. word16 offset = 0;
  3057. word16 size = 0;
  3058. /* list size */
  3059. if (offset + OPAQUE16_LEN >= length) {
  3060. return BUFFER_E;
  3061. }
  3062. ato16(input + offset, &request_length);
  3063. offset += OPAQUE16_LEN;
  3064. if (length - OPAQUE16_LEN != request_length)
  3065. return BUFFER_ERROR;
  3066. while (length > offset) {
  3067. if ((int)(length - offset) < ENUM_LEN + OPAQUE16_LEN)
  3068. return BUFFER_ERROR;
  3069. status_type = input[offset++];
  3070. ato16(input + offset, &request_length);
  3071. offset += OPAQUE16_LEN;
  3072. if (length - offset < request_length)
  3073. return BUFFER_ERROR;
  3074. switch (status_type) {
  3075. case WOLFSSL_CSR2_OCSP:
  3076. case WOLFSSL_CSR2_OCSP_MULTI:
  3077. /* skip responder_id_list */
  3078. if ((int)(length - offset) < OPAQUE16_LEN)
  3079. return BUFFER_ERROR;
  3080. ato16(input + offset, &size);
  3081. if (length - offset < size)
  3082. return BUFFER_ERROR;
  3083. offset += OPAQUE16_LEN + size;
  3084. /* skip request_extensions */
  3085. if ((int)(length - offset) < OPAQUE16_LEN)
  3086. return BUFFER_ERROR;
  3087. ato16(input + offset, &size);
  3088. if (length - offset < size)
  3089. return BUFFER_ERROR;
  3090. offset += OPAQUE16_LEN + size;
  3091. if (offset > length)
  3092. return BUFFER_ERROR;
  3093. /* is able to send OCSP response? */
  3094. if (SSL_CM(ssl) == NULL
  3095. || !SSL_CM(ssl)->ocspStaplingEnabled)
  3096. continue;
  3097. break;
  3098. default:
  3099. /* unknown status type, skipping! */
  3100. offset += request_length;
  3101. continue;
  3102. }
  3103. /* if using status_request and already sending it, remove it
  3104. * and prefer to use the v2 version */
  3105. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  3106. if (ssl->status_request) {
  3107. ssl->status_request = 0;
  3108. TLSX_Remove(&ssl->extensions, TLSX_STATUS_REQUEST, ssl->heap);
  3109. }
  3110. #endif
  3111. /* TLS 1.3 servers MUST NOT act upon presence or information in
  3112. * this extension (RFC 8448 Section 4.4.2.1).
  3113. */
  3114. if (!IsAtLeastTLSv1_3(ssl->version)) {
  3115. /* accept the first good status_type and return */
  3116. ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
  3117. status_type, 0, ssl->heap, ssl->devId);
  3118. if (ret != WOLFSSL_SUCCESS)
  3119. return ret; /* throw error */
  3120. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST_V2);
  3121. ssl->status_request_v2 = status_type;
  3122. }
  3123. return 0;
  3124. }
  3125. #endif
  3126. }
  3127. return 0;
  3128. }
  3129. int TLSX_CSR2_InitRequests(TLSX* extensions, DecodedCert* cert, byte isPeer,
  3130. void* heap)
  3131. {
  3132. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
  3133. CertificateStatusRequestItemV2* csr2 = extension ?
  3134. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3135. int ret = 0;
  3136. for (; csr2; csr2 = csr2->next) {
  3137. switch (csr2->status_type) {
  3138. case WOLFSSL_CSR2_OCSP:
  3139. if (!isPeer || csr2->requests != 0)
  3140. break;
  3141. FALL_THROUGH; /* followed by */
  3142. case WOLFSSL_CSR2_OCSP_MULTI: {
  3143. if (csr2->requests < 1 + MAX_CHAIN_DEPTH) {
  3144. byte nonce[MAX_OCSP_NONCE_SZ];
  3145. int nonceSz = csr2->request.ocsp[0].nonceSz;
  3146. /* preserve nonce, replicating nonce of ocsp[0] */
  3147. XMEMCPY(nonce, csr2->request.ocsp[0].nonce, nonceSz);
  3148. if ((ret = InitOcspRequest(
  3149. &csr2->request.ocsp[csr2->requests], cert,
  3150. 0, heap)) != 0)
  3151. return ret;
  3152. /* restore nonce */
  3153. XMEMCPY(csr2->request.ocsp[csr2->requests].nonce,
  3154. nonce, nonceSz);
  3155. csr2->request.ocsp[csr2->requests].nonceSz = nonceSz;
  3156. csr2->requests++;
  3157. }
  3158. }
  3159. break;
  3160. }
  3161. }
  3162. (void)cert;
  3163. return ret;
  3164. }
  3165. void* TLSX_CSR2_GetRequest(TLSX* extensions, byte status_type, byte idx)
  3166. {
  3167. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
  3168. CertificateStatusRequestItemV2* csr2 = extension ?
  3169. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3170. for (; csr2; csr2 = csr2->next) {
  3171. if (csr2->status_type == status_type) {
  3172. switch (csr2->status_type) {
  3173. case WOLFSSL_CSR2_OCSP:
  3174. /* followed by */
  3175. case WOLFSSL_CSR2_OCSP_MULTI:
  3176. /* requests are initialized in the reverse order */
  3177. return idx < csr2->requests
  3178. ? &csr2->request.ocsp[csr2->requests - idx - 1]
  3179. : NULL;
  3180. }
  3181. }
  3182. }
  3183. return NULL;
  3184. }
  3185. int TLSX_CSR2_ForceRequest(WOLFSSL* ssl)
  3186. {
  3187. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
  3188. CertificateStatusRequestItemV2* csr2 = extension ?
  3189. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3190. /* forces only the first one */
  3191. if (csr2) {
  3192. switch (csr2->status_type) {
  3193. case WOLFSSL_CSR2_OCSP:
  3194. /* followed by */
  3195. case WOLFSSL_CSR2_OCSP_MULTI:
  3196. if (SSL_CM(ssl)->ocspEnabled) {
  3197. csr2->request.ocsp[0].ssl = ssl;
  3198. return CheckOcspRequest(SSL_CM(ssl)->ocsp,
  3199. &csr2->request.ocsp[0], NULL, NULL);
  3200. }
  3201. else {
  3202. WOLFSSL_ERROR_VERBOSE(OCSP_LOOKUP_FAIL);
  3203. return OCSP_LOOKUP_FAIL;
  3204. }
  3205. }
  3206. }
  3207. return 0;
  3208. }
  3209. int TLSX_UseCertificateStatusRequestV2(TLSX** extensions, byte status_type,
  3210. byte options, void* heap, int devId)
  3211. {
  3212. TLSX* extension = NULL;
  3213. CertificateStatusRequestItemV2* csr2 = NULL;
  3214. int ret = 0;
  3215. if (!extensions)
  3216. return BAD_FUNC_ARG;
  3217. if (status_type != WOLFSSL_CSR2_OCSP
  3218. && status_type != WOLFSSL_CSR2_OCSP_MULTI)
  3219. return BAD_FUNC_ARG;
  3220. csr2 = (CertificateStatusRequestItemV2*)
  3221. XMALLOC(sizeof(CertificateStatusRequestItemV2), heap, DYNAMIC_TYPE_TLSX);
  3222. if (!csr2)
  3223. return MEMORY_E;
  3224. ForceZero(csr2, sizeof(CertificateStatusRequestItemV2));
  3225. csr2->status_type = status_type;
  3226. csr2->options = options;
  3227. csr2->next = NULL;
  3228. switch (csr2->status_type) {
  3229. case WOLFSSL_CSR2_OCSP:
  3230. case WOLFSSL_CSR2_OCSP_MULTI:
  3231. if (options & WOLFSSL_CSR2_OCSP_USE_NONCE) {
  3232. WC_RNG rng;
  3233. #ifndef HAVE_FIPS
  3234. ret = wc_InitRng_ex(&rng, heap, devId);
  3235. #else
  3236. ret = wc_InitRng(&rng);
  3237. (void)devId;
  3238. #endif
  3239. if (ret == 0) {
  3240. if (wc_RNG_GenerateBlock(&rng, csr2->request.ocsp[0].nonce,
  3241. MAX_OCSP_NONCE_SZ) == 0)
  3242. csr2->request.ocsp[0].nonceSz = MAX_OCSP_NONCE_SZ;
  3243. wc_FreeRng(&rng);
  3244. }
  3245. }
  3246. break;
  3247. }
  3248. /* append new item */
  3249. if ((extension = TLSX_Find(*extensions, TLSX_STATUS_REQUEST_V2))) {
  3250. CertificateStatusRequestItemV2* last =
  3251. (CertificateStatusRequestItemV2*)extension->data;
  3252. for (; last->next; last = last->next);
  3253. last->next = csr2;
  3254. }
  3255. else if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST_V2, csr2,heap))) {
  3256. XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
  3257. return ret;
  3258. }
  3259. return WOLFSSL_SUCCESS;
  3260. }
  3261. #define CSR2_FREE_ALL TLSX_CSR2_FreeAll
  3262. #define CSR2_GET_SIZE TLSX_CSR2_GetSize
  3263. #define CSR2_WRITE TLSX_CSR2_Write
  3264. #define CSR2_PARSE TLSX_CSR2_Parse
  3265. #else
  3266. #define CSR2_FREE_ALL(data, heap) WC_DO_NOTHING
  3267. #define CSR2_GET_SIZE(a, b) 0
  3268. #define CSR2_WRITE(a, b, c) 0
  3269. #define CSR2_PARSE(a, b, c, d) 0
  3270. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
  3271. /******************************************************************************/
  3272. /* Supported Elliptic Curves */
  3273. /******************************************************************************/
  3274. #ifdef HAVE_SUPPORTED_CURVES
  3275. #if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && !defined(HAVE_CURVE448) \
  3276. && !defined(HAVE_FFDHE) && !defined(HAVE_PQC)
  3277. #error Elliptic Curves Extension requires Elliptic Curve Cryptography or liboqs groups. \
  3278. Use --enable-ecc and/or --enable-liboqs in the configure script or \
  3279. define HAVE_ECC. Alternatively use FFDHE for DH cipher suites.
  3280. #endif
  3281. static int TLSX_SupportedCurve_New(SupportedCurve** curve, word16 name,
  3282. void* heap)
  3283. {
  3284. if (curve == NULL)
  3285. return BAD_FUNC_ARG;
  3286. (void)heap;
  3287. *curve = (SupportedCurve*)XMALLOC(sizeof(SupportedCurve), heap,
  3288. DYNAMIC_TYPE_TLSX);
  3289. if (*curve == NULL)
  3290. return MEMORY_E;
  3291. (*curve)->name = name;
  3292. (*curve)->next = NULL;
  3293. return 0;
  3294. }
  3295. static int TLSX_PointFormat_New(PointFormat** point, byte format, void* heap)
  3296. {
  3297. if (point == NULL)
  3298. return BAD_FUNC_ARG;
  3299. (void)heap;
  3300. *point = (PointFormat*)XMALLOC(sizeof(PointFormat), heap,
  3301. DYNAMIC_TYPE_TLSX);
  3302. if (*point == NULL)
  3303. return MEMORY_E;
  3304. (*point)->format = format;
  3305. (*point)->next = NULL;
  3306. return 0;
  3307. }
  3308. static void TLSX_SupportedCurve_FreeAll(SupportedCurve* list, void* heap)
  3309. {
  3310. SupportedCurve* curve;
  3311. while ((curve = list)) {
  3312. list = curve->next;
  3313. XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
  3314. }
  3315. (void)heap;
  3316. }
  3317. static void TLSX_PointFormat_FreeAll(PointFormat* list, void* heap)
  3318. {
  3319. PointFormat* point;
  3320. while ((point = list)) {
  3321. list = point->next;
  3322. XFREE(point, heap, DYNAMIC_TYPE_TLSX);
  3323. }
  3324. (void)heap;
  3325. }
  3326. static int TLSX_SupportedCurve_Append(SupportedCurve* list, word16 name,
  3327. void* heap)
  3328. {
  3329. int ret = BAD_FUNC_ARG;
  3330. while (list) {
  3331. if (list->name == name) {
  3332. ret = 0; /* curve already in use */
  3333. break;
  3334. }
  3335. if (list->next == NULL) {
  3336. ret = TLSX_SupportedCurve_New(&list->next, name, heap);
  3337. break;
  3338. }
  3339. list = list->next;
  3340. }
  3341. return ret;
  3342. }
  3343. static int TLSX_PointFormat_Append(PointFormat* list, byte format, void* heap)
  3344. {
  3345. int ret = BAD_FUNC_ARG;
  3346. while (list) {
  3347. if (list->format == format) {
  3348. ret = 0; /* format already in use */
  3349. break;
  3350. }
  3351. if (list->next == NULL) {
  3352. ret = TLSX_PointFormat_New(&list->next, format, heap);
  3353. break;
  3354. }
  3355. list = list->next;
  3356. }
  3357. return ret;
  3358. }
  3359. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  3360. #if defined(HAVE_FFDHE) && (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3361. defined(HAVE_CURVE448))
  3362. static void TLSX_SupportedCurve_ValidateRequest(const WOLFSSL* ssl,
  3363. const byte* semaphore)
  3364. {
  3365. /* If all pre-defined parameter types for key exchange are supported then
  3366. * always send SupportedGroups extension.
  3367. */
  3368. (void)ssl;
  3369. (void)semaphore;
  3370. }
  3371. #else
  3372. static void TLSX_SupportedCurve_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
  3373. {
  3374. word16 i;
  3375. const Suites* suites = WOLFSSL_SUITES(ssl);
  3376. for (i = 0; i < suites->suiteSz; i += 2) {
  3377. if (suites->suites[i] == TLS13_BYTE)
  3378. return;
  3379. #ifdef BUILD_TLS_SM4_GCM_SM3
  3380. if ((suites->suites[i] == CIPHER_BYTE) &&
  3381. (suites->suites[i+1] == TLS_SM4_GCM_SM3))
  3382. return;
  3383. #endif
  3384. #ifdef BUILD_TLS_SM4_CCM_SM3
  3385. if ((suites->suites[i] == CIPHER_BYTE) &&
  3386. (suites->suites[i+1] == TLS_SM4_CCM_SM3))
  3387. return;
  3388. #endif
  3389. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3
  3390. if ((suites->suites[i] == SM_BYTE) &&
  3391. (suites->suites[i+1] == TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3))
  3392. return;
  3393. #endif
  3394. if ((suites->suites[i] == ECC_BYTE) ||
  3395. (suites->suites[i] == ECDHE_PSK_BYTE) ||
  3396. (suites->suites[i] == CHACHA_BYTE)) {
  3397. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3398. defined(HAVE_CURVE448)
  3399. return;
  3400. #endif
  3401. }
  3402. #ifdef HAVE_FFDHE
  3403. else {
  3404. return;
  3405. }
  3406. #endif
  3407. }
  3408. /* turns semaphore on to avoid sending this extension. */
  3409. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_GROUPS));
  3410. }
  3411. #endif
  3412. /* Only send PointFormats if TLSv13, ECC or CHACHA cipher suite present.
  3413. */
  3414. static void TLSX_PointFormat_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
  3415. {
  3416. #ifdef HAVE_FFDHE
  3417. (void)ssl;
  3418. (void)semaphore;
  3419. #else
  3420. word16 i;
  3421. const Suites* suites = WOLFSSL_SUITES(ssl);
  3422. if (suites == NULL)
  3423. return;
  3424. for (i = 0; i < suites->suiteSz; i += 2) {
  3425. if (suites->suites[i] == TLS13_BYTE)
  3426. return;
  3427. #ifdef BUILD_TLS_SM4_GCM_SM3
  3428. if ((suites->suites[i] == CIPHER_BYTE) &&
  3429. (suites->suites[i+1] == TLS_SM4_GCM_SM3))
  3430. return;
  3431. #endif
  3432. #ifdef BUILD_TLS_SM4_CCM_SM3
  3433. if ((suites->suites[i] == CIPHER_BYTE) &&
  3434. (suites->suites[i+1] == TLS_SM4_CCM_SM3))
  3435. return;
  3436. #endif
  3437. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3
  3438. if ((suites->suites[i] == SM_BYTE) &&
  3439. (suites->suites[i+1] == TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3))
  3440. return;
  3441. #endif
  3442. if ((suites->suites[i] == ECC_BYTE) ||
  3443. (suites->suites[i] == ECDHE_PSK_BYTE) ||
  3444. (suites->suites[i] == CHACHA_BYTE)) {
  3445. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3446. defined(HAVE_CURVE448)
  3447. return;
  3448. #endif
  3449. }
  3450. }
  3451. /* turns semaphore on to avoid sending this extension. */
  3452. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  3453. #endif
  3454. }
  3455. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  3456. #ifndef NO_WOLFSSL_SERVER
  3457. static void TLSX_PointFormat_ValidateResponse(WOLFSSL* ssl, byte* semaphore)
  3458. {
  3459. #if defined(HAVE_FFDHE) || defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3460. defined(HAVE_CURVE448)
  3461. (void)semaphore;
  3462. #endif
  3463. if (ssl->options.cipherSuite0 == TLS13_BYTE)
  3464. return;
  3465. #ifdef BUILD_TLS_SM4_GCM_SM3
  3466. if ((ssl->options.cipherSuite0 == CIPHER_BYTE) &&
  3467. (ssl->options.cipherSuite == TLS_SM4_GCM_SM3))
  3468. return;
  3469. #endif
  3470. #ifdef BUILD_TLS_SM4_CCM_SM3
  3471. if ((ssl->options.cipherSuite0 == CIPHER_BYTE) &&
  3472. (ssl->options.cipherSuite == TLS_SM4_CCM_SM3))
  3473. return;
  3474. #endif
  3475. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3
  3476. if ((ssl->options.cipherSuite0 == SM_BYTE) &&
  3477. (ssl->options.cipherSuite == TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3))
  3478. return;
  3479. #endif
  3480. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  3481. if (ssl->options.cipherSuite0 == ECC_BYTE ||
  3482. ssl->options.cipherSuite0 == ECDHE_PSK_BYTE ||
  3483. ssl->options.cipherSuite0 == CHACHA_BYTE) {
  3484. return;
  3485. }
  3486. #endif
  3487. /* turns semaphore on to avoid sending this extension. */
  3488. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  3489. }
  3490. #endif /* !NO_WOLFSSL_SERVER */
  3491. #if !defined(NO_WOLFSSL_CLIENT) || defined(WOLFSSL_TLS13)
  3492. static word16 TLSX_SupportedCurve_GetSize(SupportedCurve* list)
  3493. {
  3494. SupportedCurve* curve;
  3495. word16 length = OPAQUE16_LEN; /* list length */
  3496. while ((curve = list)) {
  3497. list = curve->next;
  3498. length += OPAQUE16_LEN; /* curve length */
  3499. }
  3500. return length;
  3501. }
  3502. #endif
  3503. static word16 TLSX_PointFormat_GetSize(PointFormat* list)
  3504. {
  3505. PointFormat* point;
  3506. word16 length = ENUM_LEN; /* list length */
  3507. while ((point = list)) {
  3508. list = point->next;
  3509. length += ENUM_LEN; /* format length */
  3510. }
  3511. return length;
  3512. }
  3513. #if !defined(NO_WOLFSSL_CLIENT) || defined(WOLFSSL_TLS13)
  3514. static word16 TLSX_SupportedCurve_Write(SupportedCurve* list, byte* output)
  3515. {
  3516. word16 offset = OPAQUE16_LEN;
  3517. while (list) {
  3518. c16toa(list->name, output + offset);
  3519. offset += OPAQUE16_LEN;
  3520. list = list->next;
  3521. }
  3522. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  3523. return offset;
  3524. }
  3525. #endif
  3526. static word16 TLSX_PointFormat_Write(PointFormat* list, byte* output)
  3527. {
  3528. word16 offset = ENUM_LEN;
  3529. while (list) {
  3530. output[offset++] = list->format;
  3531. list = list->next;
  3532. }
  3533. output[0] = (byte)(offset - ENUM_LEN);
  3534. return offset;
  3535. }
  3536. #if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
  3537. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
  3538. int TLSX_SupportedCurve_Parse(const WOLFSSL* ssl, const byte* input,
  3539. word16 length, byte isRequest, TLSX** extensions)
  3540. {
  3541. word16 offset;
  3542. word16 name;
  3543. int ret;
  3544. if(!isRequest && !IsAtLeastTLSv1_3(ssl->version)) {
  3545. #ifdef WOLFSSL_ALLOW_SERVER_SC_EXT
  3546. return 0;
  3547. #else
  3548. return BUFFER_ERROR; /* servers doesn't send this extension. */
  3549. #endif
  3550. }
  3551. if (OPAQUE16_LEN > length || length % OPAQUE16_LEN)
  3552. return BUFFER_ERROR;
  3553. ato16(input, &offset);
  3554. /* validating curve list length */
  3555. if (length != OPAQUE16_LEN + offset)
  3556. return BUFFER_ERROR;
  3557. offset = OPAQUE16_LEN;
  3558. if (offset == length)
  3559. return 0;
  3560. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3561. if (!isRequest) {
  3562. TLSX* extension;
  3563. SupportedCurve* curve;
  3564. extension = TLSX_Find(*extensions, TLSX_SUPPORTED_GROUPS);
  3565. if (extension != NULL) {
  3566. /* Replace client list with server list of supported groups. */
  3567. curve = (SupportedCurve*)extension->data;
  3568. extension->data = NULL;
  3569. TLSX_SupportedCurve_FreeAll(curve, ssl->heap);
  3570. ato16(input + offset, &name);
  3571. offset += OPAQUE16_LEN;
  3572. ret = TLSX_SupportedCurve_New(&curve, name, ssl->heap);
  3573. if (ret != 0)
  3574. return ret; /* throw error */
  3575. extension->data = (void*)curve;
  3576. }
  3577. }
  3578. #endif
  3579. for (; offset < length; offset += OPAQUE16_LEN) {
  3580. ato16(input + offset, &name);
  3581. ret = TLSX_UseSupportedCurve(extensions, name, ssl->heap);
  3582. /* If it is BAD_FUNC_ARG then it is a group we do not support, but
  3583. * that is fine. */
  3584. if (ret != WOLFSSL_SUCCESS && ret != BAD_FUNC_ARG) {
  3585. return ret;
  3586. }
  3587. }
  3588. return 0;
  3589. }
  3590. #endif
  3591. #if !defined(NO_WOLFSSL_SERVER)
  3592. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3593. /* Checks the priority of the groups on the server and set the supported groups
  3594. * response if there is a group not advertised by the client that is preferred.
  3595. *
  3596. * ssl SSL/TLS object.
  3597. * returns 0 on success, otherwise an error.
  3598. */
  3599. int TLSX_SupportedCurve_CheckPriority(WOLFSSL* ssl)
  3600. {
  3601. int ret;
  3602. TLSX* extension;
  3603. TLSX* priority = NULL;
  3604. TLSX* ext = NULL;
  3605. word16 name;
  3606. SupportedCurve* curve;
  3607. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3608. /* May be doing PSK with no key exchange. */
  3609. if (extension == NULL)
  3610. return 0;
  3611. ret = TLSX_PopulateSupportedGroups(ssl, &priority);
  3612. if (ret != WOLFSSL_SUCCESS) {
  3613. TLSX_FreeAll(priority, ssl->heap);
  3614. return ret;
  3615. }
  3616. ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
  3617. if (ext == NULL) {
  3618. WOLFSSL_MSG("Could not find supported groups extension");
  3619. TLSX_FreeAll(priority, ssl->heap);
  3620. return 0;
  3621. }
  3622. curve = (SupportedCurve*)ext->data;
  3623. name = curve->name;
  3624. curve = (SupportedCurve*)extension->data;
  3625. while (curve != NULL) {
  3626. if (curve->name == name)
  3627. break;
  3628. curve = curve->next;
  3629. }
  3630. if (curve == NULL) {
  3631. /* Couldn't find the preferred group in client list. */
  3632. extension->resp = 1;
  3633. /* Send server list back and free client list. */
  3634. curve = (SupportedCurve*)extension->data;
  3635. extension->data = ext->data;
  3636. ext->data = curve;
  3637. }
  3638. TLSX_FreeAll(priority, ssl->heap);
  3639. return 0;
  3640. }
  3641. #endif /* WOLFSSL_TLS13 && !WOLFSSL_NO_SERVER_GROUPS_EXT */
  3642. #if defined(HAVE_FFDHE) && !defined(WOLFSSL_NO_TLS12)
  3643. #ifdef HAVE_PUBLIC_FFDHE
  3644. static int tlsx_ffdhe_find_group(WOLFSSL* ssl, SupportedCurve* clientGroup,
  3645. SupportedCurve* serverGroup)
  3646. {
  3647. int ret = 0;
  3648. SupportedCurve* group;
  3649. const DhParams* params = NULL;
  3650. for (; serverGroup != NULL; serverGroup = serverGroup->next) {
  3651. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(serverGroup->name))
  3652. continue;
  3653. for (group = clientGroup; group != NULL; group = group->next) {
  3654. if (serverGroup->name != group->name)
  3655. continue;
  3656. switch (serverGroup->name) {
  3657. #ifdef HAVE_FFDHE_2048
  3658. case WOLFSSL_FFDHE_2048:
  3659. params = wc_Dh_ffdhe2048_Get();
  3660. break;
  3661. #endif
  3662. #ifdef HAVE_FFDHE_3072
  3663. case WOLFSSL_FFDHE_3072:
  3664. params = wc_Dh_ffdhe3072_Get();
  3665. break;
  3666. #endif
  3667. #ifdef HAVE_FFDHE_4096
  3668. case WOLFSSL_FFDHE_4096:
  3669. params = wc_Dh_ffdhe4096_Get();
  3670. break;
  3671. #endif
  3672. #ifdef HAVE_FFDHE_6144
  3673. case WOLFSSL_FFDHE_6144:
  3674. params = wc_Dh_ffdhe6144_Get();
  3675. break;
  3676. #endif
  3677. #ifdef HAVE_FFDHE_8192
  3678. case WOLFSSL_FFDHE_8192:
  3679. params = wc_Dh_ffdhe8192_Get();
  3680. break;
  3681. #endif
  3682. default:
  3683. break;
  3684. }
  3685. if (params == NULL) {
  3686. ret = BAD_FUNC_ARG;
  3687. break;
  3688. }
  3689. if (params->p_len >= ssl->options.minDhKeySz &&
  3690. params->p_len <= ssl->options.maxDhKeySz) {
  3691. break;
  3692. }
  3693. }
  3694. if (ret != 0)
  3695. break;
  3696. if ((group != NULL) && (serverGroup->name == group->name))
  3697. break;
  3698. }
  3699. if ((ret == 0) && (serverGroup != NULL) && (params != NULL)) {
  3700. ssl->buffers.serverDH_P.buffer = (unsigned char *)params->p;
  3701. ssl->buffers.serverDH_P.length = params->p_len;
  3702. ssl->buffers.serverDH_G.buffer = (unsigned char *)params->g;
  3703. ssl->buffers.serverDH_G.length = params->g_len;
  3704. ssl->namedGroup = serverGroup->name;
  3705. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3706. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3707. ssl->options.dhDoKeyTest = 0;
  3708. #endif
  3709. ssl->options.haveDH = 1;
  3710. }
  3711. return ret;
  3712. }
  3713. #else
  3714. static int tlsx_ffdhe_find_group(WOLFSSL* ssl, SupportedCurve* clientGroup,
  3715. SupportedCurve* serverGroup)
  3716. {
  3717. int ret = 0;
  3718. SupportedCurve* group;
  3719. word32 p_len;
  3720. for (; serverGroup != NULL; serverGroup = serverGroup->next) {
  3721. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(serverGroup->name))
  3722. continue;
  3723. for (group = clientGroup; group != NULL; group = group->next) {
  3724. if (serverGroup->name != group->name)
  3725. continue;
  3726. wc_DhGetNamedKeyParamSize(serverGroup->name, &p_len, NULL, NULL);
  3727. if (p_len == 0) {
  3728. ret = BAD_FUNC_ARG;
  3729. break;
  3730. }
  3731. if (p_len >= ssl->options.minDhKeySz &&
  3732. p_len <= ssl->options.maxDhKeySz) {
  3733. break;
  3734. }
  3735. }
  3736. if (ret != 0)
  3737. break;
  3738. if ((group != NULL) && (serverGroup->name == group->name))
  3739. break;
  3740. }
  3741. if ((ret == 0) && (serverGroup != NULL)) {
  3742. word32 pSz, gSz;
  3743. ssl->buffers.serverDH_P.buffer = NULL;
  3744. ssl->buffers.serverDH_G.buffer = NULL;
  3745. ret = wc_DhGetNamedKeyParamSize(serverGroup->name, &pSz, &gSz, NULL);
  3746. if (ret == 0) {
  3747. ssl->buffers.serverDH_P.buffer =
  3748. (byte*)XMALLOC(pSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  3749. if (ssl->buffers.serverDH_P.buffer == NULL)
  3750. ret = MEMORY_E;
  3751. else
  3752. ssl->buffers.serverDH_P.length = pSz;
  3753. }
  3754. if (ret == 0) {
  3755. ssl->buffers.serverDH_G.buffer =
  3756. (byte*)XMALLOC(gSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  3757. if (ssl->buffers.serverDH_G.buffer == NULL) {
  3758. ret = MEMORY_E;
  3759. } else
  3760. ssl->buffers.serverDH_G.length = gSz;
  3761. }
  3762. if (ret == 0) {
  3763. ret = wc_DhCopyNamedKey(serverGroup->name,
  3764. ssl->buffers.serverDH_P.buffer, &pSz,
  3765. ssl->buffers.serverDH_G.buffer, &gSz,
  3766. NULL, NULL);
  3767. }
  3768. if (ret == 0) {
  3769. ssl->buffers.weOwnDH = 1;
  3770. ssl->namedGroup = serverGroup->name;
  3771. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3772. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3773. ssl->options.dhDoKeyTest = 0;
  3774. #endif
  3775. ssl->options.haveDH = 1;
  3776. }
  3777. else {
  3778. if (ssl->buffers.serverDH_P.buffer != NULL) {
  3779. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
  3780. DYNAMIC_TYPE_PUBLIC_KEY);
  3781. ssl->buffers.serverDH_P.length = 0;
  3782. ssl->buffers.serverDH_P.buffer = NULL;
  3783. }
  3784. if (ssl->buffers.serverDH_G.buffer != NULL) {
  3785. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
  3786. DYNAMIC_TYPE_PUBLIC_KEY);
  3787. ssl->buffers.serverDH_G.length = 0;
  3788. ssl->buffers.serverDH_G.buffer = NULL;
  3789. }
  3790. }
  3791. }
  3792. return ret;
  3793. }
  3794. #endif
  3795. /* Set the highest priority common FFDHE group on the server as compared to
  3796. * client extensions.
  3797. *
  3798. * ssl SSL/TLS object.
  3799. * returns 0 on success, otherwise an error.
  3800. */
  3801. int TLSX_SupportedFFDHE_Set(WOLFSSL* ssl)
  3802. {
  3803. int ret;
  3804. TLSX* priority = NULL;
  3805. TLSX* ext = NULL;
  3806. TLSX* extension;
  3807. SupportedCurve* clientGroup;
  3808. SupportedCurve* group;
  3809. int found = 0;
  3810. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3811. /* May be doing PSK with no key exchange. */
  3812. if (extension == NULL)
  3813. return 0;
  3814. clientGroup = (SupportedCurve*)extension->data;
  3815. for (group = clientGroup; group != NULL; group = group->next) {
  3816. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(group->name)) {
  3817. found = 1;
  3818. break;
  3819. }
  3820. }
  3821. if (!found)
  3822. return 0;
  3823. if (ssl->buffers.serverDH_P.buffer && ssl->buffers.weOwnDH) {
  3824. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
  3825. DYNAMIC_TYPE_PUBLIC_KEY);
  3826. }
  3827. if (ssl->buffers.serverDH_G.buffer && ssl->buffers.weOwnDH) {
  3828. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
  3829. DYNAMIC_TYPE_PUBLIC_KEY);
  3830. }
  3831. ssl->buffers.serverDH_P.buffer = NULL;
  3832. ssl->buffers.serverDH_G.buffer = NULL;
  3833. ssl->buffers.weOwnDH = 0;
  3834. ssl->options.haveDH = 0;
  3835. ret = TLSX_PopulateSupportedGroups(ssl, &priority);
  3836. if (ret == WOLFSSL_SUCCESS) {
  3837. SupportedCurve* serverGroup;
  3838. ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
  3839. serverGroup = (SupportedCurve*)ext->data;
  3840. ret = tlsx_ffdhe_find_group(ssl, clientGroup, serverGroup);
  3841. }
  3842. TLSX_FreeAll(priority, ssl->heap);
  3843. return ret;
  3844. }
  3845. #endif /* HAVE_FFDHE && !WOLFSSL_NO_TLS12 */
  3846. #endif /* !NO_WOLFSSL_SERVER */
  3847. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3848. /* Return the preferred group.
  3849. *
  3850. * ssl SSL/TLS object.
  3851. * checkSupported Whether to check for the first supported group.
  3852. * returns BAD_FUNC_ARG if no group found, otherwise the group.
  3853. */
  3854. int TLSX_SupportedCurve_Preferred(WOLFSSL* ssl, int checkSupported)
  3855. {
  3856. TLSX* extension;
  3857. SupportedCurve* curve;
  3858. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3859. if (extension == NULL)
  3860. return BAD_FUNC_ARG;
  3861. curve = (SupportedCurve*)extension->data;
  3862. while (curve != NULL) {
  3863. if (!checkSupported || TLSX_KeyShare_IsSupported(curve->name))
  3864. return curve->name;
  3865. curve = curve->next;
  3866. }
  3867. return BAD_FUNC_ARG;
  3868. }
  3869. #endif /* HAVE_SUPPORTED_CURVES */
  3870. #ifndef NO_WOLFSSL_SERVER
  3871. static int TLSX_PointFormat_Parse(WOLFSSL* ssl, const byte* input,
  3872. word16 length, byte isRequest)
  3873. {
  3874. int ret;
  3875. /* validating formats list length */
  3876. if (ENUM_LEN > length || length != (word16)ENUM_LEN + input[0])
  3877. return BUFFER_ERROR;
  3878. if (isRequest) {
  3879. /* adding uncompressed point format to response */
  3880. ret = TLSX_UsePointFormat(&ssl->extensions, WOLFSSL_EC_PF_UNCOMPRESSED,
  3881. ssl->heap);
  3882. if (ret != WOLFSSL_SUCCESS)
  3883. return ret; /* throw error */
  3884. TLSX_SetResponse(ssl, TLSX_EC_POINT_FORMATS);
  3885. }
  3886. return 0;
  3887. }
  3888. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  3889. int TLSX_ValidateSupportedCurves(const WOLFSSL* ssl, byte first, byte second,
  3890. word32* ecdhCurveOID) {
  3891. TLSX* extension = NULL;
  3892. SupportedCurve* curve = NULL;
  3893. word32 oid = 0;
  3894. word32 defOid = 0;
  3895. word32 defSz = 80; /* Maximum known curve size is 66. */
  3896. word32 nextOid = 0;
  3897. word32 nextSz = 80; /* Maximum known curve size is 66. */
  3898. word32 currOid = ssl->ecdhCurveOID;
  3899. int ephmSuite = 0;
  3900. word16 octets = 0; /* according to 'ecc_set_type ecc_sets[];' */
  3901. int key = 0; /* validate key */
  3902. (void)oid;
  3903. if (first == CHACHA_BYTE) {
  3904. switch (second) {
  3905. case TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  3906. case TLS_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3907. case TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3908. case TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
  3909. return 1; /* no suite restriction */
  3910. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  3911. case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
  3912. case TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3913. break;
  3914. }
  3915. }
  3916. if (first == ECC_BYTE || first == ECDHE_PSK_BYTE || first == CHACHA_BYTE)
  3917. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3918. if (!extension)
  3919. return 1; /* no suite restriction */
  3920. for (curve = (SupportedCurve*)extension->data;
  3921. curve && !key;
  3922. curve = curve->next) {
  3923. #ifdef OPENSSL_EXTRA
  3924. /* skip if name is not in supported ECC range
  3925. * or disabled by user */
  3926. if (wolfSSL_curve_is_disabled(ssl, curve->name))
  3927. continue;
  3928. #endif
  3929. /* find supported curve */
  3930. switch (curve->name) {
  3931. #ifdef HAVE_ECC
  3932. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  3933. #ifndef NO_ECC_SECP
  3934. case WOLFSSL_ECC_SECP160R1:
  3935. oid = ECC_SECP160R1_OID;
  3936. octets = 20;
  3937. break;
  3938. #endif /* !NO_ECC_SECP */
  3939. #ifdef HAVE_ECC_SECPR2
  3940. case WOLFSSL_ECC_SECP160R2:
  3941. oid = ECC_SECP160R2_OID;
  3942. octets = 20;
  3943. break;
  3944. #endif /* HAVE_ECC_SECPR2 */
  3945. #ifdef HAVE_ECC_KOBLITZ
  3946. case WOLFSSL_ECC_SECP160K1:
  3947. oid = ECC_SECP160K1_OID;
  3948. octets = 20;
  3949. break;
  3950. #endif /* HAVE_ECC_KOBLITZ */
  3951. #endif
  3952. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  3953. #ifndef NO_ECC_SECP
  3954. case WOLFSSL_ECC_SECP192R1:
  3955. oid = ECC_SECP192R1_OID;
  3956. octets = 24;
  3957. break;
  3958. #endif /* !NO_ECC_SECP */
  3959. #ifdef HAVE_ECC_KOBLITZ
  3960. case WOLFSSL_ECC_SECP192K1:
  3961. oid = ECC_SECP192K1_OID;
  3962. octets = 24;
  3963. break;
  3964. #endif /* HAVE_ECC_KOBLITZ */
  3965. #endif
  3966. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  3967. #ifndef NO_ECC_SECP
  3968. case WOLFSSL_ECC_SECP224R1:
  3969. oid = ECC_SECP224R1_OID;
  3970. octets = 28;
  3971. break;
  3972. #endif /* !NO_ECC_SECP */
  3973. #ifdef HAVE_ECC_KOBLITZ
  3974. case WOLFSSL_ECC_SECP224K1:
  3975. oid = ECC_SECP224K1_OID;
  3976. octets = 28;
  3977. break;
  3978. #endif /* HAVE_ECC_KOBLITZ */
  3979. #endif
  3980. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  3981. #ifndef NO_ECC_SECP
  3982. case WOLFSSL_ECC_SECP256R1:
  3983. oid = ECC_SECP256R1_OID;
  3984. octets = 32;
  3985. break;
  3986. #endif /* !NO_ECC_SECP */
  3987. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  3988. #endif
  3989. #if (defined(HAVE_CURVE25519) || defined(HAVE_ED25519)) && ECC_MIN_KEY_SZ <= 256
  3990. case WOLFSSL_ECC_X25519:
  3991. oid = ECC_X25519_OID;
  3992. octets = 32;
  3993. break;
  3994. #endif /* HAVE_CURVE25519 */
  3995. #ifdef HAVE_ECC
  3996. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  3997. #ifdef HAVE_ECC_KOBLITZ
  3998. case WOLFSSL_ECC_SECP256K1:
  3999. oid = ECC_SECP256K1_OID;
  4000. octets = 32;
  4001. break;
  4002. #endif /* HAVE_ECC_KOBLITZ */
  4003. #ifdef HAVE_ECC_BRAINPOOL
  4004. case WOLFSSL_ECC_BRAINPOOLP256R1:
  4005. oid = ECC_BRAINPOOLP256R1_OID;
  4006. octets = 32;
  4007. break;
  4008. #endif /* HAVE_ECC_BRAINPOOL */
  4009. #ifdef WOLFSSL_SM2
  4010. case WOLFSSL_ECC_SM2P256V1:
  4011. oid = ECC_SM2P256V1_OID;
  4012. octets = 32;
  4013. break;
  4014. #endif /* WOLFSSL_SM2 */
  4015. #endif
  4016. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  4017. #ifndef NO_ECC_SECP
  4018. case WOLFSSL_ECC_SECP384R1:
  4019. oid = ECC_SECP384R1_OID;
  4020. octets = 48;
  4021. break;
  4022. #endif /* !NO_ECC_SECP */
  4023. #ifdef HAVE_ECC_BRAINPOOL
  4024. case WOLFSSL_ECC_BRAINPOOLP384R1:
  4025. oid = ECC_BRAINPOOLP384R1_OID;
  4026. octets = 48;
  4027. break;
  4028. #endif /* HAVE_ECC_BRAINPOOL */
  4029. #endif
  4030. #endif
  4031. #if (defined(HAVE_CURVE448) || defined(HAVE_ED448)) && ECC_MIN_KEY_SZ <= 448
  4032. case WOLFSSL_ECC_X448:
  4033. oid = ECC_X448_OID;
  4034. octets = 57;
  4035. break;
  4036. #endif /* HAVE_CURVE448 */
  4037. #ifdef HAVE_ECC
  4038. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  4039. #ifdef HAVE_ECC_BRAINPOOL
  4040. case WOLFSSL_ECC_BRAINPOOLP512R1:
  4041. oid = ECC_BRAINPOOLP512R1_OID;
  4042. octets = 64;
  4043. break;
  4044. #endif /* HAVE_ECC_BRAINPOOL */
  4045. #endif
  4046. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  4047. #ifndef NO_ECC_SECP
  4048. case WOLFSSL_ECC_SECP521R1:
  4049. oid = ECC_SECP521R1_OID;
  4050. octets = 66;
  4051. break;
  4052. #endif /* !NO_ECC_SECP */
  4053. #endif
  4054. #endif
  4055. default: continue; /* unsupported curve */
  4056. }
  4057. #ifdef HAVE_ECC
  4058. /* Set default Oid */
  4059. if (defOid == 0 && ssl->eccTempKeySz <= octets && defSz > octets) {
  4060. defOid = oid;
  4061. defSz = octets;
  4062. }
  4063. /* The eccTempKeySz is the preferred ephemeral key size */
  4064. if (currOid == 0 && ssl->eccTempKeySz == octets)
  4065. currOid = oid;
  4066. if ((nextOid == 0 || nextSz > octets) && ssl->eccTempKeySz <= octets) {
  4067. nextOid = oid;
  4068. nextSz = octets;
  4069. }
  4070. #else
  4071. if (defOid == 0 && defSz > octets) {
  4072. defOid = oid;
  4073. defSz = octets;
  4074. }
  4075. if (currOid == 0)
  4076. currOid = oid;
  4077. if (nextOid == 0 || nextSz > octets) {
  4078. nextOid = oid;
  4079. nextSz = octets;
  4080. }
  4081. #endif
  4082. if (first == ECC_BYTE) {
  4083. switch (second) {
  4084. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  4085. /* ECDHE_ECDSA */
  4086. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:
  4087. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:
  4088. case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA:
  4089. case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA:
  4090. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
  4091. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384:
  4092. case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  4093. case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
  4094. case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
  4095. case TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8:
  4096. key |= ssl->ecdhCurveOID == oid;
  4097. ephmSuite = 1;
  4098. break;
  4099. #ifdef WOLFSSL_STATIC_DH
  4100. /* ECDH_ECDSA */
  4101. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA:
  4102. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA:
  4103. case TLS_ECDH_ECDSA_WITH_RC4_128_SHA:
  4104. case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA:
  4105. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256:
  4106. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384:
  4107. case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256:
  4108. case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384:
  4109. if (oid == ECC_X25519_OID && defOid == oid) {
  4110. defOid = 0;
  4111. defSz = 80;
  4112. }
  4113. if (oid == ECC_X448_OID && defOid == oid) {
  4114. defOid = 0;
  4115. defSz = 80;
  4116. }
  4117. key |= ssl->pkCurveOID == oid;
  4118. break;
  4119. #endif /* WOLFSSL_STATIC_DH */
  4120. #endif /* HAVE_ECC || HAVE_ED25519 || HAVE_ED448 */
  4121. #ifndef NO_RSA
  4122. /* ECDHE_RSA */
  4123. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
  4124. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
  4125. case TLS_ECDHE_RSA_WITH_RC4_128_SHA:
  4126. case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:
  4127. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
  4128. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384:
  4129. case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
  4130. case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
  4131. key |= ssl->ecdhCurveOID == oid;
  4132. ephmSuite = 1;
  4133. break;
  4134. #if defined(HAVE_ECC) && defined(WOLFSSL_STATIC_DH)
  4135. /* ECDH_RSA */
  4136. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA:
  4137. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA:
  4138. case TLS_ECDH_RSA_WITH_RC4_128_SHA:
  4139. case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA:
  4140. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256:
  4141. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384:
  4142. case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256:
  4143. case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384:
  4144. if (oid == ECC_X25519_OID && defOid == oid) {
  4145. defOid = 0;
  4146. defSz = 80;
  4147. }
  4148. if (oid == ECC_X448_OID && defOid == oid) {
  4149. defOid = 0;
  4150. defSz = 80;
  4151. }
  4152. key |= ssl->pkCurveOID == oid;
  4153. break;
  4154. #endif /* HAVE_ECC && WOLFSSL_STATIC_DH */
  4155. #endif
  4156. default:
  4157. if (oid == ECC_X25519_OID && defOid == oid) {
  4158. defOid = 0;
  4159. defSz = 80;
  4160. }
  4161. if (oid == ECC_X448_OID && defOid == oid) {
  4162. defOid = 0;
  4163. defSz = 80;
  4164. }
  4165. key = 1;
  4166. break;
  4167. }
  4168. }
  4169. /* ChaCha20-Poly1305 ECC cipher suites */
  4170. if (first == CHACHA_BYTE) {
  4171. switch (second) {
  4172. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  4173. /* ECDHE_ECDSA */
  4174. case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 :
  4175. case TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
  4176. key |= ssl->ecdhCurveOID == oid;
  4177. ephmSuite = 1;
  4178. break;
  4179. #endif /* HAVE_ECC || HAVE_ED25519 || HAVE_ED448 */
  4180. #ifndef NO_RSA
  4181. /* ECDHE_RSA */
  4182. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 :
  4183. case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
  4184. key |= ssl->ecdhCurveOID == oid;
  4185. ephmSuite = 1;
  4186. break;
  4187. #endif
  4188. default:
  4189. key = 1;
  4190. break;
  4191. }
  4192. }
  4193. }
  4194. *ecdhCurveOID = ssl->ecdhCurveOID;
  4195. /* Choose the default if it is at the required strength. */
  4196. #ifdef HAVE_ECC
  4197. if (*ecdhCurveOID == 0 && defSz == ssl->eccTempKeySz)
  4198. #else
  4199. if (*ecdhCurveOID == 0)
  4200. #endif
  4201. {
  4202. key = 1;
  4203. *ecdhCurveOID = defOid;
  4204. }
  4205. /* Choose any curve at the required strength. */
  4206. if (*ecdhCurveOID == 0) {
  4207. key = 1;
  4208. *ecdhCurveOID = currOid;
  4209. }
  4210. /* Choose the default if it is at the next highest strength. */
  4211. if (*ecdhCurveOID == 0 && defSz == nextSz)
  4212. *ecdhCurveOID = defOid;
  4213. /* Choose any curve at the next highest strength. */
  4214. if (*ecdhCurveOID == 0)
  4215. *ecdhCurveOID = nextOid;
  4216. /* No curve and ephemeral ECC suite requires a matching curve. */
  4217. if (*ecdhCurveOID == 0 && ephmSuite)
  4218. key = 0;
  4219. return key;
  4220. }
  4221. #endif
  4222. #endif /* NO_WOLFSSL_SERVER */
  4223. int TLSX_SupportedCurve_Copy(TLSX* src, TLSX** dst, void* heap)
  4224. {
  4225. TLSX* extension;
  4226. int ret;
  4227. extension = TLSX_Find(src, TLSX_SUPPORTED_GROUPS);
  4228. if (extension != NULL) {
  4229. SupportedCurve* curve;
  4230. for (curve = (SupportedCurve*)extension->data; curve != NULL;
  4231. curve = curve->next) {
  4232. ret = TLSX_UseSupportedCurve(dst, curve->name, heap);
  4233. if (ret != WOLFSSL_SUCCESS)
  4234. return MEMORY_E;
  4235. }
  4236. }
  4237. return 0;
  4238. }
  4239. int TLSX_UseSupportedCurve(TLSX** extensions, word16 name, void* heap)
  4240. {
  4241. TLSX* extension = NULL;
  4242. SupportedCurve* curve = NULL;
  4243. int ret;
  4244. if (extensions == NULL) {
  4245. return BAD_FUNC_ARG;
  4246. }
  4247. #ifdef WOLFSSL_TLS13
  4248. if (! TLSX_KeyShare_IsSupported(name)) {
  4249. return BAD_FUNC_ARG;
  4250. }
  4251. #endif
  4252. extension = TLSX_Find(*extensions, TLSX_SUPPORTED_GROUPS);
  4253. if (!extension) {
  4254. ret = TLSX_SupportedCurve_New(&curve, name, heap);
  4255. if (ret != 0)
  4256. return ret;
  4257. ret = TLSX_Push(extensions, TLSX_SUPPORTED_GROUPS, curve, heap);
  4258. if (ret != 0) {
  4259. XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
  4260. return ret;
  4261. }
  4262. }
  4263. else {
  4264. ret = TLSX_SupportedCurve_Append((SupportedCurve*)extension->data, name,
  4265. heap);
  4266. if (ret != 0)
  4267. return ret;
  4268. }
  4269. return WOLFSSL_SUCCESS;
  4270. }
  4271. int TLSX_UsePointFormat(TLSX** extensions, byte format, void* heap)
  4272. {
  4273. TLSX* extension = NULL;
  4274. PointFormat* point = NULL;
  4275. int ret = 0;
  4276. if (extensions == NULL)
  4277. return BAD_FUNC_ARG;
  4278. extension = TLSX_Find(*extensions, TLSX_EC_POINT_FORMATS);
  4279. if (!extension) {
  4280. ret = TLSX_PointFormat_New(&point, format, heap);
  4281. if (ret != 0)
  4282. return ret;
  4283. ret = TLSX_Push(extensions, TLSX_EC_POINT_FORMATS, point, heap);
  4284. if (ret != 0) {
  4285. XFREE(point, heap, DYNAMIC_TYPE_TLSX);
  4286. return ret;
  4287. }
  4288. }
  4289. else {
  4290. ret = TLSX_PointFormat_Append((PointFormat*)extension->data, format,
  4291. heap);
  4292. if (ret != 0)
  4293. return ret;
  4294. }
  4295. return WOLFSSL_SUCCESS;
  4296. }
  4297. #define EC_FREE_ALL TLSX_SupportedCurve_FreeAll
  4298. #define EC_VALIDATE_REQUEST TLSX_SupportedCurve_ValidateRequest
  4299. /* In TLS 1.2 the server never sends supported curve extension, but in TLS 1.3
  4300. * the server can send supported groups extension to indicate what it will
  4301. * support for later connections. */
  4302. #if !defined(NO_WOLFSSL_CLIENT) || defined(WOLFSSL_TLS13)
  4303. #define EC_GET_SIZE TLSX_SupportedCurve_GetSize
  4304. #define EC_WRITE TLSX_SupportedCurve_Write
  4305. #else
  4306. #define EC_GET_SIZE(list) 0
  4307. #define EC_WRITE(a, b) 0
  4308. #endif
  4309. #if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
  4310. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
  4311. #define EC_PARSE TLSX_SupportedCurve_Parse
  4312. #else
  4313. #define EC_PARSE(a, b, c, d, e) 0
  4314. #endif
  4315. #define PF_FREE_ALL TLSX_PointFormat_FreeAll
  4316. #define PF_VALIDATE_REQUEST TLSX_PointFormat_ValidateRequest
  4317. #define PF_VALIDATE_RESPONSE TLSX_PointFormat_ValidateResponse
  4318. #define PF_GET_SIZE TLSX_PointFormat_GetSize
  4319. #define PF_WRITE TLSX_PointFormat_Write
  4320. #ifndef NO_WOLFSSL_SERVER
  4321. #define PF_PARSE TLSX_PointFormat_Parse
  4322. #else
  4323. #define PF_PARSE(a, b, c, d) 0
  4324. #endif
  4325. #else
  4326. #define EC_FREE_ALL(list, heap) WC_DO_NOTHING
  4327. #define EC_GET_SIZE(list) 0
  4328. #define EC_WRITE(a, b) 0
  4329. #define EC_PARSE(a, b, c, d, e) 0
  4330. #define EC_VALIDATE_REQUEST(a, b) WC_DO_NOTHING
  4331. #define PF_FREE_ALL(list, heap) WC_DO_NOTHING
  4332. #define PF_GET_SIZE(list) 0
  4333. #define PF_WRITE(a, b) 0
  4334. #define PF_PARSE(a, b, c, d) 0
  4335. #define PF_VALIDATE_REQUEST(a, b) WC_DO_NOTHING
  4336. #define PF_VALIDATE_RESPONSE(a, b) WC_DO_NOTHING
  4337. #endif /* HAVE_SUPPORTED_CURVES */
  4338. /******************************************************************************/
  4339. /* Renegotiation Indication */
  4340. /******************************************************************************/
  4341. #if defined(HAVE_SECURE_RENEGOTIATION) \
  4342. || defined(HAVE_SERVER_RENEGOTIATION_INFO)
  4343. static byte TLSX_SecureRenegotiation_GetSize(SecureRenegotiation* data,
  4344. int isRequest)
  4345. {
  4346. byte length = OPAQUE8_LEN; /* empty info length */
  4347. /* data will be NULL for HAVE_SERVER_RENEGOTIATION_INFO only */
  4348. if (data && data->enabled && data->verifySet) {
  4349. /* client sends client_verify_data only */
  4350. length += TLS_FINISHED_SZ;
  4351. /* server also sends server_verify_data */
  4352. if (!isRequest)
  4353. length += TLS_FINISHED_SZ;
  4354. }
  4355. return length;
  4356. }
  4357. static word16 TLSX_SecureRenegotiation_Write(SecureRenegotiation* data,
  4358. byte* output, int isRequest)
  4359. {
  4360. word16 offset = OPAQUE8_LEN; /* RenegotiationInfo length */
  4361. if (data && data->enabled && data->verifySet) {
  4362. /* client sends client_verify_data only */
  4363. XMEMCPY(output + offset, data->client_verify_data, TLS_FINISHED_SZ);
  4364. offset += TLS_FINISHED_SZ;
  4365. /* server also sends server_verify_data */
  4366. if (!isRequest) {
  4367. XMEMCPY(output + offset, data->server_verify_data, TLS_FINISHED_SZ);
  4368. offset += TLS_FINISHED_SZ;
  4369. }
  4370. }
  4371. output[0] = (byte)(offset - 1); /* info length - self */
  4372. return offset;
  4373. }
  4374. static int TLSX_SecureRenegotiation_Parse(WOLFSSL* ssl, const byte* input,
  4375. word16 length, byte isRequest)
  4376. {
  4377. int ret = SECURE_RENEGOTIATION_E;
  4378. if (length >= OPAQUE8_LEN) {
  4379. if (isRequest) {
  4380. #ifndef NO_WOLFSSL_SERVER
  4381. if (ssl->secure_renegotiation == NULL) {
  4382. ret = wolfSSL_UseSecureRenegotiation(ssl);
  4383. if (ret == WOLFSSL_SUCCESS)
  4384. ret = 0;
  4385. }
  4386. if (ret != 0 && ret != SECURE_RENEGOTIATION_E) {
  4387. }
  4388. else if (ssl->secure_renegotiation == NULL) {
  4389. }
  4390. else if (!ssl->secure_renegotiation->enabled) {
  4391. if (*input == 0) {
  4392. input++; /* get past size */
  4393. ssl->secure_renegotiation->enabled = 1;
  4394. TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
  4395. ret = 0;
  4396. }
  4397. else {
  4398. /* already in error state */
  4399. WOLFSSL_MSG("SCR client verify data present");
  4400. }
  4401. }
  4402. else if (*input == TLS_FINISHED_SZ) {
  4403. if (length < TLS_FINISHED_SZ + 1) {
  4404. WOLFSSL_MSG("SCR malformed buffer");
  4405. ret = BUFFER_E;
  4406. }
  4407. else {
  4408. input++; /* get past size */
  4409. /* validate client verify data */
  4410. if (XMEMCMP(input,
  4411. ssl->secure_renegotiation->client_verify_data,
  4412. TLS_FINISHED_SZ) == 0) {
  4413. WOLFSSL_MSG("SCR client verify data match");
  4414. TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
  4415. ret = 0; /* verified */
  4416. }
  4417. else {
  4418. /* already in error state */
  4419. WOLFSSL_MSG("SCR client verify data Failure");
  4420. }
  4421. }
  4422. }
  4423. #endif
  4424. }
  4425. else if (ssl->secure_renegotiation != NULL) {
  4426. #ifndef NO_WOLFSSL_CLIENT
  4427. if (!ssl->secure_renegotiation->enabled) {
  4428. if (*input == 0) {
  4429. ssl->secure_renegotiation->enabled = 1;
  4430. ret = 0;
  4431. }
  4432. }
  4433. else if (*input == 2 * TLS_FINISHED_SZ &&
  4434. length == 2 * TLS_FINISHED_SZ + OPAQUE8_LEN) {
  4435. input++; /* get past size */
  4436. /* validate client and server verify data */
  4437. if (XMEMCMP(input,
  4438. ssl->secure_renegotiation->client_verify_data,
  4439. TLS_FINISHED_SZ) == 0 &&
  4440. XMEMCMP(input + TLS_FINISHED_SZ,
  4441. ssl->secure_renegotiation->server_verify_data,
  4442. TLS_FINISHED_SZ) == 0) {
  4443. WOLFSSL_MSG("SCR client and server verify data match");
  4444. ret = 0; /* verified */
  4445. }
  4446. else {
  4447. /* already in error state */
  4448. WOLFSSL_MSG("SCR client and server verify data Failure");
  4449. }
  4450. }
  4451. #endif
  4452. }
  4453. }
  4454. if (ret != 0) {
  4455. WOLFSSL_ERROR_VERBOSE(ret);
  4456. SendAlert(ssl, alert_fatal, handshake_failure);
  4457. }
  4458. return ret;
  4459. }
  4460. int TLSX_UseSecureRenegotiation(TLSX** extensions, void* heap)
  4461. {
  4462. int ret = 0;
  4463. SecureRenegotiation* data;
  4464. data = (SecureRenegotiation*)XMALLOC(sizeof(SecureRenegotiation), heap,
  4465. DYNAMIC_TYPE_TLSX);
  4466. if (data == NULL)
  4467. return MEMORY_E;
  4468. XMEMSET(data, 0, sizeof(SecureRenegotiation));
  4469. ret = TLSX_Push(extensions, TLSX_RENEGOTIATION_INFO, data, heap);
  4470. if (ret != 0) {
  4471. XFREE(data, heap, DYNAMIC_TYPE_TLSX);
  4472. return ret;
  4473. }
  4474. return WOLFSSL_SUCCESS;
  4475. }
  4476. #ifdef HAVE_SERVER_RENEGOTIATION_INFO
  4477. int TLSX_AddEmptyRenegotiationInfo(TLSX** extensions, void* heap)
  4478. {
  4479. int ret;
  4480. /* send empty renegotiation_info extension */
  4481. TLSX* ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
  4482. if (ext == NULL) {
  4483. ret = TLSX_UseSecureRenegotiation(extensions, heap);
  4484. if (ret != WOLFSSL_SUCCESS)
  4485. return ret;
  4486. ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
  4487. }
  4488. if (ext)
  4489. ext->resp = 1;
  4490. return WOLFSSL_SUCCESS;
  4491. }
  4492. #endif /* HAVE_SERVER_RENEGOTIATION_INFO */
  4493. #define SCR_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
  4494. #define SCR_GET_SIZE TLSX_SecureRenegotiation_GetSize
  4495. #define SCR_WRITE TLSX_SecureRenegotiation_Write
  4496. #define SCR_PARSE TLSX_SecureRenegotiation_Parse
  4497. #else
  4498. #define SCR_FREE_ALL(a, heap) WC_DO_NOTHING
  4499. #define SCR_GET_SIZE(a, b) 0
  4500. #define SCR_WRITE(a, b, c) 0
  4501. #define SCR_PARSE(a, b, c, d) 0
  4502. #endif /* HAVE_SECURE_RENEGOTIATION || HAVE_SERVER_RENEGOTIATION_INFO */
  4503. /******************************************************************************/
  4504. /* Session Tickets */
  4505. /******************************************************************************/
  4506. #ifdef HAVE_SESSION_TICKET
  4507. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  4508. static void TLSX_SessionTicket_ValidateRequest(WOLFSSL* ssl)
  4509. {
  4510. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_SESSION_TICKET);
  4511. SessionTicket* ticket = extension ?
  4512. (SessionTicket*)extension->data : NULL;
  4513. if (ticket) {
  4514. /* TODO validate ticket timeout here! */
  4515. if (ticket->lifetime == 0xfffffff) {
  4516. /* send empty ticket on timeout */
  4517. TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4518. }
  4519. }
  4520. }
  4521. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  4522. static word16 TLSX_SessionTicket_GetSize(SessionTicket* ticket, int isRequest)
  4523. {
  4524. (void)isRequest;
  4525. return ticket ? ticket->size : 0;
  4526. }
  4527. static word16 TLSX_SessionTicket_Write(SessionTicket* ticket, byte* output,
  4528. int isRequest)
  4529. {
  4530. word16 offset = 0; /* empty ticket */
  4531. if (isRequest && ticket) {
  4532. XMEMCPY(output + offset, ticket->data, ticket->size);
  4533. offset += ticket->size;
  4534. }
  4535. return offset;
  4536. }
  4537. static int TLSX_SessionTicket_Parse(WOLFSSL* ssl, const byte* input,
  4538. word16 length, byte isRequest)
  4539. {
  4540. int ret = 0;
  4541. (void) input; /* avoid unused parameter if NO_WOLFSSL_SERVER defined */
  4542. if (!isRequest) {
  4543. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_SESSION_TICKET))
  4544. return TLSX_HandleUnsupportedExtension(ssl);
  4545. if (length != 0)
  4546. return BUFFER_ERROR;
  4547. #ifndef NO_WOLFSSL_CLIENT
  4548. ssl->expect_session_ticket = 1;
  4549. #endif
  4550. }
  4551. #ifndef NO_WOLFSSL_SERVER
  4552. else {
  4553. /* server side */
  4554. if (ssl->ctx->ticketEncCb == NULL) {
  4555. WOLFSSL_MSG("Client sent session ticket, server has no callback");
  4556. return 0;
  4557. }
  4558. #ifdef HAVE_SECURE_RENEGOTIATION
  4559. if (IsSCR(ssl)) {
  4560. WOLFSSL_MSG("Client sent session ticket during SCR. Ignoring.");
  4561. return 0;
  4562. }
  4563. #endif
  4564. if (length > SESSION_TICKET_LEN) {
  4565. ret = BAD_TICKET_MSG_SZ;
  4566. WOLFSSL_ERROR_VERBOSE(ret);
  4567. } else if (IsAtLeastTLSv1_3(ssl->version)) {
  4568. WOLFSSL_MSG("Process client ticket rejected, TLS 1.3 no support");
  4569. ssl->options.rejectTicket = 1;
  4570. ret = 0; /* not fatal */
  4571. } else if (ssl->options.noTicketTls12) {
  4572. /* ignore ticket request */
  4573. } else if (length == 0) {
  4574. /* blank ticket */
  4575. ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4576. if (ret == WOLFSSL_SUCCESS) {
  4577. ret = 0;
  4578. /* send blank ticket */
  4579. TLSX_SetResponse(ssl, TLSX_SESSION_TICKET);
  4580. ssl->options.createTicket = 1; /* will send ticket msg */
  4581. ssl->options.useTicket = 1;
  4582. ssl->options.resuming = 0; /* no standard resumption */
  4583. ssl->arrays->sessionIDSz = 0; /* no echo on blank ticket */
  4584. }
  4585. } else {
  4586. /* got actual ticket from client */
  4587. ret = DoClientTicket(ssl, input, length);
  4588. if (ret == WOLFSSL_TICKET_RET_OK) { /* use ticket to resume */
  4589. WOLFSSL_MSG("Using existing client ticket");
  4590. ssl->options.useTicket = 1;
  4591. ssl->options.resuming = 1;
  4592. /* SERVER: ticket is peer auth. */
  4593. ssl->options.peerAuthGood = 1;
  4594. } else if (ret == WOLFSSL_TICKET_RET_CREATE) {
  4595. WOLFSSL_MSG("Using existing client ticket, creating new one");
  4596. ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4597. if (ret == WOLFSSL_SUCCESS) {
  4598. ret = 0;
  4599. TLSX_SetResponse(ssl, TLSX_SESSION_TICKET);
  4600. /* send blank ticket */
  4601. ssl->options.createTicket = 1; /* will send ticket msg */
  4602. ssl->options.useTicket = 1;
  4603. ssl->options.resuming = 1;
  4604. /* SERVER: ticket is peer auth. */
  4605. ssl->options.peerAuthGood = 1;
  4606. }
  4607. } else if (ret == WOLFSSL_TICKET_RET_REJECT) {
  4608. WOLFSSL_MSG("Process client ticket rejected, not using");
  4609. ssl->options.rejectTicket = 1;
  4610. ret = 0; /* not fatal */
  4611. } else if (ret == VERSION_ERROR) {
  4612. WOLFSSL_MSG("Process client ticket rejected, bad TLS version");
  4613. ssl->options.rejectTicket = 1;
  4614. ret = 0; /* not fatal */
  4615. } else if (ret == WOLFSSL_TICKET_RET_FATAL) {
  4616. WOLFSSL_MSG("Process client ticket fatal error, not using");
  4617. } else if (ret < 0) {
  4618. WOLFSSL_MSG("Process client ticket unknown error, not using");
  4619. }
  4620. }
  4621. }
  4622. #endif /* NO_WOLFSSL_SERVER */
  4623. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  4624. (void)ssl;
  4625. #endif
  4626. return ret;
  4627. }
  4628. WOLFSSL_LOCAL SessionTicket* TLSX_SessionTicket_Create(word32 lifetime,
  4629. byte* data, word16 size, void* heap)
  4630. {
  4631. SessionTicket* ticket = (SessionTicket*)XMALLOC(sizeof(SessionTicket),
  4632. heap, DYNAMIC_TYPE_TLSX);
  4633. if (ticket) {
  4634. ticket->data = (byte*)XMALLOC(size, heap, DYNAMIC_TYPE_TLSX);
  4635. if (ticket->data == NULL) {
  4636. XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
  4637. return NULL;
  4638. }
  4639. XMEMCPY(ticket->data, data, size);
  4640. ticket->size = size;
  4641. ticket->lifetime = lifetime;
  4642. }
  4643. (void)heap;
  4644. return ticket;
  4645. }
  4646. WOLFSSL_LOCAL void TLSX_SessionTicket_Free(SessionTicket* ticket, void* heap)
  4647. {
  4648. if (ticket) {
  4649. XFREE(ticket->data, heap, DYNAMIC_TYPE_TLSX);
  4650. XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
  4651. }
  4652. (void)heap;
  4653. }
  4654. int TLSX_UseSessionTicket(TLSX** extensions, SessionTicket* ticket, void* heap)
  4655. {
  4656. int ret = 0;
  4657. if (extensions == NULL)
  4658. return BAD_FUNC_ARG;
  4659. /* If the ticket is NULL, the client will request a new ticket from the
  4660. server. Otherwise, the client will use it in the next client hello. */
  4661. if ((ret = TLSX_Push(extensions, TLSX_SESSION_TICKET, (void*)ticket, heap))
  4662. != 0)
  4663. return ret;
  4664. return WOLFSSL_SUCCESS;
  4665. }
  4666. #define WOLF_STK_VALIDATE_REQUEST TLSX_SessionTicket_ValidateRequest
  4667. #define WOLF_STK_GET_SIZE TLSX_SessionTicket_GetSize
  4668. #define WOLF_STK_WRITE TLSX_SessionTicket_Write
  4669. #define WOLF_STK_PARSE TLSX_SessionTicket_Parse
  4670. #define WOLF_STK_FREE(stk, heap) TLSX_SessionTicket_Free((SessionTicket*)(stk),(heap))
  4671. #else
  4672. #define WOLF_STK_FREE(a, b) WC_DO_NOTHING
  4673. #define WOLF_STK_VALIDATE_REQUEST(a) WC_DO_NOTHING
  4674. #define WOLF_STK_GET_SIZE(a, b) 0
  4675. #define WOLF_STK_WRITE(a, b, c) 0
  4676. #define WOLF_STK_PARSE(a, b, c, d) 0
  4677. #endif /* HAVE_SESSION_TICKET */
  4678. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  4679. /******************************************************************************/
  4680. /* Encrypt-then-MAC */
  4681. /******************************************************************************/
  4682. #ifndef WOLFSSL_NO_TLS12
  4683. static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl);
  4684. /**
  4685. * Get the size of the Encrypt-Then-MAC extension.
  4686. *
  4687. * msgType Type of message to put extension into.
  4688. * pSz Size of extension data.
  4689. * return SANITY_MSG_E when the message is not allowed to have extension and
  4690. * 0 otherwise.
  4691. */
  4692. static int TLSX_EncryptThenMac_GetSize(byte msgType, word16* pSz)
  4693. {
  4694. (void)pSz;
  4695. if (msgType != client_hello && msgType != server_hello) {
  4696. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4697. return SANITY_MSG_E;
  4698. }
  4699. /* Empty extension */
  4700. return 0;
  4701. }
  4702. /**
  4703. * Write the Encrypt-Then-MAC extension.
  4704. *
  4705. * data Unused
  4706. * output Extension data buffer. Unused.
  4707. * msgType Type of message to put extension into.
  4708. * pSz Size of extension data.
  4709. * return SANITY_MSG_E when the message is not allowed to have extension and
  4710. * 0 otherwise.
  4711. */
  4712. static int TLSX_EncryptThenMac_Write(void* data, byte* output, byte msgType,
  4713. word16* pSz)
  4714. {
  4715. (void)data;
  4716. (void)output;
  4717. (void)pSz;
  4718. if (msgType != client_hello && msgType != server_hello) {
  4719. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4720. return SANITY_MSG_E;
  4721. }
  4722. /* Empty extension */
  4723. return 0;
  4724. }
  4725. /**
  4726. * Parse the Encrypt-Then-MAC extension.
  4727. *
  4728. * ssl SSL object
  4729. * input Extension data buffer.
  4730. * length Length of this extension's data.
  4731. * msgType Type of message to extension appeared in.
  4732. * return SANITY_MSG_E when the message is not allowed to have extension,
  4733. * BUFFER_ERROR when the extension's data is invalid,
  4734. * MEMORY_E when unable to allocate memory and
  4735. * 0 otherwise.
  4736. */
  4737. static int TLSX_EncryptThenMac_Parse(WOLFSSL* ssl, const byte* input,
  4738. word16 length, byte msgType)
  4739. {
  4740. int ret;
  4741. (void)input;
  4742. if (msgType != client_hello && msgType != server_hello) {
  4743. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4744. return SANITY_MSG_E;
  4745. }
  4746. /* Empty extension */
  4747. if (length != 0)
  4748. return BUFFER_ERROR;
  4749. if (msgType == client_hello) {
  4750. /* Check the user hasn't disallowed use of Encrypt-Then-Mac. */
  4751. if (!ssl->options.disallowEncThenMac) {
  4752. ssl->options.encThenMac = 1;
  4753. /* Set the extension reply. */
  4754. ret = TLSX_EncryptThenMac_Use(ssl);
  4755. if (ret != 0)
  4756. return ret;
  4757. }
  4758. return 0;
  4759. }
  4760. /* Server Hello */
  4761. if (ssl->options.disallowEncThenMac) {
  4762. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4763. return SANITY_MSG_E;
  4764. }
  4765. ssl->options.encThenMac = 1;
  4766. return 0;
  4767. }
  4768. /**
  4769. * Add the Encrypt-Then-MAC extension to list.
  4770. *
  4771. * ssl SSL object
  4772. * return MEMORY_E when unable to allocate memory and 0 otherwise.
  4773. */
  4774. static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl)
  4775. {
  4776. int ret = 0;
  4777. TLSX* extension;
  4778. /* Find the Encrypt-Then-Mac extension if it exists. */
  4779. extension = TLSX_Find(ssl->extensions, TLSX_ENCRYPT_THEN_MAC);
  4780. if (extension == NULL) {
  4781. /* Push new Encrypt-Then-Mac extension. */
  4782. ret = TLSX_Push(&ssl->extensions, TLSX_ENCRYPT_THEN_MAC, NULL,
  4783. ssl->heap);
  4784. if (ret != 0)
  4785. return ret;
  4786. }
  4787. return 0;
  4788. }
  4789. /**
  4790. * Set the Encrypt-Then-MAC extension as one to respond too.
  4791. *
  4792. * ssl SSL object
  4793. * return EXT_MISSING when EncryptThenMac extension not in list.
  4794. */
  4795. int TLSX_EncryptThenMac_Respond(WOLFSSL* ssl)
  4796. {
  4797. TLSX* extension;
  4798. extension = TLSX_Find(ssl->extensions, TLSX_ENCRYPT_THEN_MAC);
  4799. if (extension == NULL)
  4800. return EXT_MISSING;
  4801. extension->resp = 1;
  4802. return 0;
  4803. }
  4804. #define ETM_GET_SIZE TLSX_EncryptThenMac_GetSize
  4805. #define ETM_WRITE TLSX_EncryptThenMac_Write
  4806. #define ETM_PARSE TLSX_EncryptThenMac_Parse
  4807. #else
  4808. #define ETM_GET_SIZE(a, b) 0
  4809. #define ETM_WRITE(a, b, c, d) 0
  4810. #define ETM_PARSE(a, b, c, d) 0
  4811. #endif /* !WOLFSSL_NO_TLS12 */
  4812. #endif /* HAVE_ENCRYPT_THEN_MAC && !WOLFSSL_AEAD_ONLY */
  4813. #ifdef WOLFSSL_SRTP
  4814. /******************************************************************************/
  4815. /* DTLS SRTP (Secure Real-time Transport Protocol) */
  4816. /******************************************************************************/
  4817. /* Only support single SRTP profile */
  4818. typedef struct TlsxSrtp {
  4819. word16 profileCount;
  4820. word16 ids; /* selected bits */
  4821. } TlsxSrtp;
  4822. static int TLSX_UseSRTP_GetSize(TlsxSrtp *srtp)
  4823. {
  4824. /* SRTP Profile Len (2)
  4825. * SRTP Profiles (2)
  4826. * MKI (master key id) Length */
  4827. return (OPAQUE16_LEN + (srtp->profileCount * OPAQUE16_LEN) + 1);
  4828. }
  4829. static TlsxSrtp* TLSX_UseSRTP_New(word16 ids, void* heap)
  4830. {
  4831. TlsxSrtp* srtp;
  4832. int i;
  4833. srtp = (TlsxSrtp*)XMALLOC(sizeof(TlsxSrtp), heap, DYNAMIC_TYPE_TLSX);
  4834. if (srtp == NULL) {
  4835. WOLFSSL_MSG("TLSX SRTP Memory failure");
  4836. return NULL;
  4837. }
  4838. /* count and test each bit set */
  4839. srtp->profileCount = 0;
  4840. for (i=0; i<16; i++) {
  4841. if (ids & (1 << i)) {
  4842. srtp->profileCount++;
  4843. }
  4844. }
  4845. srtp->ids = ids;
  4846. return srtp;
  4847. }
  4848. static void TLSX_UseSRTP_Free(TlsxSrtp *srtp, void* heap)
  4849. {
  4850. if (srtp != NULL) {
  4851. XFREE(srtp, heap, DYNAMIC_TYPE_TLSX);
  4852. }
  4853. (void)heap;
  4854. }
  4855. static int TLSX_UseSRTP_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  4856. byte isRequest)
  4857. {
  4858. int ret = BAD_FUNC_ARG;
  4859. word16 profile_len = 0;
  4860. word16 profile_value = 0;
  4861. word16 offset = 0;
  4862. #ifndef NO_WOLFSSL_SERVER
  4863. int i;
  4864. TlsxSrtp* srtp = NULL;
  4865. #endif
  4866. if (length < OPAQUE16_LEN) {
  4867. return BUFFER_ERROR;
  4868. }
  4869. /* reset selected DTLS SRTP profile ID */
  4870. ssl->dtlsSrtpId = 0;
  4871. /* total length, not include itself */
  4872. ato16(input, &profile_len);
  4873. offset += OPAQUE16_LEN;
  4874. if (!isRequest) {
  4875. #ifndef NO_WOLFSSL_CLIENT
  4876. if (length < offset + OPAQUE16_LEN)
  4877. return BUFFER_ERROR;
  4878. ato16(input + offset, &profile_value);
  4879. /* check that the profile received was in the ones we support */
  4880. if (profile_value < 16 &&
  4881. (ssl->dtlsSrtpProfiles & (1 << profile_value))) {
  4882. ssl->dtlsSrtpId = profile_value;
  4883. ret = 0; /* success */
  4884. }
  4885. #endif
  4886. }
  4887. #ifndef NO_WOLFSSL_SERVER
  4888. else {
  4889. /* parse remainder one profile at a time, looking for match in CTX */
  4890. ret = 0;
  4891. for (i=offset; i<length; i+=OPAQUE16_LEN) {
  4892. if (length < (i + OPAQUE16_LEN)) {
  4893. WOLFSSL_MSG("Unexpected length when parsing SRTP profile");
  4894. ret = BUFFER_ERROR;
  4895. break;
  4896. }
  4897. ato16(input+i, &profile_value);
  4898. /* find first match */
  4899. if (profile_value < 16 &&
  4900. ssl->dtlsSrtpProfiles & (1 << profile_value)) {
  4901. ssl->dtlsSrtpId = profile_value;
  4902. /* make sure we respond with selected SRTP id selected */
  4903. srtp = TLSX_UseSRTP_New((1 << profile_value), ssl->heap);
  4904. if (srtp != NULL) {
  4905. ret = TLSX_Push(&ssl->extensions, TLSX_USE_SRTP,
  4906. (void*)srtp, ssl->heap);
  4907. if (ret == 0) {
  4908. TLSX_SetResponse(ssl, TLSX_USE_SRTP);
  4909. /* successfully set extension */
  4910. }
  4911. }
  4912. else {
  4913. ret = MEMORY_E;
  4914. }
  4915. break;
  4916. }
  4917. }
  4918. }
  4919. if (ret == 0 && ssl->dtlsSrtpId == 0) {
  4920. WOLFSSL_MSG("TLSX_UseSRTP_Parse profile not found!");
  4921. /* not fatal */
  4922. }
  4923. else if (ret != 0) {
  4924. ssl->dtlsSrtpId = 0;
  4925. TLSX_UseSRTP_Free(srtp, ssl->heap);
  4926. }
  4927. #endif
  4928. (void)profile_len;
  4929. return ret;
  4930. }
  4931. static word16 TLSX_UseSRTP_Write(TlsxSrtp* srtp, byte* output)
  4932. {
  4933. word16 offset = 0;
  4934. int i, j;
  4935. c16toa(srtp->profileCount * 2, output + offset);
  4936. offset += OPAQUE16_LEN;
  4937. j = 0;
  4938. for (i = 0; i < srtp->profileCount; i++) {
  4939. for (; j < 16; j++) {
  4940. if (srtp->ids & (1 << j)) {
  4941. c16toa(j, output + offset);
  4942. offset += OPAQUE16_LEN;
  4943. }
  4944. }
  4945. }
  4946. output[offset++] = 0x00; /* MKI Length */
  4947. return offset;
  4948. }
  4949. static int TLSX_UseSRTP(TLSX** extensions, word16 profiles, void* heap)
  4950. {
  4951. int ret = 0;
  4952. TLSX* extension;
  4953. if (extensions == NULL) {
  4954. return BAD_FUNC_ARG;
  4955. }
  4956. extension = TLSX_Find(*extensions, TLSX_USE_SRTP);
  4957. if (extension == NULL) {
  4958. TlsxSrtp* srtp = TLSX_UseSRTP_New(profiles, heap);
  4959. if (srtp == NULL) {
  4960. return MEMORY_E;
  4961. }
  4962. ret = TLSX_Push(extensions, TLSX_USE_SRTP, (void*)srtp, heap);
  4963. if (ret != 0) {
  4964. TLSX_UseSRTP_Free(srtp, heap);
  4965. }
  4966. }
  4967. return ret;
  4968. }
  4969. #ifndef NO_WOLFSSL_SERVER
  4970. #define SRTP_FREE TLSX_UseSRTP_Free
  4971. #define SRTP_PARSE TLSX_UseSRTP_Parse
  4972. #define SRTP_WRITE TLSX_UseSRTP_Write
  4973. #define SRTP_GET_SIZE TLSX_UseSRTP_GetSize
  4974. #else
  4975. #define SRTP_FREE(a, b) WC_DO_NOTHING
  4976. #define SRTP_PARSE(a, b, c, d) 0
  4977. #define SRTP_WRITE(a, b) 0
  4978. #define SRTP_GET_SIZE(a) 0
  4979. #endif
  4980. #endif /* WOLFSSL_SRTP */
  4981. /******************************************************************************/
  4982. /* Supported Versions */
  4983. /******************************************************************************/
  4984. #ifdef WOLFSSL_TLS13
  4985. static WC_INLINE int versionIsGreater(byte isDtls, byte a, byte b)
  4986. {
  4987. (void)isDtls;
  4988. #ifdef WOLFSSL_DTLS
  4989. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4990. if (isDtls)
  4991. return a < b;
  4992. #endif /* WOLFSSL_DTLS */
  4993. return a > b;
  4994. }
  4995. static WC_INLINE int versionIsLesser(byte isDtls, byte a, byte b)
  4996. {
  4997. (void)isDtls;
  4998. #ifdef WOLFSSL_DTLS
  4999. /* DTLS version increases backwards (-1,-2,-3,etc) */
  5000. if (isDtls)
  5001. return a > b;
  5002. #endif /* WOLFSSL_DTLS */
  5003. return a < b;
  5004. }
  5005. static WC_INLINE int versionIsAtLeast(byte isDtls, byte a, byte b)
  5006. {
  5007. (void)isDtls;
  5008. #ifdef WOLFSSL_DTLS
  5009. /* DTLS version increases backwards (-1,-2,-3,etc) */
  5010. if (isDtls)
  5011. return a <= b;
  5012. #endif /* WOLFSSL_DTLS */
  5013. return a >= b;
  5014. }
  5015. static WC_INLINE int versionIsLessEqual(byte isDtls, byte a, byte b)
  5016. {
  5017. (void)isDtls;
  5018. #ifdef WOLFSSL_DTLS
  5019. /* DTLS version increases backwards (-1,-2,-3,etc) */
  5020. if (isDtls)
  5021. return a >= b;
  5022. #endif /* WOLFSSL_DTLS */
  5023. return a <= b;
  5024. }
  5025. /* Return the size of the SupportedVersions extension's data.
  5026. *
  5027. * data The SSL/TLS object.
  5028. * msgType The type of the message this extension is being written into.
  5029. * returns the length of data that will be in the extension.
  5030. */
  5031. static int TLSX_SupportedVersions_GetSize(void* data, byte msgType, word16* pSz)
  5032. {
  5033. WOLFSSL* ssl = (WOLFSSL*)data;
  5034. byte tls13Minor, tls12Minor, tls11Minor, isDtls;
  5035. isDtls = !!ssl->options.dtls;
  5036. tls13Minor = (byte)(isDtls ? DTLSv1_3_MINOR : TLSv1_3_MINOR);
  5037. tls12Minor = (byte)(isDtls ? DTLSv1_2_MINOR : TLSv1_2_MINOR);
  5038. tls11Minor = (byte)(isDtls ? DTLS_MINOR : TLSv1_1_MINOR);
  5039. /* unused on some configuration */
  5040. (void)tls12Minor;
  5041. (void)tls13Minor;
  5042. (void)tls11Minor;
  5043. if (msgType == client_hello) {
  5044. /* TLS v1.2 and TLS v1.3 */
  5045. int cnt = 0;
  5046. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls13Minor)
  5047. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  5048. defined(WOLFSSL_WPAS_SMALL)
  5049. && (ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0
  5050. #endif
  5051. ) {
  5052. cnt++;
  5053. }
  5054. if (ssl->options.downgrade) {
  5055. #ifndef WOLFSSL_NO_TLS12
  5056. if (versionIsLessEqual(
  5057. isDtls, ssl->options.minDowngrade, tls12Minor)
  5058. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  5059. defined(WOLFSSL_WPAS_SMALL)
  5060. && (ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0
  5061. #endif
  5062. ) {
  5063. cnt++;
  5064. }
  5065. #endif
  5066. #ifndef NO_OLD_TLS
  5067. if (versionIsLessEqual(
  5068. isDtls, ssl->options.minDowngrade, tls11Minor)
  5069. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  5070. defined(WOLFSSL_WPAS_SMALL)
  5071. && (ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0
  5072. #endif
  5073. ) {
  5074. cnt++;
  5075. }
  5076. #ifdef WOLFSSL_ALLOW_TLSV10
  5077. if (!ssl->options.dtls && (ssl->options.minDowngrade <= TLSv1_MINOR)
  5078. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  5079. defined(WOLFSSL_WPAS_SMALL)
  5080. && (ssl->options.mask & SSL_OP_NO_TLSv1) == 0
  5081. #endif
  5082. ) {
  5083. cnt++;
  5084. }
  5085. #endif
  5086. #endif
  5087. }
  5088. *pSz += (word16)(OPAQUE8_LEN + cnt * OPAQUE16_LEN);
  5089. }
  5090. else if (msgType == server_hello || msgType == hello_retry_request) {
  5091. *pSz += OPAQUE16_LEN;
  5092. }
  5093. else {
  5094. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5095. return SANITY_MSG_E;
  5096. }
  5097. return 0;
  5098. }
  5099. /* Writes the SupportedVersions extension into the buffer.
  5100. *
  5101. * data The SSL/TLS object.
  5102. * output The buffer to write the extension into.
  5103. * msgType The type of the message this extension is being written into.
  5104. * returns the length of data that was written.
  5105. */
  5106. static int TLSX_SupportedVersions_Write(void* data, byte* output,
  5107. byte msgType, word16* pSz)
  5108. {
  5109. WOLFSSL* ssl = (WOLFSSL*)data;
  5110. byte tls13minor, tls12minor, tls11minor, isDtls = 0;
  5111. tls13minor = (byte)TLSv1_3_MINOR;
  5112. tls12minor = (byte)TLSv1_2_MINOR;
  5113. tls11minor = (byte)TLSv1_1_MINOR;
  5114. /* unused in some configuration */
  5115. (void)tls11minor;
  5116. (void)tls12minor;
  5117. #ifdef WOLFSSL_DTLS13
  5118. if (ssl->options.dtls) {
  5119. tls13minor = (byte)DTLSv1_3_MINOR;
  5120. tls12minor = (byte)DTLSv1_2_MINOR;
  5121. tls11minor = (byte)DTLS_MINOR;
  5122. isDtls = 1;
  5123. }
  5124. #endif /* WOLFSSL_DTLS13 */
  5125. if (msgType == client_hello) {
  5126. byte major = ssl->ctx->method->version.major;
  5127. byte* cnt = output++;
  5128. *cnt = 0;
  5129. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls13minor)
  5130. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  5131. defined(WOLFSSL_WPAS_SMALL)
  5132. && (ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0
  5133. #endif
  5134. ) {
  5135. *cnt += OPAQUE16_LEN;
  5136. #ifdef WOLFSSL_TLS13_DRAFT
  5137. /* The TLS draft major number. */
  5138. *(output++) = TLS_DRAFT_MAJOR;
  5139. /* Version of draft supported. */
  5140. *(output++) = TLS_DRAFT_MINOR;
  5141. #else
  5142. *(output++) = major;
  5143. *(output++) = tls13minor;
  5144. #endif
  5145. }
  5146. if (ssl->options.downgrade) {
  5147. #ifndef WOLFSSL_NO_TLS12
  5148. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls12minor)
  5149. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  5150. defined(WOLFSSL_WPAS_SMALL)
  5151. && (ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0
  5152. #endif
  5153. ) {
  5154. *cnt += OPAQUE16_LEN;
  5155. *(output++) = major;
  5156. *(output++) = tls12minor;
  5157. }
  5158. #endif
  5159. #ifndef NO_OLD_TLS
  5160. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls11minor)
  5161. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  5162. defined(WOLFSSL_WPAS_SMALL)
  5163. && (ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0
  5164. #endif
  5165. ) {
  5166. *cnt += OPAQUE16_LEN;
  5167. *(output++) = major;
  5168. *(output++) = tls11minor;
  5169. }
  5170. #ifdef WOLFSSL_ALLOW_TLSV10
  5171. if (!ssl->options.dtls && (ssl->options.minDowngrade <= TLSv1_MINOR)
  5172. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  5173. defined(WOLFSSL_WPAS_SMALL)
  5174. && (ssl->options.mask & SSL_OP_NO_TLSv1) == 0
  5175. #endif
  5176. ) {
  5177. *cnt += OPAQUE16_LEN;
  5178. *(output++) = major;
  5179. *(output++) = (byte)TLSv1_MINOR;
  5180. }
  5181. #endif
  5182. #endif
  5183. }
  5184. *pSz += (word16)(OPAQUE8_LEN + *cnt);
  5185. }
  5186. else if (msgType == server_hello || msgType == hello_retry_request) {
  5187. output[0] = ssl->version.major;
  5188. output[1] = ssl->version.minor;
  5189. *pSz += OPAQUE16_LEN;
  5190. }
  5191. else {
  5192. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5193. return SANITY_MSG_E;
  5194. }
  5195. return 0;
  5196. }
  5197. /* Parse the SupportedVersions extension.
  5198. *
  5199. * ssl The SSL/TLS object.
  5200. * input The buffer with the extension data.
  5201. * length The length of the extension data.
  5202. * msgType The type of the message this extension is being parsed from.
  5203. * pv The output ProtocolVersion for the negotiated version
  5204. * opts The output options structure. Can be NULL.
  5205. * exts The output extensions list. Can be NULL.
  5206. * returns 0 on success, otherwise failure.
  5207. */
  5208. int TLSX_SupportedVersions_Parse(const WOLFSSL* ssl, const byte* input,
  5209. word16 length, byte msgType, ProtocolVersion* pv, Options* opts,
  5210. TLSX** exts)
  5211. {
  5212. /* The client's greatest minor version that we support */
  5213. byte clientGreatestMinor = SSLv3_MINOR;
  5214. int ret;
  5215. byte major, minor;
  5216. byte tls13minor, tls12minor;
  5217. byte isDtls;
  5218. tls13minor = TLSv1_3_MINOR;
  5219. tls12minor = TLSv1_2_MINOR;
  5220. isDtls = ssl->options.dtls == 1;
  5221. #ifdef WOLFSSL_DTLS13
  5222. if (ssl->options.dtls) {
  5223. tls13minor = DTLSv1_3_MINOR;
  5224. tls12minor = DTLSv1_2_MINOR;
  5225. clientGreatestMinor = DTLS_MINOR;
  5226. }
  5227. #endif /* WOLFSSL_DTLS13 */
  5228. if (msgType == client_hello) {
  5229. int i;
  5230. int len;
  5231. int set = 0;
  5232. /* Must contain a length and at least one version. */
  5233. if (length < OPAQUE8_LEN + OPAQUE16_LEN || (length & 1) != 1)
  5234. return BUFFER_ERROR;
  5235. len = *input;
  5236. /* Protocol version array must fill rest of data. */
  5237. if (length != (word16)OPAQUE8_LEN + len)
  5238. return BUFFER_ERROR;
  5239. input++;
  5240. /* Find first match. */
  5241. for (i = 0; i < len; i += OPAQUE16_LEN) {
  5242. major = input[i];
  5243. minor = input[i + OPAQUE8_LEN];
  5244. #ifdef WOLFSSL_TLS13_DRAFT
  5245. if (major == TLS_DRAFT_MAJOR && minor == TLS_DRAFT_MINOR) {
  5246. major = SSLv3_MAJOR;
  5247. minor = TLSv1_3_MINOR;
  5248. }
  5249. #else
  5250. if (major == TLS_DRAFT_MAJOR)
  5251. continue;
  5252. #endif
  5253. if (major != ssl->ctx->method->version.major)
  5254. continue;
  5255. /* No upgrade allowed. */
  5256. if (versionIsGreater(isDtls, minor, ssl->version.minor))
  5257. continue;
  5258. /* Check downgrade. */
  5259. if (versionIsLesser(isDtls, minor, ssl->version.minor)) {
  5260. if (!ssl->options.downgrade)
  5261. continue;
  5262. if (versionIsLesser(isDtls, minor, ssl->options.minDowngrade))
  5263. continue;
  5264. }
  5265. if (versionIsGreater(isDtls, minor, clientGreatestMinor))
  5266. clientGreatestMinor = minor;
  5267. set = 1;
  5268. }
  5269. if (!set) {
  5270. /* No common supported version was negotiated */
  5271. SendAlert((WOLFSSL*)ssl, alert_fatal,
  5272. wolfssl_alert_protocol_version);
  5273. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5274. return VERSION_ERROR;
  5275. }
  5276. pv->minor = clientGreatestMinor;
  5277. if (versionIsAtLeast(isDtls, clientGreatestMinor, tls13minor)) {
  5278. if (opts != NULL)
  5279. opts->tls1_3 = 1;
  5280. /* TLS v1.3 requires supported version extension */
  5281. if (exts != NULL &&
  5282. TLSX_Find(*exts, TLSX_SUPPORTED_VERSIONS) == NULL) {
  5283. ret = TLSX_Push(exts,
  5284. TLSX_SUPPORTED_VERSIONS, ssl, ssl->heap);
  5285. if (ret != 0) {
  5286. return ret;
  5287. }
  5288. /* *exts should be pointing to the TLSX_SUPPORTED_VERSIONS
  5289. * ext in the list since it was pushed. */
  5290. (*exts)->resp = 1;
  5291. }
  5292. }
  5293. }
  5294. else if (msgType == server_hello || msgType == hello_retry_request) {
  5295. /* Must contain one version. */
  5296. if (length != OPAQUE16_LEN)
  5297. return BUFFER_ERROR;
  5298. major = input[0];
  5299. minor = input[OPAQUE8_LEN];
  5300. if (major != ssl->ctx->method->version.major) {
  5301. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5302. return VERSION_ERROR;
  5303. }
  5304. /* Can't downgrade with this extension below TLS v1.3. */
  5305. if (versionIsLesser(isDtls, minor, tls13minor)) {
  5306. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5307. return VERSION_ERROR;
  5308. }
  5309. /* Version is TLS v1.2 to handle downgrading from TLS v1.3+. */
  5310. if (ssl->options.downgrade && ssl->version.minor == tls12minor) {
  5311. /* Set minor version back to TLS v1.3+ */
  5312. pv->minor = ssl->ctx->method->version.minor;
  5313. }
  5314. /* No upgrade allowed. */
  5315. if (versionIsLesser(isDtls, ssl->version.minor, minor)) {
  5316. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5317. return VERSION_ERROR;
  5318. }
  5319. /* Check downgrade. */
  5320. if (versionIsGreater(isDtls, ssl->version.minor, minor)) {
  5321. if (!ssl->options.downgrade) {
  5322. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5323. return VERSION_ERROR;
  5324. }
  5325. if (versionIsLesser(
  5326. isDtls, minor, ssl->options.minDowngrade)) {
  5327. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5328. return VERSION_ERROR;
  5329. }
  5330. /* Downgrade the version. */
  5331. pv->minor = minor;
  5332. }
  5333. }
  5334. else {
  5335. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5336. return SANITY_MSG_E;
  5337. }
  5338. return 0;
  5339. }
  5340. /* Sets a new SupportedVersions extension into the extension list.
  5341. *
  5342. * extensions The list of extensions.
  5343. * data The extensions specific data.
  5344. * heap The heap used for allocation.
  5345. * returns 0 on success, otherwise failure.
  5346. */
  5347. static int TLSX_SetSupportedVersions(TLSX** extensions, const void* data,
  5348. void* heap)
  5349. {
  5350. if (extensions == NULL || data == NULL)
  5351. return BAD_FUNC_ARG;
  5352. return TLSX_Push(extensions, TLSX_SUPPORTED_VERSIONS, data, heap);
  5353. }
  5354. #define SV_GET_SIZE TLSX_SupportedVersions_GetSize
  5355. #define SV_WRITE TLSX_SupportedVersions_Write
  5356. #define SV_PARSE TLSX_SupportedVersions_Parse
  5357. #else
  5358. #define SV_GET_SIZE(a, b, c) 0
  5359. #define SV_WRITE(a, b, c, d) 0
  5360. #define SV_PARSE(a, b, c, d, e, f, g) 0
  5361. #endif /* WOLFSSL_TLS13 */
  5362. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  5363. /******************************************************************************/
  5364. /* Cookie */
  5365. /******************************************************************************/
  5366. /* Free the cookie data.
  5367. *
  5368. * cookie Cookie data.
  5369. * heap The heap used for allocation.
  5370. */
  5371. static void TLSX_Cookie_FreeAll(Cookie* cookie, void* heap)
  5372. {
  5373. (void)heap;
  5374. if (cookie != NULL)
  5375. XFREE(cookie, heap, DYNAMIC_TYPE_TLSX);
  5376. }
  5377. /* Get the size of the encoded Cookie extension.
  5378. * In messages: ClientHello and HelloRetryRequest.
  5379. *
  5380. * cookie The cookie to write.
  5381. * msgType The type of the message this extension is being written into.
  5382. * returns the number of bytes of the encoded Cookie extension.
  5383. */
  5384. static int TLSX_Cookie_GetSize(Cookie* cookie, byte msgType, word16* pSz)
  5385. {
  5386. if (msgType == client_hello || msgType == hello_retry_request) {
  5387. *pSz += OPAQUE16_LEN + cookie->len;
  5388. }
  5389. else {
  5390. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5391. return SANITY_MSG_E;
  5392. }
  5393. return 0;
  5394. }
  5395. /* Writes the Cookie extension into the output buffer.
  5396. * Assumes that the the output buffer is big enough to hold data.
  5397. * In messages: ClientHello and HelloRetryRequest.
  5398. *
  5399. * cookie The cookie to write.
  5400. * output The buffer to write into.
  5401. * msgType The type of the message this extension is being written into.
  5402. * returns the number of bytes written into the buffer.
  5403. */
  5404. static int TLSX_Cookie_Write(Cookie* cookie, byte* output, byte msgType,
  5405. word16* pSz)
  5406. {
  5407. if (msgType == client_hello || msgType == hello_retry_request) {
  5408. c16toa(cookie->len, output);
  5409. output += OPAQUE16_LEN;
  5410. XMEMCPY(output, cookie->data, cookie->len);
  5411. *pSz += OPAQUE16_LEN + cookie->len;
  5412. }
  5413. else {
  5414. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5415. return SANITY_MSG_E;
  5416. }
  5417. return 0;
  5418. }
  5419. /* Parse the Cookie extension.
  5420. * In messages: ClientHello and HelloRetryRequest.
  5421. *
  5422. * ssl The SSL/TLS object.
  5423. * input The extension data.
  5424. * length The length of the extension data.
  5425. * msgType The type of the message this extension is being parsed from.
  5426. * returns 0 on success and other values indicate failure.
  5427. */
  5428. static int TLSX_Cookie_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  5429. byte msgType)
  5430. {
  5431. word16 len;
  5432. word16 idx = 0;
  5433. TLSX* extension;
  5434. Cookie* cookie;
  5435. if (msgType != client_hello && msgType != hello_retry_request) {
  5436. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5437. return SANITY_MSG_E;
  5438. }
  5439. /* Message contains length and Cookie which must be at least one byte
  5440. * in length.
  5441. */
  5442. if (length < OPAQUE16_LEN + 1)
  5443. return BUFFER_E;
  5444. ato16(input + idx, &len);
  5445. idx += OPAQUE16_LEN;
  5446. if (length - idx != len)
  5447. return BUFFER_E;
  5448. if (msgType == hello_retry_request)
  5449. return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 1,
  5450. &ssl->extensions);
  5451. /* client_hello */
  5452. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5453. if (extension == NULL) {
  5454. #ifdef WOLFSSL_DTLS13
  5455. if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version))
  5456. /* Allow a cookie extension with DTLS 1.3 because it is possible
  5457. * that a different SSL instance sent the cookie but we are now
  5458. * receiving it. */
  5459. return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0,
  5460. &ssl->extensions);
  5461. else
  5462. #endif
  5463. {
  5464. WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR);
  5465. return HRR_COOKIE_ERROR;
  5466. }
  5467. }
  5468. cookie = (Cookie*)extension->data;
  5469. if (cookie->len != len || XMEMCMP(cookie->data, input + idx, len) != 0) {
  5470. WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR);
  5471. return HRR_COOKIE_ERROR;
  5472. }
  5473. /* Request seen. */
  5474. extension->resp = 0;
  5475. return 0;
  5476. }
  5477. /* Use the data to create a new Cookie object in the extensions.
  5478. *
  5479. * ssl SSL/TLS object.
  5480. * data Cookie data.
  5481. * len Length of cookie data in bytes.
  5482. * mac MAC data.
  5483. * macSz Length of MAC data in bytes.
  5484. * resp Indicates the extension will go into a response (HelloRetryRequest).
  5485. * returns 0 on success and other values indicate failure.
  5486. */
  5487. int TLSX_Cookie_Use(const WOLFSSL* ssl, const byte* data, word16 len, byte* mac,
  5488. byte macSz, int resp, TLSX** exts)
  5489. {
  5490. int ret = 0;
  5491. TLSX* extension;
  5492. Cookie* cookie;
  5493. /* Find the cookie extension if it exists. */
  5494. extension = TLSX_Find(*exts, TLSX_COOKIE);
  5495. if (extension == NULL) {
  5496. /* Push new cookie extension. */
  5497. ret = TLSX_Push(exts, TLSX_COOKIE, NULL, ssl->heap);
  5498. if (ret != 0)
  5499. return ret;
  5500. extension = TLSX_Find(*exts, TLSX_COOKIE);
  5501. if (extension == NULL)
  5502. return MEMORY_E;
  5503. }
  5504. cookie = (Cookie*)XMALLOC(sizeof(Cookie) + len + macSz, ssl->heap,
  5505. DYNAMIC_TYPE_TLSX);
  5506. if (cookie == NULL)
  5507. return MEMORY_E;
  5508. cookie->len = len + macSz;
  5509. XMEMCPY(cookie->data, data, len);
  5510. if (mac != NULL)
  5511. XMEMCPY(cookie->data + len, mac, macSz);
  5512. if (extension->data != NULL)
  5513. XFREE(extension->data, ssl->heap, DYNAMIC_TYPE_TLSX);
  5514. extension->data = (void*)cookie;
  5515. extension->resp = (byte)resp;
  5516. return 0;
  5517. }
  5518. #define CKE_FREE_ALL TLSX_Cookie_FreeAll
  5519. #define CKE_GET_SIZE TLSX_Cookie_GetSize
  5520. #define CKE_WRITE TLSX_Cookie_Write
  5521. #define CKE_PARSE TLSX_Cookie_Parse
  5522. #else
  5523. #define CKE_FREE_ALL(a, b) 0
  5524. #define CKE_GET_SIZE(a, b, c) 0
  5525. #define CKE_WRITE(a, b, c, d) 0
  5526. #define CKE_PARSE(a, b, c, d) 0
  5527. #endif
  5528. #if defined(WOLFSSL_TLS13) && !defined(NO_CERTS) && \
  5529. !defined(WOLFSSL_NO_CA_NAMES) && defined(OPENSSL_EXTRA)
  5530. /* Currently only settable through compatibility API */
  5531. /******************************************************************************/
  5532. /* Certificate Authorities */
  5533. /******************************************************************************/
  5534. static word16 TLSX_CA_Names_GetSize(void* data)
  5535. {
  5536. WOLFSSL* ssl = (WOLFSSL*)data;
  5537. WOLF_STACK_OF(WOLFSSL_X509_NAME)* names;
  5538. word16 size = 0;
  5539. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  5540. /* To add support use a different member like ssl->ca_names and
  5541. * add accessor functions:
  5542. * - *_set0_CA_list
  5543. * - *_get0_CA_list */
  5544. WOLFSSL_MSG("We don't currently support sending the client's list.");
  5545. return 0;
  5546. }
  5547. /* Length of names */
  5548. size += OPAQUE16_LEN;
  5549. for (names = SSL_CA_NAMES(ssl); names != NULL; names = names->next) {
  5550. byte seq[MAX_SEQ_SZ];
  5551. WOLFSSL_X509_NAME* name = names->data.name;
  5552. if (name != NULL) {
  5553. /* 16-bit length | SEQ | Len | DER of name */
  5554. size += (word16)(OPAQUE16_LEN + SetSequence(name->rawLen, seq) +
  5555. name->rawLen);
  5556. }
  5557. }
  5558. return size;
  5559. }
  5560. static word16 TLSX_CA_Names_Write(void* data, byte* output)
  5561. {
  5562. WOLFSSL* ssl = (WOLFSSL*)data;
  5563. WOLF_STACK_OF(WOLFSSL_X509_NAME)* names;
  5564. byte* len;
  5565. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  5566. /* To add support use a different member like ssl->ca_names and
  5567. * add accessor functions:
  5568. * - *_set0_CA_list
  5569. * - *_get0_CA_list */
  5570. WOLFSSL_MSG("We don't currently support sending the client's list.");
  5571. return 0;
  5572. }
  5573. /* Reserve space for the length value */
  5574. len = output;
  5575. output += OPAQUE16_LEN;
  5576. for (names = SSL_CA_NAMES(ssl); names != NULL; names = names->next) {
  5577. byte seq[MAX_SEQ_SZ];
  5578. WOLFSSL_X509_NAME* name = names->data.name;
  5579. if (name != NULL) {
  5580. c16toa((word16)name->rawLen +
  5581. (word16)SetSequence(name->rawLen, seq), output);
  5582. output += OPAQUE16_LEN;
  5583. output += SetSequence(name->rawLen, output);
  5584. XMEMCPY(output, name->raw, name->rawLen);
  5585. output += name->rawLen;
  5586. }
  5587. }
  5588. /* Write the total length */
  5589. c16toa((word16)(output - len - OPAQUE16_LEN), len);
  5590. return (word16)(output - len);
  5591. }
  5592. static int TLSX_CA_Names_Parse(WOLFSSL *ssl, const byte* input,
  5593. word16 length, byte isRequest)
  5594. {
  5595. word16 extLen;
  5596. (void)isRequest;
  5597. if (ssl->options.side == WOLFSSL_SERVER_END) {
  5598. /* To add support use a different member like ssl->ca_names and
  5599. * add accessor functions:
  5600. * - *_set0_CA_list
  5601. * - *_get0_CA_list */
  5602. WOLFSSL_MSG("We don't currently support parsing the client's list.");
  5603. return 0;
  5604. }
  5605. if (ssl->client_ca_names != ssl->ctx->client_ca_names)
  5606. wolfSSL_sk_X509_NAME_pop_free(ssl->client_ca_names, NULL);
  5607. ssl->client_ca_names = wolfSSL_sk_X509_NAME_new(NULL);
  5608. if (ssl->client_ca_names == NULL)
  5609. return MEMORY_ERROR;
  5610. if (length < OPAQUE16_LEN)
  5611. return BUFFER_ERROR;
  5612. ato16(input, &extLen);
  5613. input += OPAQUE16_LEN;
  5614. length -= OPAQUE16_LEN;
  5615. if (extLen != length)
  5616. return BUFFER_ERROR;
  5617. while (length) {
  5618. word32 idx = 0;
  5619. WOLFSSL_X509_NAME* name = NULL;
  5620. int ret = 0;
  5621. int didInit = FALSE;
  5622. /* Use a DecodedCert struct to get access to GetName to
  5623. * parse DN name */
  5624. #ifdef WOLFSSL_SMALL_STACK
  5625. DecodedCert *cert = (DecodedCert *)XMALLOC(
  5626. sizeof(*cert), ssl->heap, DYNAMIC_TYPE_DCERT);
  5627. if (cert == NULL)
  5628. return MEMORY_ERROR;
  5629. #else
  5630. DecodedCert cert[1];
  5631. #endif
  5632. if (length < OPAQUE16_LEN) {
  5633. ret = BUFFER_ERROR;
  5634. }
  5635. if (ret == 0) {
  5636. ato16(input, &extLen);
  5637. idx += OPAQUE16_LEN;
  5638. if (idx + extLen > length)
  5639. ret = BUFFER_ERROR;
  5640. }
  5641. if (ret == 0) {
  5642. InitDecodedCert(cert, input + idx, extLen, ssl->heap);
  5643. didInit = TRUE;
  5644. idx += extLen;
  5645. ret = GetName(cert, SUBJECT, extLen);
  5646. }
  5647. if (ret == 0 && (name = wolfSSL_X509_NAME_new()) == NULL)
  5648. ret = MEMORY_ERROR;
  5649. if (ret == 0) {
  5650. CopyDecodedName(name, cert, SUBJECT);
  5651. if (wolfSSL_sk_X509_NAME_push(ssl->client_ca_names, name)
  5652. == WOLFSSL_FAILURE)
  5653. ret = MEMORY_ERROR;
  5654. }
  5655. if (didInit)
  5656. FreeDecodedCert(cert);
  5657. #ifdef WOLFSSL_SMALL_STACK
  5658. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  5659. #endif
  5660. if (ret != 0)
  5661. return ret;
  5662. input += idx;
  5663. length -= (word16)idx;
  5664. }
  5665. return 0;
  5666. }
  5667. #define CAN_GET_SIZE TLSX_CA_Names_GetSize
  5668. #define CAN_WRITE TLSX_CA_Names_Write
  5669. #define CAN_PARSE TLSX_CA_Names_Parse
  5670. #else
  5671. #define CAN_GET_SIZE(...) 0
  5672. #define CAN_WRITE(...) 0
  5673. #define CAN_PARSE(...) 0
  5674. #endif
  5675. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  5676. /******************************************************************************/
  5677. /* Signature Algorithms */
  5678. /******************************************************************************/
  5679. /* Return the size of the SignatureAlgorithms extension's data.
  5680. *
  5681. * data Unused
  5682. * returns the length of data that will be in the extension.
  5683. */
  5684. static word16 TLSX_SignatureAlgorithms_GetSize(void* data)
  5685. {
  5686. SignatureAlgorithms* sa = (SignatureAlgorithms*)data;
  5687. if (sa->hashSigAlgoSz == 0)
  5688. return OPAQUE16_LEN + WOLFSSL_SUITES(sa->ssl)->hashSigAlgoSz;
  5689. else
  5690. return OPAQUE16_LEN + sa->hashSigAlgoSz;
  5691. }
  5692. /* Creates a bit string of supported hash algorithms with RSA PSS.
  5693. * The bit string is used when determining which signature algorithm to use
  5694. * when creating the CertificateVerify message.
  5695. * Note: Valid data has an even length as each signature algorithm is two bytes.
  5696. *
  5697. * ssl The SSL/TLS object.
  5698. * input The buffer with the list of supported signature algorithms.
  5699. * length The length of the list in bytes.
  5700. * returns 0 on success, BUFFER_ERROR when the length is not even.
  5701. */
  5702. static int TLSX_SignatureAlgorithms_MapPss(WOLFSSL *ssl, const byte* input,
  5703. word16 length)
  5704. {
  5705. word16 i;
  5706. if ((length & 1) == 1)
  5707. return BUFFER_ERROR;
  5708. ssl->pssAlgo = 0;
  5709. for (i = 0; i < length; i += 2) {
  5710. if (input[i] == rsa_pss_sa_algo && input[i + 1] <= sha512_mac)
  5711. ssl->pssAlgo |= 1 << input[i + 1];
  5712. #ifdef WOLFSSL_TLS13
  5713. if (input[i] == rsa_pss_sa_algo && input[i + 1] >= pss_sha256 &&
  5714. input[i + 1] <= pss_sha512) {
  5715. ssl->pssAlgo |= 1 << input[i + 1];
  5716. }
  5717. #endif
  5718. }
  5719. return 0;
  5720. }
  5721. /* Writes the SignatureAlgorithms extension into the buffer.
  5722. *
  5723. * data Unused
  5724. * output The buffer to write the extension into.
  5725. * returns the length of data that was written.
  5726. */
  5727. static word16 TLSX_SignatureAlgorithms_Write(void* data, byte* output)
  5728. {
  5729. SignatureAlgorithms* sa = (SignatureAlgorithms*)data;
  5730. const Suites* suites = WOLFSSL_SUITES(sa->ssl);
  5731. word16 hashSigAlgoSz;
  5732. if (sa->hashSigAlgoSz == 0) {
  5733. c16toa(suites->hashSigAlgoSz, output);
  5734. XMEMCPY(output + OPAQUE16_LEN, suites->hashSigAlgo,
  5735. suites->hashSigAlgoSz);
  5736. hashSigAlgoSz = suites->hashSigAlgoSz;
  5737. }
  5738. else {
  5739. c16toa(sa->hashSigAlgoSz, output);
  5740. XMEMCPY(output + OPAQUE16_LEN, sa->hashSigAlgo,
  5741. sa->hashSigAlgoSz);
  5742. hashSigAlgoSz = sa->hashSigAlgoSz;
  5743. }
  5744. #ifndef NO_RSA
  5745. TLSX_SignatureAlgorithms_MapPss(sa->ssl, output + OPAQUE16_LEN,
  5746. hashSigAlgoSz);
  5747. #endif
  5748. return OPAQUE16_LEN + hashSigAlgoSz;
  5749. }
  5750. /* Parse the SignatureAlgorithms extension.
  5751. *
  5752. * ssl The SSL/TLS object.
  5753. * input The buffer with the extension data.
  5754. * length The length of the extension data.
  5755. * returns 0 on success, otherwise failure.
  5756. */
  5757. static int TLSX_SignatureAlgorithms_Parse(WOLFSSL *ssl, const byte* input,
  5758. word16 length, byte isRequest, Suites* suites)
  5759. {
  5760. word16 len;
  5761. if (!isRequest)
  5762. return BUFFER_ERROR;
  5763. /* Must contain a length and at least algorithm. */
  5764. if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
  5765. return BUFFER_ERROR;
  5766. ato16(input, &len);
  5767. input += OPAQUE16_LEN;
  5768. /* Algorithm array must fill rest of data. */
  5769. if (length != OPAQUE16_LEN + len)
  5770. return BUFFER_ERROR;
  5771. /* Sig Algo list size must be even. */
  5772. if (suites->hashSigAlgoSz % 2 != 0)
  5773. return BUFFER_ERROR;
  5774. /* truncate hashSigAlgo list if too long */
  5775. suites->hashSigAlgoSz = len;
  5776. if (suites->hashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
  5777. WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
  5778. suites->hashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
  5779. }
  5780. XMEMCPY(suites->hashSigAlgo, input, suites->hashSigAlgoSz);
  5781. return TLSX_SignatureAlgorithms_MapPss(ssl, input, len);
  5782. }
  5783. /* Sets a new SignatureAlgorithms extension into the extension list.
  5784. *
  5785. * extensions The list of extensions.
  5786. * data The extensions specific data.
  5787. * heap The heap used for allocation.
  5788. * returns 0 on success, otherwise failure.
  5789. */
  5790. static int TLSX_SetSignatureAlgorithms(TLSX** extensions, WOLFSSL* ssl,
  5791. void* heap)
  5792. {
  5793. SignatureAlgorithms* sa;
  5794. int ret;
  5795. if (extensions == NULL)
  5796. return BAD_FUNC_ARG;
  5797. /* Already present */
  5798. if (TLSX_Find(*extensions, TLSX_SIGNATURE_ALGORITHMS) != NULL)
  5799. return 0;
  5800. sa = TLSX_SignatureAlgorithms_New(ssl, 0, heap);
  5801. if (sa == NULL)
  5802. return MEMORY_ERROR;
  5803. ret = TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS, sa, heap);
  5804. if (ret != 0)
  5805. TLSX_SignatureAlgorithms_FreeAll(sa, heap);
  5806. return ret;
  5807. }
  5808. SignatureAlgorithms* TLSX_SignatureAlgorithms_New(WOLFSSL* ssl,
  5809. word16 hashSigAlgoSz, void* heap)
  5810. {
  5811. SignatureAlgorithms* sa;
  5812. (void)heap;
  5813. sa = (SignatureAlgorithms*)XMALLOC(sizeof(*sa) + hashSigAlgoSz, heap,
  5814. DYNAMIC_TYPE_TLSX);
  5815. if (sa != NULL) {
  5816. XMEMSET(sa, 0, sizeof(*sa) + hashSigAlgoSz);
  5817. sa->ssl = ssl;
  5818. sa->hashSigAlgoSz = hashSigAlgoSz;
  5819. }
  5820. return sa;
  5821. }
  5822. void TLSX_SignatureAlgorithms_FreeAll(SignatureAlgorithms* sa,
  5823. void* heap)
  5824. {
  5825. XFREE(sa, heap, DYNAMIC_TYPE_TLSX);
  5826. (void)heap;
  5827. }
  5828. #define SA_GET_SIZE TLSX_SignatureAlgorithms_GetSize
  5829. #define SA_WRITE TLSX_SignatureAlgorithms_Write
  5830. #define SA_PARSE TLSX_SignatureAlgorithms_Parse
  5831. #define SA_FREE_ALL TLSX_SignatureAlgorithms_FreeAll
  5832. #endif
  5833. /******************************************************************************/
  5834. /* Signature Algorithms Certificate */
  5835. /******************************************************************************/
  5836. #if defined(WOLFSSL_TLS13) && !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  5837. /* Return the size of the SignatureAlgorithms extension's data.
  5838. *
  5839. * data Unused
  5840. * returns the length of data that will be in the extension.
  5841. */
  5842. static word16 TLSX_SignatureAlgorithmsCert_GetSize(void* data)
  5843. {
  5844. WOLFSSL* ssl = (WOLFSSL*)data;
  5845. return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
  5846. }
  5847. /* Writes the SignatureAlgorithmsCert extension into the buffer.
  5848. *
  5849. * data Unused
  5850. * output The buffer to write the extension into.
  5851. * returns the length of data that was written.
  5852. */
  5853. static word16 TLSX_SignatureAlgorithmsCert_Write(void* data, byte* output)
  5854. {
  5855. WOLFSSL* ssl = (WOLFSSL*)data;
  5856. c16toa(ssl->certHashSigAlgoSz, output);
  5857. XMEMCPY(output + OPAQUE16_LEN, ssl->certHashSigAlgo,
  5858. ssl->certHashSigAlgoSz);
  5859. return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
  5860. }
  5861. /* Parse the SignatureAlgorithmsCert extension.
  5862. *
  5863. * ssl The SSL/TLS object.
  5864. * input The buffer with the extension data.
  5865. * length The length of the extension data.
  5866. * returns 0 on success, otherwise failure.
  5867. */
  5868. static int TLSX_SignatureAlgorithmsCert_Parse(WOLFSSL *ssl, const byte* input,
  5869. word16 length, byte isRequest)
  5870. {
  5871. word16 len;
  5872. if (!isRequest)
  5873. return BUFFER_ERROR;
  5874. /* Must contain a length and at least algorithm. */
  5875. if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
  5876. return BUFFER_ERROR;
  5877. ato16(input, &len);
  5878. input += OPAQUE16_LEN;
  5879. /* Algorithm array must fill rest of data. */
  5880. if (length != OPAQUE16_LEN + len)
  5881. return BUFFER_ERROR;
  5882. /* truncate hashSigAlgo list if too long */
  5883. ssl->certHashSigAlgoSz = len;
  5884. if (ssl->certHashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
  5885. WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
  5886. ssl->certHashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
  5887. }
  5888. XMEMCPY(ssl->certHashSigAlgo, input, ssl->certHashSigAlgoSz);
  5889. return 0;
  5890. }
  5891. /* Sets a new SignatureAlgorithmsCert extension into the extension list.
  5892. *
  5893. * extensions The list of extensions.
  5894. * data The extensions specific data.
  5895. * heap The heap used for allocation.
  5896. * returns 0 on success, otherwise failure.
  5897. */
  5898. static int TLSX_SetSignatureAlgorithmsCert(TLSX** extensions,
  5899. const WOLFSSL* data, void* heap)
  5900. {
  5901. if (extensions == NULL)
  5902. return BAD_FUNC_ARG;
  5903. return TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS_CERT, data, heap);
  5904. }
  5905. #define SAC_GET_SIZE TLSX_SignatureAlgorithmsCert_GetSize
  5906. #define SAC_WRITE TLSX_SignatureAlgorithmsCert_Write
  5907. #define SAC_PARSE TLSX_SignatureAlgorithmsCert_Parse
  5908. #endif /* WOLFSSL_TLS13 */
  5909. /******************************************************************************/
  5910. /* Key Share */
  5911. /******************************************************************************/
  5912. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  5913. /* Create a key share entry using named Diffie-Hellman parameters group.
  5914. * Generates a key pair.
  5915. *
  5916. * ssl The SSL/TLS object.
  5917. * kse The key share entry object.
  5918. * returns 0 on success, otherwise failure.
  5919. */
  5920. static int TLSX_KeyShare_GenDhKey(WOLFSSL *ssl, KeyShareEntry* kse)
  5921. {
  5922. int ret = 0;
  5923. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  5924. word32 pSz = 0, pvtSz = 0;
  5925. DhKey* dhKey = (DhKey*)kse->key;
  5926. /* Pick the parameters from the named group. */
  5927. #ifdef HAVE_PUBLIC_FFDHE
  5928. const DhParams* params = NULL;
  5929. switch (kse->group) {
  5930. #ifdef HAVE_FFDHE_2048
  5931. case WOLFSSL_FFDHE_2048:
  5932. params = wc_Dh_ffdhe2048_Get();
  5933. pvtSz = 29;
  5934. break;
  5935. #endif
  5936. #ifdef HAVE_FFDHE_3072
  5937. case WOLFSSL_FFDHE_3072:
  5938. params = wc_Dh_ffdhe3072_Get();
  5939. pvtSz = 34;
  5940. break;
  5941. #endif
  5942. #ifdef HAVE_FFDHE_4096
  5943. case WOLFSSL_FFDHE_4096:
  5944. params = wc_Dh_ffdhe4096_Get();
  5945. pvtSz = 39;
  5946. break;
  5947. #endif
  5948. #ifdef HAVE_FFDHE_6144
  5949. case WOLFSSL_FFDHE_6144:
  5950. params = wc_Dh_ffdhe6144_Get();
  5951. pvtSz = 46;
  5952. break;
  5953. #endif
  5954. #ifdef HAVE_FFDHE_8192
  5955. case WOLFSSL_FFDHE_8192:
  5956. params = wc_Dh_ffdhe8192_Get();
  5957. pvtSz = 52;
  5958. break;
  5959. #endif
  5960. default:
  5961. break;
  5962. }
  5963. if (params == NULL)
  5964. return BAD_FUNC_ARG;
  5965. pSz = params->p_len;
  5966. #else
  5967. pvtSz = wc_DhGetNamedKeyMinSize(kse->group);
  5968. if (pvtSz == 0) {
  5969. return BAD_FUNC_ARG;
  5970. }
  5971. ret = wc_DhGetNamedKeyParamSize(kse->group, &pSz, NULL, NULL);
  5972. if (ret != 0) {
  5973. return BAD_FUNC_ARG;
  5974. }
  5975. #endif
  5976. /* Trigger Key Generation */
  5977. if (kse->pubKey == NULL || kse->privKey == NULL) {
  5978. if (kse->key == NULL) {
  5979. kse->key = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap,
  5980. DYNAMIC_TYPE_DH);
  5981. if (kse->key == NULL)
  5982. return MEMORY_E;
  5983. /* Setup Key */
  5984. ret = wc_InitDhKey_ex((DhKey*)kse->key, ssl->heap, ssl->devId);
  5985. if (ret == 0) {
  5986. dhKey = (DhKey*)kse->key;
  5987. #ifdef HAVE_PUBLIC_FFDHE
  5988. ret = wc_DhSetKey(dhKey, params->p, params->p_len, params->g,
  5989. params->g_len);
  5990. #else
  5991. ret = wc_DhSetNamedKey(dhKey, kse->group);
  5992. #endif
  5993. }
  5994. }
  5995. /* Allocate space for the private and public key */
  5996. if (ret == 0 && kse->pubKey == NULL) {
  5997. kse->pubKey = (byte*)XMALLOC(pSz, ssl->heap,
  5998. DYNAMIC_TYPE_PUBLIC_KEY);
  5999. if (kse->pubKey == NULL)
  6000. ret = MEMORY_E;
  6001. }
  6002. if (ret == 0 && kse->privKey == NULL) {
  6003. kse->privKey = (byte*)XMALLOC(pvtSz, ssl->heap,
  6004. DYNAMIC_TYPE_PRIVATE_KEY);
  6005. if (kse->privKey == NULL)
  6006. ret = MEMORY_E;
  6007. }
  6008. if (ret == 0) {
  6009. #if defined(WOLFSSL_STATIC_EPHEMERAL) && defined(WOLFSSL_DH_EXTRA)
  6010. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_DH, kse->key);
  6011. kse->pubKeyLen = pSz;
  6012. kse->keyLen = pvtSz;
  6013. if (ret == 0) {
  6014. ret = wc_DhExportKeyPair(dhKey,
  6015. (byte*)kse->privKey, &kse->keyLen, /* private */
  6016. kse->pubKey, &kse->pubKeyLen /* public */
  6017. );
  6018. }
  6019. else
  6020. #endif
  6021. {
  6022. /* Generate a new key pair */
  6023. /* For async this is called once and when event is done, the
  6024. * provided buffers will be populated.
  6025. * Final processing is zero pad below. */
  6026. kse->pubKeyLen = pSz;
  6027. kse->keyLen = pvtSz;
  6028. ret = DhGenKeyPair(ssl, dhKey,
  6029. (byte*)kse->privKey, &kse->keyLen, /* private */
  6030. kse->pubKey, &kse->pubKeyLen /* public */
  6031. );
  6032. #ifdef WOLFSSL_ASYNC_CRYPT
  6033. if (ret == WC_PENDING_E) {
  6034. return ret;
  6035. }
  6036. #endif
  6037. }
  6038. }
  6039. }
  6040. if (ret == 0) {
  6041. if (pSz != kse->pubKeyLen) {
  6042. /* Zero pad the front of the public key to match prime "p" size */
  6043. XMEMMOVE(kse->pubKey + pSz - kse->pubKeyLen, kse->pubKey,
  6044. kse->pubKeyLen);
  6045. XMEMSET(kse->pubKey, 0, pSz - kse->pubKeyLen);
  6046. kse->pubKeyLen = pSz;
  6047. }
  6048. if (pvtSz != kse->keyLen) {
  6049. /* Zero pad the front of the private key */
  6050. XMEMMOVE(kse->privKey + pvtSz - kse->keyLen, kse->privKey,
  6051. kse->keyLen);
  6052. XMEMSET(kse->privKey, 0, pvtSz - kse->keyLen);
  6053. kse->keyLen = pvtSz;
  6054. }
  6055. #ifdef WOLFSSL_DEBUG_TLS
  6056. WOLFSSL_MSG("Public DH Key");
  6057. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  6058. #endif
  6059. }
  6060. /* Always release the DH key to free up memory.
  6061. * The DhKey will be setup again in TLSX_KeyShare_ProcessDh */
  6062. if (dhKey != NULL)
  6063. wc_FreeDhKey(dhKey);
  6064. if (kse->key != NULL) {
  6065. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_DH);
  6066. kse->key = NULL;
  6067. }
  6068. if (ret != 0) {
  6069. /* Cleanup on error, otherwise data owned by key share entry */
  6070. if (kse->privKey != NULL) {
  6071. XFREE(kse->privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6072. kse->privKey = NULL;
  6073. }
  6074. if (kse->pubKey != NULL) {
  6075. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6076. kse->pubKey = NULL;
  6077. }
  6078. }
  6079. #else
  6080. (void)ssl;
  6081. (void)kse;
  6082. ret = NOT_COMPILED_IN;
  6083. WOLFSSL_ERROR_VERBOSE(ret);
  6084. #endif
  6085. return ret;
  6086. }
  6087. /* Create a key share entry using X25519 parameters group.
  6088. * Generates a key pair.
  6089. *
  6090. * ssl The SSL/TLS object.
  6091. * kse The key share entry object.
  6092. * returns 0 on success, otherwise failure.
  6093. */
  6094. static int TLSX_KeyShare_GenX25519Key(WOLFSSL *ssl, KeyShareEntry* kse)
  6095. {
  6096. int ret = 0;
  6097. #ifdef HAVE_CURVE25519
  6098. curve25519_key* key = (curve25519_key*)kse->key;
  6099. if (kse->key == NULL) {
  6100. /* Allocate a Curve25519 key to hold private key. */
  6101. kse->key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
  6102. DYNAMIC_TYPE_PRIVATE_KEY);
  6103. if (kse->key == NULL) {
  6104. WOLFSSL_MSG("GenX25519Key memory error");
  6105. return MEMORY_E;
  6106. }
  6107. /* Make an Curve25519 key. */
  6108. ret = wc_curve25519_init_ex((curve25519_key*)kse->key, ssl->heap,
  6109. INVALID_DEVID);
  6110. if (ret == 0) {
  6111. /* setting "key" means okay to call wc_curve25519_free */
  6112. key = (curve25519_key*)kse->key;
  6113. #ifdef WOLFSSL_STATIC_EPHEMERAL
  6114. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_CURVE25519, kse->key);
  6115. if (ret != 0)
  6116. #endif
  6117. {
  6118. ret = wc_curve25519_make_key(ssl->rng, CURVE25519_KEYSIZE, key);
  6119. }
  6120. }
  6121. }
  6122. if (ret == 0 && kse->pubKey == NULL) {
  6123. /* Allocate space for the public key. */
  6124. kse->pubKey = (byte*)XMALLOC(CURVE25519_KEYSIZE, ssl->heap,
  6125. DYNAMIC_TYPE_PUBLIC_KEY);
  6126. if (kse->pubKey == NULL) {
  6127. WOLFSSL_MSG("GenX25519Key pub memory error");
  6128. ret = MEMORY_E;
  6129. }
  6130. }
  6131. if (ret == 0) {
  6132. /* Export Curve25519 public key. */
  6133. kse->pubKeyLen = CURVE25519_KEYSIZE;
  6134. if (wc_curve25519_export_public_ex(key, kse->pubKey, &kse->pubKeyLen,
  6135. EC25519_LITTLE_ENDIAN) != 0) {
  6136. ret = ECC_EXPORT_ERROR;
  6137. WOLFSSL_ERROR_VERBOSE(ret);
  6138. }
  6139. kse->pubKeyLen = CURVE25519_KEYSIZE; /* always CURVE25519_KEYSIZE */
  6140. }
  6141. #ifdef WOLFSSL_DEBUG_TLS
  6142. if (ret == 0) {
  6143. WOLFSSL_MSG("Public Curve25519 Key");
  6144. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  6145. }
  6146. #endif
  6147. if (ret != 0) {
  6148. /* Data owned by key share entry otherwise. */
  6149. if (kse->pubKey != NULL) {
  6150. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6151. kse->pubKey = NULL;
  6152. }
  6153. if (key != NULL)
  6154. wc_curve25519_free(key);
  6155. if (kse->key != NULL) {
  6156. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6157. kse->key = NULL;
  6158. }
  6159. }
  6160. #else
  6161. (void)ssl;
  6162. (void)kse;
  6163. ret = NOT_COMPILED_IN;
  6164. WOLFSSL_ERROR_VERBOSE(ret);
  6165. #endif /* HAVE_CURVE25519 */
  6166. return ret;
  6167. }
  6168. /* Create a key share entry using X448 parameters group.
  6169. * Generates a key pair.
  6170. *
  6171. * ssl The SSL/TLS object.
  6172. * kse The key share entry object.
  6173. * returns 0 on success, otherwise failure.
  6174. */
  6175. static int TLSX_KeyShare_GenX448Key(WOLFSSL *ssl, KeyShareEntry* kse)
  6176. {
  6177. int ret = 0;
  6178. #ifdef HAVE_CURVE448
  6179. curve448_key* key = (curve448_key*)kse->key;
  6180. if (kse->key == NULL) {
  6181. /* Allocate a Curve448 key to hold private key. */
  6182. kse->key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
  6183. DYNAMIC_TYPE_PRIVATE_KEY);
  6184. if (kse->key == NULL) {
  6185. WOLFSSL_MSG("GenX448Key memory error");
  6186. return MEMORY_E;
  6187. }
  6188. /* Make an Curve448 key. */
  6189. ret = wc_curve448_init((curve448_key*)kse->key);
  6190. if (ret == 0) {
  6191. key = (curve448_key*)kse->key;
  6192. #ifdef WOLFSSL_STATIC_EPHEMERAL
  6193. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_CURVE448, kse->key);
  6194. if (ret != 0)
  6195. #endif
  6196. {
  6197. ret = wc_curve448_make_key(ssl->rng, CURVE448_KEY_SIZE, key);
  6198. }
  6199. }
  6200. }
  6201. if (ret == 0 && kse->pubKey == NULL) {
  6202. /* Allocate space for the public key. */
  6203. kse->pubKey = (byte*)XMALLOC(CURVE448_KEY_SIZE, ssl->heap,
  6204. DYNAMIC_TYPE_PUBLIC_KEY);
  6205. if (kse->pubKey == NULL) {
  6206. WOLFSSL_MSG("GenX448Key pub memory error");
  6207. ret = MEMORY_E;
  6208. }
  6209. }
  6210. if (ret == 0) {
  6211. /* Export Curve448 public key. */
  6212. kse->pubKeyLen = CURVE448_KEY_SIZE;
  6213. if (wc_curve448_export_public_ex(key, kse->pubKey, &kse->pubKeyLen,
  6214. EC448_LITTLE_ENDIAN) != 0) {
  6215. ret = ECC_EXPORT_ERROR;
  6216. }
  6217. kse->pubKeyLen = CURVE448_KEY_SIZE; /* always CURVE448_KEY_SIZE */
  6218. }
  6219. #ifdef WOLFSSL_DEBUG_TLS
  6220. if (ret == 0) {
  6221. WOLFSSL_MSG("Public Curve448 Key");
  6222. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  6223. }
  6224. #endif
  6225. if (ret != 0) {
  6226. /* Data owned by key share entry otherwise. */
  6227. if (kse->pubKey != NULL) {
  6228. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6229. kse->pubKey = NULL;
  6230. }
  6231. if (key != NULL)
  6232. wc_curve448_free(key);
  6233. if (kse->key != NULL) {
  6234. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6235. kse->key = NULL;
  6236. }
  6237. }
  6238. #else
  6239. (void)ssl;
  6240. (void)kse;
  6241. ret = NOT_COMPILED_IN;
  6242. WOLFSSL_ERROR_VERBOSE(ret);
  6243. #endif /* HAVE_CURVE448 */
  6244. return ret;
  6245. }
  6246. /* Create a key share entry using named elliptic curve parameters group.
  6247. * Generates a key pair.
  6248. *
  6249. * ssl The SSL/TLS object.
  6250. * kse The key share entry object.
  6251. * returns 0 on success, otherwise failure.
  6252. */
  6253. static int TLSX_KeyShare_GenEccKey(WOLFSSL *ssl, KeyShareEntry* kse)
  6254. {
  6255. int ret = 0;
  6256. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT)
  6257. word32 keySize = 0;
  6258. word16 curveId = (word16) ECC_CURVE_INVALID;
  6259. ecc_key* eccKey = (ecc_key*)kse->key;
  6260. /* TODO: [TLS13] Get key sizes using wc_ecc_get_curve_size_from_id. */
  6261. /* Translate named group to a curve id. */
  6262. switch (kse->group) {
  6263. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  6264. #ifndef NO_ECC_SECP
  6265. case WOLFSSL_ECC_SECP256R1:
  6266. curveId = ECC_SECP256R1;
  6267. keySize = 32;
  6268. break;
  6269. #endif /* !NO_ECC_SECP */
  6270. #ifdef WOLFSSL_SM2
  6271. case WOLFSSL_ECC_SM2P256V1:
  6272. curveId = ECC_SM2P256V1;
  6273. keySize = 32;
  6274. break;
  6275. #endif /* !NO_ECC_SECP */
  6276. #endif
  6277. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  6278. #ifndef NO_ECC_SECP
  6279. case WOLFSSL_ECC_SECP384R1:
  6280. curveId = ECC_SECP384R1;
  6281. keySize = 48;
  6282. break;
  6283. #endif /* !NO_ECC_SECP */
  6284. #endif
  6285. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  6286. #ifndef NO_ECC_SECP
  6287. case WOLFSSL_ECC_SECP521R1:
  6288. curveId = ECC_SECP521R1;
  6289. keySize = 66;
  6290. break;
  6291. #endif /* !NO_ECC_SECP */
  6292. #endif
  6293. default:
  6294. WOLFSSL_ERROR_VERBOSE(BAD_FUNC_ARG);
  6295. return BAD_FUNC_ARG;
  6296. }
  6297. if (kse->key == NULL) {
  6298. /* Allocate an ECC key to hold private key. */
  6299. kse->key = (byte*)XMALLOC(sizeof(ecc_key), ssl->heap, DYNAMIC_TYPE_ECC);
  6300. if (kse->key == NULL) {
  6301. WOLFSSL_MSG_EX("Failed to allocate %d bytes, ssl->heap: %p",
  6302. (int)sizeof(ecc_key), (wc_ptr_t)ssl->heap);
  6303. WOLFSSL_MSG("EccTempKey Memory error!");
  6304. return MEMORY_E;
  6305. }
  6306. /* Initialize an ECC key struct for the ephemeral key */
  6307. ret = wc_ecc_init_ex((ecc_key*)kse->key, ssl->heap, ssl->devId);
  6308. if (ret == 0) {
  6309. kse->keyLen = keySize;
  6310. kse->pubKeyLen = keySize * 2 + 1;
  6311. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  6312. ret = tsip_Tls13GenEccKeyPair(ssl, kse);
  6313. if (ret != CRYPTOCB_UNAVAILABLE) {
  6314. return ret;
  6315. }
  6316. #endif
  6317. /* setting eccKey means okay to call wc_ecc_free */
  6318. eccKey = (ecc_key*)kse->key;
  6319. #ifdef WOLFSSL_STATIC_EPHEMERAL
  6320. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_ECDH, kse->key);
  6321. if (ret != 0)
  6322. #endif
  6323. {
  6324. /* set curve info for EccMakeKey "peer" info */
  6325. ret = wc_ecc_set_curve(eccKey, kse->keyLen, curveId);
  6326. if (ret == 0) {
  6327. #ifdef WOLFSSL_ASYNC_CRYPT
  6328. /* Detect when private key generation is done */
  6329. if (ssl->error == WC_PENDING_E &&
  6330. eccKey->type == ECC_PRIVATEKEY) {
  6331. ret = 0; /* ECC Key Generation is done */
  6332. }
  6333. else
  6334. #endif
  6335. {
  6336. /* Generate ephemeral ECC key */
  6337. /* For async this is called once and when event is done, the
  6338. * provided buffers in key be populated.
  6339. * Final processing is x963 key export below. */
  6340. ret = EccMakeKey(ssl, eccKey, eccKey);
  6341. }
  6342. }
  6343. #ifdef WOLFSSL_ASYNC_CRYPT
  6344. if (ret == WC_PENDING_E)
  6345. return ret;
  6346. #endif
  6347. }
  6348. }
  6349. }
  6350. if (ret == 0 && kse->pubKey == NULL) {
  6351. /* Allocate space for the public key */
  6352. kse->pubKey = (byte*)XMALLOC(kse->pubKeyLen, ssl->heap,
  6353. DYNAMIC_TYPE_PUBLIC_KEY);
  6354. if (kse->pubKey == NULL) {
  6355. WOLFSSL_MSG("Key data Memory error");
  6356. ret = MEMORY_E;
  6357. }
  6358. }
  6359. if (ret == 0) {
  6360. XMEMSET(kse->pubKey, 0, kse->pubKeyLen);
  6361. /* Export public key. */
  6362. PRIVATE_KEY_UNLOCK();
  6363. if (wc_ecc_export_x963(eccKey, kse->pubKey, &kse->pubKeyLen) != 0) {
  6364. ret = ECC_EXPORT_ERROR;
  6365. WOLFSSL_ERROR_VERBOSE(ret);
  6366. }
  6367. PRIVATE_KEY_LOCK();
  6368. }
  6369. #ifdef WOLFSSL_DEBUG_TLS
  6370. if (ret == 0) {
  6371. WOLFSSL_MSG("Public ECC Key");
  6372. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  6373. }
  6374. #endif
  6375. if (ret != 0) {
  6376. /* Cleanup on error, otherwise data owned by key share entry */
  6377. if (kse->pubKey != NULL) {
  6378. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6379. kse->pubKey = NULL;
  6380. }
  6381. if (eccKey != NULL)
  6382. wc_ecc_free(eccKey);
  6383. if (kse->key != NULL) {
  6384. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6385. kse->key = NULL;
  6386. }
  6387. }
  6388. #else
  6389. (void)ssl;
  6390. (void)kse;
  6391. ret = NOT_COMPILED_IN;
  6392. WOLFSSL_ERROR_VERBOSE(ret);
  6393. #endif /* HAVE_ECC && HAVE_ECC_KEY_EXPORT */
  6394. return ret;
  6395. }
  6396. #ifdef HAVE_PQC
  6397. static int kyber_id2type(int id, int *type)
  6398. {
  6399. int ret = 0;
  6400. switch (id) {
  6401. #ifdef WOLFSSL_KYBER512
  6402. case WOLFSSL_KYBER_LEVEL1:
  6403. *type = KYBER512;
  6404. break;
  6405. #endif
  6406. #ifdef WOLFSSL_KYBER768
  6407. case WOLFSSL_KYBER_LEVEL3:
  6408. *type = KYBER768;
  6409. break;
  6410. #endif
  6411. #ifdef WOLFSSL_KYBER1024
  6412. case WOLFSSL_KYBER_LEVEL5:
  6413. *type = KYBER1024;
  6414. break;
  6415. #endif
  6416. default:
  6417. ret = NOT_COMPILED_IN;
  6418. break;
  6419. }
  6420. return ret;
  6421. }
  6422. typedef struct PqcHybridMapping {
  6423. int hybrid;
  6424. int ecc;
  6425. int pqc;
  6426. } PqcHybridMapping;
  6427. static const PqcHybridMapping pqc_hybrid_mapping[] = {
  6428. {.hybrid = WOLFSSL_P256_KYBER_LEVEL1, .ecc = WOLFSSL_ECC_SECP256R1,
  6429. .pqc = WOLFSSL_KYBER_LEVEL1},
  6430. {.hybrid = WOLFSSL_P384_KYBER_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  6431. .pqc = WOLFSSL_KYBER_LEVEL3},
  6432. {.hybrid = WOLFSSL_P521_KYBER_LEVEL5, .ecc = WOLFSSL_ECC_SECP521R1,
  6433. .pqc = WOLFSSL_KYBER_LEVEL5},
  6434. {.hybrid = 0, .ecc = 0, .pqc = 0}
  6435. };
  6436. /* This will map an ecc-pqs hybrid group into its ecc group and pqc kem group.
  6437. * If it cannot find a mapping then *pqc is set to group. ecc is optional. */
  6438. static void findEccPqc(int *ecc, int *pqc, int group)
  6439. {
  6440. int i;
  6441. if (pqc == NULL) {
  6442. return;
  6443. }
  6444. *pqc = 0;
  6445. if (ecc != NULL) {
  6446. *ecc = 0;
  6447. }
  6448. for (i = 0; pqc_hybrid_mapping[i].hybrid != 0; i++) {
  6449. if (pqc_hybrid_mapping[i].hybrid == group) {
  6450. *pqc = pqc_hybrid_mapping[i].pqc;
  6451. if (ecc != NULL) {
  6452. *ecc = pqc_hybrid_mapping[i].ecc;
  6453. }
  6454. break;
  6455. }
  6456. }
  6457. if (*pqc == 0) {
  6458. /* It is not a hybrid, so maybe its simple. */
  6459. *pqc = group;
  6460. }
  6461. }
  6462. /* Create a key share entry using liboqs parameters group.
  6463. * Generates a key pair.
  6464. *
  6465. * ssl The SSL/TLS object.
  6466. * kse The key share entry object.
  6467. * returns 0 on success, otherwise failure.
  6468. */
  6469. static int TLSX_KeyShare_GenPqcKey(WOLFSSL *ssl, KeyShareEntry* kse)
  6470. {
  6471. int ret = 0;
  6472. int type = 0;
  6473. KyberKey kem[1];
  6474. byte* pubKey = NULL;
  6475. byte* privKey = NULL;
  6476. KeyShareEntry *ecc_kse = NULL;
  6477. int oqs_group = 0;
  6478. int ecc_group = 0;
  6479. word32 privSz = 0;
  6480. word32 pubSz = 0;
  6481. /* This gets called twice. Once during parsing of the key share and once
  6482. * during the population of the extension. No need to do work the second
  6483. * time. Just return success if its already been done. */
  6484. if (kse->pubKey != NULL) {
  6485. return ret;
  6486. }
  6487. findEccPqc(&ecc_group, &oqs_group, kse->group);
  6488. ret = kyber_id2type(oqs_group, &type);
  6489. if (ret == NOT_COMPILED_IN) {
  6490. WOLFSSL_MSG("Invalid Kyber algorithm specified.");
  6491. ret = BAD_FUNC_ARG;
  6492. }
  6493. if (ret == 0) {
  6494. ret = wc_KyberKey_Init(type, kem, ssl->heap, ssl->devId);
  6495. if (ret != 0) {
  6496. WOLFSSL_MSG("Failed to initialize Kyber Key.");
  6497. }
  6498. }
  6499. if (ret == 0) {
  6500. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  6501. DYNAMIC_TYPE_TLSX);
  6502. if (ecc_kse == NULL) {
  6503. WOLFSSL_MSG("ecc_kse memory allocation failure");
  6504. ret = MEMORY_ERROR;
  6505. }
  6506. }
  6507. if (ret == 0) {
  6508. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  6509. ret = wc_KyberKey_PrivateKeySize(kem, &privSz);
  6510. }
  6511. if (ret == 0) {
  6512. ret = wc_KyberKey_PublicKeySize(kem, &pubSz);
  6513. }
  6514. if (ret == 0 && ecc_group != 0) {
  6515. ecc_kse->group = ecc_group;
  6516. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  6517. /* If fail, no error message, TLSX_KeyShare_GenEccKey will do it. */
  6518. }
  6519. if (ret == 0) {
  6520. pubKey = (byte*)XMALLOC(ecc_kse->pubKeyLen + pubSz, ssl->heap,
  6521. DYNAMIC_TYPE_PUBLIC_KEY);
  6522. if (pubKey == NULL) {
  6523. WOLFSSL_MSG("pubkey memory allocation failure");
  6524. ret = MEMORY_ERROR;
  6525. }
  6526. }
  6527. if (ret == 0) {
  6528. privKey = (byte*)XMALLOC(privSz, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6529. if (privKey == NULL) {
  6530. WOLFSSL_MSG("privkey memory allocation failure");
  6531. ret = MEMORY_ERROR;
  6532. }
  6533. }
  6534. if (ret == 0) {
  6535. ret = wc_KyberKey_MakeKey(kem, ssl->rng);
  6536. if (ret != 0) {
  6537. WOLFSSL_MSG("Kyber keygen failure");
  6538. }
  6539. }
  6540. if (ret == 0) {
  6541. ret = wc_KyberKey_EncodePublicKey(kem, pubKey + ecc_kse->pubKeyLen,
  6542. pubSz);
  6543. }
  6544. if (ret == 0) {
  6545. ret = wc_KyberKey_EncodePrivateKey(kem, privKey, privSz);
  6546. }
  6547. if (ret == 0) {
  6548. if (ecc_kse->pubKeyLen > 0)
  6549. XMEMCPY(pubKey, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  6550. kse->pubKey = pubKey;
  6551. kse->pubKeyLen = ecc_kse->pubKeyLen + pubSz;
  6552. pubKey = NULL;
  6553. /* Note we are saving the OQS private key and ECC private key
  6554. * separately. That's because the ECC private key is not simply a
  6555. * buffer. Its is an ecc_key struct. Typically do not need the private
  6556. * key size, but will need to zero it out upon freeing. */
  6557. kse->privKey = privKey;
  6558. privKey = NULL;
  6559. kse->privKeyLen = privSz;
  6560. kse->key = ecc_kse->key;
  6561. ecc_kse->key = NULL;
  6562. }
  6563. #ifdef WOLFSSL_DEBUG_TLS
  6564. WOLFSSL_MSG("Public Kyber Key");
  6565. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen );
  6566. #endif
  6567. wc_KyberKey_Free(kem);
  6568. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  6569. if (pubKey != NULL)
  6570. XFREE(pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6571. if (privKey != NULL)
  6572. XFREE(privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6573. return ret;
  6574. }
  6575. #endif /* HAVE_PQC */
  6576. /* Generate a secret/key using the key share entry.
  6577. *
  6578. * ssl The SSL/TLS object.
  6579. * kse The key share entry holding peer data.
  6580. */
  6581. int TLSX_KeyShare_GenKey(WOLFSSL *ssl, KeyShareEntry *kse)
  6582. {
  6583. int ret;
  6584. /* Named FFDHE groups have a bit set to identify them. */
  6585. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(kse->group))
  6586. ret = TLSX_KeyShare_GenDhKey(ssl, kse);
  6587. else if (kse->group == WOLFSSL_ECC_X25519)
  6588. ret = TLSX_KeyShare_GenX25519Key(ssl, kse);
  6589. else if (kse->group == WOLFSSL_ECC_X448)
  6590. ret = TLSX_KeyShare_GenX448Key(ssl, kse);
  6591. #ifdef HAVE_PQC
  6592. else if (WOLFSSL_NAMED_GROUP_IS_PQC(kse->group))
  6593. ret = TLSX_KeyShare_GenPqcKey(ssl, kse);
  6594. #endif
  6595. else
  6596. ret = TLSX_KeyShare_GenEccKey(ssl, kse);
  6597. #ifdef WOLFSSL_ASYNC_CRYPT
  6598. kse->lastRet = ret;
  6599. #endif
  6600. return ret;
  6601. }
  6602. /* Free the key share dynamic data.
  6603. *
  6604. * list The linked list of key share entry objects.
  6605. * heap The heap used for allocation.
  6606. */
  6607. static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap)
  6608. {
  6609. KeyShareEntry* current;
  6610. while ((current = list) != NULL) {
  6611. list = current->next;
  6612. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(current->group)) {
  6613. #ifndef NO_DH
  6614. wc_FreeDhKey((DhKey*)current->key);
  6615. #endif
  6616. }
  6617. else if (current->group == WOLFSSL_ECC_X25519) {
  6618. #ifdef HAVE_CURVE25519
  6619. wc_curve25519_free((curve25519_key*)current->key);
  6620. #endif
  6621. }
  6622. else if (current->group == WOLFSSL_ECC_X448) {
  6623. #ifdef HAVE_CURVE448
  6624. wc_curve448_free((curve448_key*)current->key);
  6625. #endif
  6626. }
  6627. #ifdef HAVE_PQC
  6628. else if (WOLFSSL_NAMED_GROUP_IS_PQC(current->group)) {
  6629. if (current->key != NULL) {
  6630. ForceZero((byte*)current->key, current->keyLen);
  6631. }
  6632. if (current->pubKey != NULL) {
  6633. XFREE(current->pubKey, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6634. current->pubKey = NULL;
  6635. }
  6636. if (current->privKey != NULL) {
  6637. ForceZero(current->privKey, current->privKeyLen);
  6638. XFREE(current->privKey, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6639. current->privKey = NULL;
  6640. }
  6641. }
  6642. #endif
  6643. else {
  6644. #ifdef HAVE_ECC
  6645. wc_ecc_free((ecc_key*)current->key);
  6646. #endif
  6647. }
  6648. XFREE(current->key, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6649. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  6650. XFREE(current->privKey, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6651. #endif
  6652. XFREE(current->pubKey, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6653. XFREE(current->ke, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6654. XFREE(current, heap, DYNAMIC_TYPE_TLSX);
  6655. }
  6656. (void)heap;
  6657. }
  6658. /* Get the size of the encoded key share extension.
  6659. *
  6660. * list The linked list of key share extensions.
  6661. * msgType The type of the message this extension is being written into.
  6662. * returns the number of bytes of the encoded key share extension.
  6663. */
  6664. static word16 TLSX_KeyShare_GetSize(KeyShareEntry* list, byte msgType)
  6665. {
  6666. word16 len = 0;
  6667. byte isRequest = (msgType == client_hello);
  6668. KeyShareEntry* current;
  6669. /* The named group the server wants to use. */
  6670. if (msgType == hello_retry_request)
  6671. return OPAQUE16_LEN;
  6672. /* List of key exchange groups. */
  6673. if (isRequest)
  6674. len += OPAQUE16_LEN;
  6675. while ((current = list) != NULL) {
  6676. list = current->next;
  6677. if (!isRequest && current->pubKey == NULL)
  6678. continue;
  6679. len += (word16)(KE_GROUP_LEN + OPAQUE16_LEN + current->pubKeyLen);
  6680. }
  6681. return len;
  6682. }
  6683. /* Writes the key share extension into the output buffer.
  6684. * Assumes that the the output buffer is big enough to hold data.
  6685. *
  6686. * list The linked list of key share entries.
  6687. * output The buffer to write into.
  6688. * msgType The type of the message this extension is being written into.
  6689. * returns the number of bytes written into the buffer.
  6690. */
  6691. static word16 TLSX_KeyShare_Write(KeyShareEntry* list, byte* output,
  6692. byte msgType)
  6693. {
  6694. word16 i = 0;
  6695. byte isRequest = (msgType == client_hello);
  6696. KeyShareEntry* current;
  6697. if (msgType == hello_retry_request) {
  6698. c16toa(list->group, output);
  6699. return OPAQUE16_LEN;
  6700. }
  6701. /* ClientHello has a list but ServerHello is only the chosen. */
  6702. if (isRequest)
  6703. i += OPAQUE16_LEN;
  6704. /* Write out all in the list. */
  6705. while ((current = list) != NULL) {
  6706. list = current->next;
  6707. if (!isRequest && current->pubKey == NULL)
  6708. continue;
  6709. c16toa(current->group, &output[i]);
  6710. i += KE_GROUP_LEN;
  6711. c16toa((word16)(current->pubKeyLen), &output[i]);
  6712. i += OPAQUE16_LEN;
  6713. XMEMCPY(&output[i], current->pubKey, current->pubKeyLen);
  6714. i += (word16)current->pubKeyLen;
  6715. }
  6716. /* Write the length of the list if required. */
  6717. if (isRequest)
  6718. c16toa(i - OPAQUE16_LEN, output);
  6719. return i;
  6720. }
  6721. /* Process the DH key share extension on the client side.
  6722. *
  6723. * ssl The SSL/TLS object.
  6724. * keyShareEntry The key share entry object to use to calculate shared secret.
  6725. * returns 0 on success and other values indicate failure.
  6726. */
  6727. static int TLSX_KeyShare_ProcessDh(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6728. {
  6729. int ret = 0;
  6730. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  6731. word32 pSz = 0;
  6732. DhKey* dhKey = (DhKey*)keyShareEntry->key;
  6733. #ifdef HAVE_PUBLIC_FFDHE
  6734. const DhParams* params = NULL;
  6735. switch (keyShareEntry->group) {
  6736. #ifdef HAVE_FFDHE_2048
  6737. case WOLFSSL_FFDHE_2048:
  6738. params = wc_Dh_ffdhe2048_Get();
  6739. break;
  6740. #endif
  6741. #ifdef HAVE_FFDHE_3072
  6742. case WOLFSSL_FFDHE_3072:
  6743. params = wc_Dh_ffdhe3072_Get();
  6744. break;
  6745. #endif
  6746. #ifdef HAVE_FFDHE_4096
  6747. case WOLFSSL_FFDHE_4096:
  6748. params = wc_Dh_ffdhe4096_Get();
  6749. break;
  6750. #endif
  6751. #ifdef HAVE_FFDHE_6144
  6752. case WOLFSSL_FFDHE_6144:
  6753. params = wc_Dh_ffdhe6144_Get();
  6754. break;
  6755. #endif
  6756. #ifdef HAVE_FFDHE_8192
  6757. case WOLFSSL_FFDHE_8192:
  6758. params = wc_Dh_ffdhe8192_Get();
  6759. break;
  6760. #endif
  6761. default:
  6762. break;
  6763. }
  6764. if (params == NULL) {
  6765. WOLFSSL_ERROR_VERBOSE(PEER_KEY_ERROR);
  6766. return PEER_KEY_ERROR;
  6767. }
  6768. pSz = params->p_len;
  6769. #else
  6770. ret = wc_DhGetNamedKeyParamSize(keyShareEntry->group, &pSz, NULL, NULL);
  6771. if (ret != 0 || pSz == 0) {
  6772. WOLFSSL_ERROR_VERBOSE(PEER_KEY_ERROR);
  6773. return PEER_KEY_ERROR;
  6774. }
  6775. #endif
  6776. /* if DhKey is not setup, do it now */
  6777. if (keyShareEntry->key == NULL) {
  6778. keyShareEntry->key = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap,
  6779. DYNAMIC_TYPE_DH);
  6780. if (keyShareEntry->key == NULL)
  6781. return MEMORY_E;
  6782. /* Setup Key */
  6783. ret = wc_InitDhKey_ex((DhKey*)keyShareEntry->key, ssl->heap, ssl->devId);
  6784. if (ret == 0) {
  6785. dhKey = (DhKey*)keyShareEntry->key;
  6786. /* Set key */
  6787. #ifdef HAVE_PUBLIC_FFDHE
  6788. ret = wc_DhSetKey(dhKey, params->p, params->p_len, params->g,
  6789. params->g_len);
  6790. #else
  6791. ret = wc_DhSetNamedKey(dhKey, keyShareEntry->group);
  6792. #endif
  6793. }
  6794. }
  6795. if (ret == 0
  6796. #ifdef WOLFSSL_ASYNC_CRYPT
  6797. && keyShareEntry->lastRet == 0 /* don't enter here if WC_PENDING_E */
  6798. #endif
  6799. ) {
  6800. #ifdef WOLFSSL_DEBUG_TLS
  6801. WOLFSSL_MSG("Peer DH Key");
  6802. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6803. #endif
  6804. ssl->options.dhKeySz = (word16)pSz;
  6805. /* Derive secret from private key and peer's public key. */
  6806. ret = DhAgree(ssl, dhKey,
  6807. (const byte*)keyShareEntry->privKey, keyShareEntry->keyLen, /* our private */
  6808. keyShareEntry->ke, keyShareEntry->keLen, /* peer's public key */
  6809. ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz, /* secret */
  6810. NULL, 0
  6811. );
  6812. #ifdef WOLFSSL_ASYNC_CRYPT
  6813. if (ret == WC_PENDING_E) {
  6814. return ret;
  6815. }
  6816. #endif
  6817. }
  6818. /* RFC 8446 Section 7.4.1:
  6819. * ... left-padded with zeros up to the size of the prime. ...
  6820. */
  6821. if (ret == 0 && (word32)ssl->options.dhKeySz > ssl->arrays->preMasterSz) {
  6822. word32 diff = (word32)ssl->options.dhKeySz - ssl->arrays->preMasterSz;
  6823. XMEMMOVE(ssl->arrays->preMasterSecret + diff,
  6824. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
  6825. XMEMSET(ssl->arrays->preMasterSecret, 0, diff);
  6826. ssl->arrays->preMasterSz = ssl->options.dhKeySz;
  6827. }
  6828. /* done with key share, release resources */
  6829. if (dhKey)
  6830. wc_FreeDhKey(dhKey);
  6831. if (keyShareEntry->key) {
  6832. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_DH);
  6833. keyShareEntry->key = NULL;
  6834. }
  6835. if (keyShareEntry->privKey != NULL) {
  6836. XFREE(keyShareEntry->privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6837. keyShareEntry->privKey = NULL;
  6838. }
  6839. if (keyShareEntry->pubKey != NULL) {
  6840. XFREE(keyShareEntry->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6841. keyShareEntry->pubKey = NULL;
  6842. }
  6843. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6844. keyShareEntry->ke = NULL;
  6845. #else
  6846. (void)ssl;
  6847. (void)keyShareEntry;
  6848. ret = PEER_KEY_ERROR;
  6849. WOLFSSL_ERROR_VERBOSE(ret);
  6850. #endif
  6851. return ret;
  6852. }
  6853. /* Process the X25519 key share extension on the client side.
  6854. *
  6855. * ssl The SSL/TLS object.
  6856. * keyShareEntry The key share entry object to use to calculate shared secret.
  6857. * returns 0 on success and other values indicate failure.
  6858. */
  6859. static int TLSX_KeyShare_ProcessX25519(WOLFSSL* ssl,
  6860. KeyShareEntry* keyShareEntry)
  6861. {
  6862. int ret;
  6863. #ifdef HAVE_CURVE25519
  6864. curve25519_key* key = (curve25519_key*)keyShareEntry->key;
  6865. curve25519_key* peerX25519Key;
  6866. #ifdef HAVE_ECC
  6867. if (ssl->peerEccKey != NULL) {
  6868. wc_ecc_free(ssl->peerEccKey);
  6869. ssl->peerEccKey = NULL;
  6870. ssl->peerEccKeyPresent = 0;
  6871. }
  6872. #endif
  6873. peerX25519Key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
  6874. DYNAMIC_TYPE_TLSX);
  6875. if (peerX25519Key == NULL) {
  6876. WOLFSSL_MSG("PeerEccKey Memory error");
  6877. return MEMORY_ERROR;
  6878. }
  6879. ret = wc_curve25519_init(peerX25519Key);
  6880. if (ret != 0) {
  6881. XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6882. return ret;
  6883. }
  6884. #ifdef WOLFSSL_DEBUG_TLS
  6885. WOLFSSL_MSG("Peer Curve25519 Key");
  6886. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6887. #endif
  6888. if (wc_curve25519_check_public(keyShareEntry->ke, keyShareEntry->keLen,
  6889. EC25519_LITTLE_ENDIAN) != 0) {
  6890. ret = ECC_PEERKEY_ERROR;
  6891. WOLFSSL_ERROR_VERBOSE(ret);
  6892. }
  6893. if (ret == 0) {
  6894. if (wc_curve25519_import_public_ex(keyShareEntry->ke,
  6895. keyShareEntry->keLen, peerX25519Key,
  6896. EC25519_LITTLE_ENDIAN) != 0) {
  6897. ret = ECC_PEERKEY_ERROR;
  6898. WOLFSSL_ERROR_VERBOSE(ret);
  6899. }
  6900. }
  6901. if (ret == 0) {
  6902. ssl->ecdhCurveOID = ECC_X25519_OID;
  6903. ret = wc_curve25519_shared_secret_ex(key, peerX25519Key,
  6904. ssl->arrays->preMasterSecret,
  6905. &ssl->arrays->preMasterSz,
  6906. EC25519_LITTLE_ENDIAN);
  6907. }
  6908. wc_curve25519_free(peerX25519Key);
  6909. XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6910. wc_curve25519_free((curve25519_key*)keyShareEntry->key);
  6911. if (keyShareEntry->key != NULL) {
  6912. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6913. keyShareEntry->key = NULL;
  6914. }
  6915. #else
  6916. (void)ssl;
  6917. (void)keyShareEntry;
  6918. ret = PEER_KEY_ERROR;
  6919. WOLFSSL_ERROR_VERBOSE(ret);
  6920. #endif /* HAVE_CURVE25519 */
  6921. return ret;
  6922. }
  6923. /* Process the X448 key share extension on the client side.
  6924. *
  6925. * ssl The SSL/TLS object.
  6926. * keyShareEntry The key share entry object to use to calculate shared secret.
  6927. * returns 0 on success and other values indicate failure.
  6928. */
  6929. static int TLSX_KeyShare_ProcessX448(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6930. {
  6931. int ret;
  6932. #ifdef HAVE_CURVE448
  6933. curve448_key* key = (curve448_key*)keyShareEntry->key;
  6934. curve448_key* peerX448Key;
  6935. #ifdef HAVE_ECC
  6936. if (ssl->peerEccKey != NULL) {
  6937. wc_ecc_free(ssl->peerEccKey);
  6938. ssl->peerEccKey = NULL;
  6939. ssl->peerEccKeyPresent = 0;
  6940. }
  6941. #endif
  6942. peerX448Key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
  6943. DYNAMIC_TYPE_TLSX);
  6944. if (peerX448Key == NULL) {
  6945. WOLFSSL_MSG("PeerEccKey Memory error");
  6946. return MEMORY_ERROR;
  6947. }
  6948. ret = wc_curve448_init(peerX448Key);
  6949. if (ret != 0) {
  6950. XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6951. return ret;
  6952. }
  6953. #ifdef WOLFSSL_DEBUG_TLS
  6954. WOLFSSL_MSG("Peer Curve448 Key");
  6955. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6956. #endif
  6957. if (wc_curve448_check_public(keyShareEntry->ke, keyShareEntry->keLen,
  6958. EC448_LITTLE_ENDIAN) != 0) {
  6959. ret = ECC_PEERKEY_ERROR;
  6960. WOLFSSL_ERROR_VERBOSE(ret);
  6961. }
  6962. if (ret == 0) {
  6963. if (wc_curve448_import_public_ex(keyShareEntry->ke,
  6964. keyShareEntry->keLen, peerX448Key,
  6965. EC448_LITTLE_ENDIAN) != 0) {
  6966. ret = ECC_PEERKEY_ERROR;
  6967. WOLFSSL_ERROR_VERBOSE(ret);
  6968. }
  6969. }
  6970. if (ret == 0) {
  6971. ssl->ecdhCurveOID = ECC_X448_OID;
  6972. ret = wc_curve448_shared_secret_ex(key, peerX448Key,
  6973. ssl->arrays->preMasterSecret,
  6974. &ssl->arrays->preMasterSz,
  6975. EC448_LITTLE_ENDIAN);
  6976. }
  6977. wc_curve448_free(peerX448Key);
  6978. XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6979. wc_curve448_free((curve448_key*)keyShareEntry->key);
  6980. if (keyShareEntry->key != NULL) {
  6981. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6982. keyShareEntry->key = NULL;
  6983. }
  6984. #else
  6985. (void)ssl;
  6986. (void)keyShareEntry;
  6987. ret = PEER_KEY_ERROR;
  6988. WOLFSSL_ERROR_VERBOSE(ret);
  6989. #endif /* HAVE_CURVE448 */
  6990. return ret;
  6991. }
  6992. /* Process the ECC key share extension on the client side.
  6993. *
  6994. * ssl The SSL/TLS object.
  6995. * keyShareEntry The key share entry object to use to calculate shared secret.
  6996. * returns 0 on success and other values indicate failure.
  6997. */
  6998. static int TLSX_KeyShare_ProcessEcc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6999. {
  7000. int ret = 0;
  7001. #ifdef HAVE_ECC
  7002. int curveId = ECC_CURVE_INVALID;
  7003. ecc_key* eccKey = (ecc_key*)keyShareEntry->key;
  7004. /* find supported curve */
  7005. switch (keyShareEntry->group) {
  7006. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  7007. #ifndef NO_ECC_SECP
  7008. case WOLFSSL_ECC_SECP256R1:
  7009. curveId = ECC_SECP256R1;
  7010. break;
  7011. #endif /* !NO_ECC_SECP */
  7012. #ifdef WOLFSSL_SM2
  7013. case WOLFSSL_ECC_SM2P256V1:
  7014. curveId = ECC_SM2P256V1;
  7015. break;
  7016. #endif
  7017. #endif
  7018. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  7019. #ifndef NO_ECC_SECP
  7020. case WOLFSSL_ECC_SECP384R1:
  7021. curveId = ECC_SECP384R1;
  7022. break;
  7023. #endif /* !NO_ECC_SECP */
  7024. #endif
  7025. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  7026. #ifndef NO_ECC_SECP
  7027. case WOLFSSL_ECC_SECP521R1:
  7028. curveId = ECC_SECP521R1;
  7029. break;
  7030. #endif /* !NO_ECC_SECP */
  7031. #endif
  7032. #if defined(HAVE_X448) && ECC_MIN_KEY_SZ <= 448
  7033. case WOLFSSL_ECC_X448:
  7034. curveId = ECC_X448;
  7035. break;
  7036. #endif
  7037. default:
  7038. /* unsupported curve */
  7039. WOLFSSL_ERROR_VERBOSE(ECC_PEERKEY_ERROR);
  7040. return ECC_PEERKEY_ERROR;
  7041. }
  7042. #ifdef WOLFSSL_ASYNC_CRYPT
  7043. if (keyShareEntry->lastRet == 0) /* don't enter here if WC_PENDING_E */
  7044. #endif
  7045. {
  7046. #ifdef WOLFSSL_DEBUG_TLS
  7047. WOLFSSL_MSG("Peer ECC Key");
  7048. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  7049. #endif
  7050. if (ssl->peerEccKey != NULL) {
  7051. wc_ecc_free(ssl->peerEccKey);
  7052. XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
  7053. ssl->peerEccKeyPresent = 0;
  7054. }
  7055. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  7056. ret = tsip_Tls13GenSharedSecret(ssl, keyShareEntry);
  7057. if (ret != CRYPTOCB_UNAVAILABLE) {
  7058. return ret;
  7059. }
  7060. ret = 0;
  7061. #endif
  7062. ssl->peerEccKey = (ecc_key*)XMALLOC(sizeof(ecc_key), ssl->heap,
  7063. DYNAMIC_TYPE_ECC);
  7064. if (ssl->peerEccKey == NULL) {
  7065. WOLFSSL_MSG("PeerEccKey Memory error");
  7066. ret = MEMORY_ERROR;
  7067. }
  7068. if (ret == 0) {
  7069. ret = wc_ecc_init_ex(ssl->peerEccKey, ssl->heap, ssl->devId);
  7070. }
  7071. /* Point is validated by import function. */
  7072. if (ret == 0) {
  7073. ret = wc_ecc_import_x963_ex(keyShareEntry->ke, keyShareEntry->keLen,
  7074. ssl->peerEccKey, curveId);
  7075. if (ret != 0) {
  7076. ret = ECC_PEERKEY_ERROR;
  7077. WOLFSSL_ERROR_VERBOSE(ret);
  7078. }
  7079. }
  7080. if (ret == 0) {
  7081. ssl->ecdhCurveOID = ssl->peerEccKey->dp->oidSum;
  7082. ssl->peerEccKeyPresent = 1;
  7083. }
  7084. }
  7085. if (ret == 0 && eccKey == NULL)
  7086. ret = BAD_FUNC_ARG;
  7087. if (ret == 0) {
  7088. ret = EccSharedSecret(ssl, eccKey, ssl->peerEccKey,
  7089. keyShareEntry->ke, &keyShareEntry->keLen,
  7090. ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz,
  7091. ssl->options.side
  7092. );
  7093. #ifdef WOLFSSL_ASYNC_CRYPT
  7094. if (ret == WC_PENDING_E)
  7095. return ret;
  7096. #endif
  7097. }
  7098. /* done with key share, release resources */
  7099. if (ssl->peerEccKey != NULL
  7100. #ifdef HAVE_PK_CALLBACKS
  7101. && ssl->ctx->EccSharedSecretCb == NULL
  7102. #endif
  7103. ) {
  7104. wc_ecc_free(ssl->peerEccKey);
  7105. XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
  7106. ssl->peerEccKey = NULL;
  7107. ssl->peerEccKeyPresent = 0;
  7108. }
  7109. if (keyShareEntry->key) {
  7110. wc_ecc_free((ecc_key*)keyShareEntry->key);
  7111. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_ECC);
  7112. keyShareEntry->key = NULL;
  7113. }
  7114. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7115. keyShareEntry->ke = NULL;
  7116. #else
  7117. (void)ssl;
  7118. (void)keyShareEntry;
  7119. ret = PEER_KEY_ERROR;
  7120. WOLFSSL_ERROR_VERBOSE(ret);
  7121. #endif /* HAVE_ECC */
  7122. return ret;
  7123. }
  7124. #ifdef HAVE_PQC
  7125. /* Process the Kyber key share extension on the client side.
  7126. *
  7127. * ssl The SSL/TLS object.
  7128. * keyShareEntry The key share entry object to use to calculate shared secret.
  7129. * returns 0 on success and other values indicate failure.
  7130. */
  7131. static int TLSX_KeyShare_ProcessPqc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  7132. {
  7133. int ret = 0;
  7134. int type;
  7135. KyberKey kem[1];
  7136. byte* sharedSecret = NULL;
  7137. word32 sharedSecretLen = 0;
  7138. int oqs_group = 0;
  7139. int ecc_group = 0;
  7140. ecc_key eccpubkey;
  7141. word32 outlen = 0;
  7142. word32 privSz = 0;
  7143. word32 ctSz = 0;
  7144. word32 ssSz = 0;
  7145. if (keyShareEntry->ke == NULL) {
  7146. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  7147. return BAD_FUNC_ARG;
  7148. }
  7149. if (ssl->options.side == WOLFSSL_SERVER_END) {
  7150. /* I am the server, the shared secret has already been generated and
  7151. * is in keyShareEntry->ke; copy it to the pre-master secret
  7152. * pre-allocated buffer. */
  7153. if (keyShareEntry->keLen > ENCRYPT_LEN) {
  7154. WOLFSSL_MSG("shared secret is too long.");
  7155. return LENGTH_ERROR;
  7156. }
  7157. XMEMCPY(ssl->arrays->preMasterSecret, keyShareEntry->ke,
  7158. keyShareEntry->keLen);
  7159. ssl->arrays->preMasterSz = keyShareEntry->keLen;
  7160. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_SECRET);
  7161. keyShareEntry->ke = NULL;
  7162. keyShareEntry->keLen = 0;
  7163. return 0;
  7164. }
  7165. /* I am the client, the ciphertext is in keyShareEntry->ke */
  7166. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  7167. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  7168. if (ret != 0) {
  7169. WOLFSSL_MSG("Memory allocation error.");
  7170. return MEMORY_E;
  7171. }
  7172. ret = kyber_id2type(oqs_group, &type);
  7173. if (ret != 0) {
  7174. wc_ecc_free(&eccpubkey);
  7175. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  7176. return BAD_FUNC_ARG;
  7177. }
  7178. ret = wc_KyberKey_Init(type, kem, ssl->heap, ssl->devId);
  7179. if (ret != 0) {
  7180. wc_ecc_free(&eccpubkey);
  7181. WOLFSSL_MSG("Error creating Kyber KEM");
  7182. return MEMORY_E;
  7183. }
  7184. if (ret == 0) {
  7185. ret = wc_KyberKey_SharedSecretSize(kem, &ssSz);
  7186. }
  7187. if (ret == 0) {
  7188. sharedSecretLen = ssSz;
  7189. switch (ecc_group) {
  7190. case WOLFSSL_ECC_SECP256R1:
  7191. sharedSecretLen += 32;
  7192. outlen = 32;
  7193. break;
  7194. case WOLFSSL_ECC_SECP384R1:
  7195. sharedSecretLen += 48;
  7196. outlen = 48;
  7197. break;
  7198. case WOLFSSL_ECC_SECP521R1:
  7199. sharedSecretLen += 66;
  7200. outlen = 66;
  7201. break;
  7202. default:
  7203. break;
  7204. }
  7205. }
  7206. if (ret == 0) {
  7207. sharedSecret = (byte*)XMALLOC(sharedSecretLen, ssl->heap,
  7208. DYNAMIC_TYPE_TLSX);
  7209. if (sharedSecret == NULL) {
  7210. WOLFSSL_MSG("Memory allocation error.");
  7211. ret = MEMORY_E;
  7212. }
  7213. }
  7214. if (ret == 0) {
  7215. ret = wc_KyberKey_CipherTextSize(kem, &ctSz);
  7216. }
  7217. if (ret == 0) {
  7218. ret = wc_KyberKey_PrivateKeySize(kem, &privSz);
  7219. }
  7220. if (ret == 0) {
  7221. ret = wc_KyberKey_DecodePrivateKey(kem, keyShareEntry->privKey, privSz);
  7222. }
  7223. if (ret == 0) {
  7224. ret = wc_KyberKey_Decapsulate(kem, sharedSecret + outlen,
  7225. keyShareEntry->ke + keyShareEntry->keLen - ctSz, ctSz);
  7226. if (ret != 0) {
  7227. WOLFSSL_MSG("wc_KyberKey decapsulation failure.");
  7228. ret = BAD_FUNC_ARG;
  7229. }
  7230. }
  7231. if (ecc_group != 0) {
  7232. if (ret == 0) {
  7233. /* Point is validated by import function. */
  7234. ret = wc_ecc_import_x963(keyShareEntry->ke,
  7235. keyShareEntry->keLen - ctSz,
  7236. &eccpubkey);
  7237. if (ret != 0) {
  7238. WOLFSSL_MSG("ECC Public key import error.");
  7239. }
  7240. }
  7241. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  7242. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  7243. !defined(HAVE_SELFTEST)
  7244. if (ret == 0) {
  7245. ret = wc_ecc_set_rng(keyShareEntry->key, ssl->rng);
  7246. if (ret != 0) {
  7247. WOLFSSL_MSG("Failure to set the ECC private key RNG.");
  7248. }
  7249. }
  7250. #endif
  7251. if (ret == 0) {
  7252. PRIVATE_KEY_UNLOCK();
  7253. ret = wc_ecc_shared_secret(keyShareEntry->key, &eccpubkey,
  7254. sharedSecret, &outlen);
  7255. PRIVATE_KEY_LOCK();
  7256. if (outlen != sharedSecretLen - ssSz) {
  7257. WOLFSSL_MSG("ECC shared secret derivation error.");
  7258. ret = BAD_FUNC_ARG;
  7259. }
  7260. }
  7261. }
  7262. if ((ret == 0) && (sharedSecretLen > ENCRYPT_LEN)) {
  7263. WOLFSSL_MSG("shared secret is too long.");
  7264. ret = LENGTH_ERROR;
  7265. }
  7266. if (ret == 0) {
  7267. /* Copy the shared secret to the pre-master secret pre-allocated
  7268. * buffer. */
  7269. XMEMCPY(ssl->arrays->preMasterSecret, sharedSecret, sharedSecretLen);
  7270. ssl->arrays->preMasterSz = (word32) sharedSecretLen;
  7271. }
  7272. if (sharedSecret != NULL) {
  7273. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
  7274. }
  7275. wc_ecc_free(&eccpubkey);
  7276. wc_KyberKey_Free(kem);
  7277. return ret;
  7278. }
  7279. #endif /* HAVE_PQC */
  7280. /* Process the key share extension on the client side.
  7281. *
  7282. * ssl The SSL/TLS object.
  7283. * keyShareEntry The key share entry object to use to calculate shared secret.
  7284. * returns 0 on success and other values indicate failure.
  7285. */
  7286. static int TLSX_KeyShare_Process(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  7287. {
  7288. int ret;
  7289. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  7290. ssl->session->namedGroup = keyShareEntry->group;
  7291. #endif
  7292. /* reset the pre master secret size */
  7293. if (ssl->arrays->preMasterSz == 0)
  7294. ssl->arrays->preMasterSz = ENCRYPT_LEN;
  7295. /* Use Key Share Data from server. */
  7296. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(keyShareEntry->group))
  7297. ret = TLSX_KeyShare_ProcessDh(ssl, keyShareEntry);
  7298. else if (keyShareEntry->group == WOLFSSL_ECC_X25519)
  7299. ret = TLSX_KeyShare_ProcessX25519(ssl, keyShareEntry);
  7300. else if (keyShareEntry->group == WOLFSSL_ECC_X448)
  7301. ret = TLSX_KeyShare_ProcessX448(ssl, keyShareEntry);
  7302. #ifdef HAVE_PQC
  7303. else if (WOLFSSL_NAMED_GROUP_IS_PQC(keyShareEntry->group))
  7304. ret = TLSX_KeyShare_ProcessPqc(ssl, keyShareEntry);
  7305. #endif
  7306. else
  7307. ret = TLSX_KeyShare_ProcessEcc(ssl, keyShareEntry);
  7308. #ifdef WOLFSSL_DEBUG_TLS
  7309. if (ret == 0) {
  7310. WOLFSSL_MSG("KE Secret");
  7311. WOLFSSL_BUFFER(ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
  7312. }
  7313. #endif
  7314. #ifdef WOLFSSL_ASYNC_CRYPT
  7315. keyShareEntry->lastRet = ret;
  7316. #endif
  7317. return ret;
  7318. }
  7319. /* Parse an entry of the KeyShare extension.
  7320. *
  7321. * ssl The SSL/TLS object.
  7322. * input The extension data.
  7323. * length The length of the extension data.
  7324. * kse The new key share entry object.
  7325. * returns a positive number to indicate amount of data parsed and a negative
  7326. * number on error.
  7327. */
  7328. static int TLSX_KeyShareEntry_Parse(const WOLFSSL* ssl, const byte* input,
  7329. word16 length, KeyShareEntry **kse, TLSX** extensions)
  7330. {
  7331. int ret;
  7332. word16 group;
  7333. word16 keLen;
  7334. int offset = 0;
  7335. byte* ke;
  7336. if (length < OPAQUE16_LEN + OPAQUE16_LEN)
  7337. return BUFFER_ERROR;
  7338. /* Named group */
  7339. ato16(&input[offset], &group);
  7340. offset += OPAQUE16_LEN;
  7341. /* Key exchange data - public key. */
  7342. ato16(&input[offset], &keLen);
  7343. offset += OPAQUE16_LEN;
  7344. if (keLen == 0)
  7345. return INVALID_PARAMETER;
  7346. if (keLen > length - offset)
  7347. return BUFFER_ERROR;
  7348. #ifdef HAVE_PQC
  7349. if (WOLFSSL_NAMED_GROUP_IS_PQC(group) &&
  7350. ssl->options.side == WOLFSSL_SERVER_END) {
  7351. /* For KEMs, the public key is not stored. Casting away const because
  7352. * we know for KEMs, it will be read-only.*/
  7353. ke = (byte *)&input[offset];
  7354. } else
  7355. #endif
  7356. {
  7357. /* Store a copy in the key share object. */
  7358. ke = (byte*)XMALLOC(keLen, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7359. if (ke == NULL)
  7360. return MEMORY_E;
  7361. XMEMCPY(ke, &input[offset], keLen);
  7362. }
  7363. /* Populate a key share object in the extension. */
  7364. ret = TLSX_KeyShare_Use(ssl, group, keLen, ke, kse, extensions);
  7365. if (ret != 0) {
  7366. if (ke != &input[offset]) {
  7367. XFREE(ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7368. }
  7369. return ret;
  7370. }
  7371. /* Total length of the parsed data. */
  7372. return offset + keLen;
  7373. }
  7374. /* Searches the groups sent for the specified named group.
  7375. *
  7376. * ssl SSL/TLS object.
  7377. * name Group name to match.
  7378. * returns 1 when the extension has the group name and 0 otherwise.
  7379. */
  7380. static int TLSX_KeyShare_Find(WOLFSSL* ssl, word16 group)
  7381. {
  7382. TLSX* extension;
  7383. KeyShareEntry* list;
  7384. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7385. if (extension == NULL) {
  7386. extension = TLSX_Find(ssl->ctx->extensions, TLSX_KEY_SHARE);
  7387. if (extension == NULL)
  7388. return 0;
  7389. }
  7390. list = (KeyShareEntry*)extension->data;
  7391. while (list != NULL) {
  7392. if (list->group == group)
  7393. return 1;
  7394. list = list->next;
  7395. }
  7396. return 0;
  7397. }
  7398. /* Searches the supported groups extension for the specified named group.
  7399. *
  7400. * ssl The SSL/TLS object.
  7401. * name The group name to match.
  7402. * returns 1 when the extension has the group name and 0 otherwise.
  7403. */
  7404. static int TLSX_SupportedGroups_Find(const WOLFSSL* ssl, word16 name,
  7405. TLSX* extensions)
  7406. {
  7407. #ifdef HAVE_SUPPORTED_CURVES
  7408. TLSX* extension;
  7409. SupportedCurve* curve = NULL;
  7410. if ((extension = TLSX_Find(extensions, TLSX_SUPPORTED_GROUPS)) == NULL) {
  7411. if ((extension = TLSX_Find(ssl->ctx->extensions,
  7412. TLSX_SUPPORTED_GROUPS)) == NULL) {
  7413. return 0;
  7414. }
  7415. }
  7416. for (curve = (SupportedCurve*)extension->data; curve; curve = curve->next) {
  7417. if (curve->name == name)
  7418. return 1;
  7419. }
  7420. #endif
  7421. (void)ssl;
  7422. (void)name;
  7423. return 0;
  7424. }
  7425. int TLSX_KeyShare_Parse_ClientHello(const WOLFSSL* ssl,
  7426. const byte* input, word16 length, TLSX** extensions)
  7427. {
  7428. int ret;
  7429. int offset = 0;
  7430. word16 len;
  7431. TLSX* extension;
  7432. /* Add a KeyShare extension if it doesn't exist even if peer sent no
  7433. * entries. The presence of this extension signals that the peer can be
  7434. * negotiated with. */
  7435. extension = TLSX_Find(*extensions, TLSX_KEY_SHARE);
  7436. if (extension == NULL) {
  7437. /* Push new KeyShare extension. */
  7438. ret = TLSX_Push(extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7439. if (ret != 0)
  7440. return ret;
  7441. }
  7442. if (length < OPAQUE16_LEN)
  7443. return BUFFER_ERROR;
  7444. /* ClientHello contains zero or more key share entries. */
  7445. ato16(input, &len);
  7446. if (len != length - OPAQUE16_LEN)
  7447. return BUFFER_ERROR;
  7448. offset += OPAQUE16_LEN;
  7449. while (offset < (int)length) {
  7450. ret = TLSX_KeyShareEntry_Parse(ssl, &input[offset],
  7451. length - (word16)offset, NULL, extensions);
  7452. if (ret < 0)
  7453. return ret;
  7454. offset += ret;
  7455. }
  7456. return 0;
  7457. }
  7458. /* Parse the KeyShare extension.
  7459. * Different formats in different messages.
  7460. *
  7461. * ssl The SSL/TLS object.
  7462. * input The extension data.
  7463. * length The length of the extension data.
  7464. * msgType The type of the message this extension is being parsed from.
  7465. * returns 0 on success and other values indicate failure.
  7466. */
  7467. int TLSX_KeyShare_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  7468. byte msgType)
  7469. {
  7470. int ret = 0;
  7471. KeyShareEntry *keyShareEntry = NULL;
  7472. word16 group;
  7473. if (msgType == client_hello) {
  7474. ret = TLSX_KeyShare_Parse_ClientHello(ssl, input, length,
  7475. &ssl->extensions);
  7476. }
  7477. else if (msgType == server_hello) {
  7478. int len;
  7479. if (length < OPAQUE16_LEN)
  7480. return BUFFER_ERROR;
  7481. /* The data is the named group the server wants to use. */
  7482. ato16(input, &group);
  7483. /* Check the selected group was supported by ClientHello extensions. */
  7484. if (!TLSX_SupportedGroups_Find(ssl, group, ssl->extensions)) {
  7485. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7486. return BAD_KEY_SHARE_DATA;
  7487. }
  7488. /* Check if the group was sent. */
  7489. if (!TLSX_KeyShare_Find(ssl, group)) {
  7490. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7491. return BAD_KEY_SHARE_DATA;
  7492. }
  7493. /* ServerHello contains one key share entry. */
  7494. len = TLSX_KeyShareEntry_Parse(ssl, input, length, &keyShareEntry,
  7495. &ssl->extensions);
  7496. if (len != (int)length)
  7497. return BUFFER_ERROR;
  7498. /* Not in list sent if there isn't a private key. */
  7499. if (keyShareEntry == NULL || (keyShareEntry->key == NULL
  7500. #if !defined(NO_DH) || defined(HAVE_PQC)
  7501. && keyShareEntry->privKey == NULL
  7502. #endif
  7503. )) {
  7504. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7505. return BAD_KEY_SHARE_DATA;
  7506. }
  7507. /* Process the entry to calculate the secret. */
  7508. ret = TLSX_KeyShare_Process(ssl, keyShareEntry);
  7509. if (ret == 0)
  7510. ssl->session->namedGroup = ssl->namedGroup = group;
  7511. }
  7512. else if (msgType == hello_retry_request) {
  7513. if (length != OPAQUE16_LEN)
  7514. return BUFFER_ERROR;
  7515. /* The data is the named group the server wants to use. */
  7516. ato16(input, &group);
  7517. #ifdef WOLFSSL_ASYNC_CRYPT
  7518. /* only perform find and clear TLSX if not returning from async */
  7519. if (ssl->error != WC_PENDING_E)
  7520. #endif
  7521. {
  7522. /* Check the selected group was supported by ClientHello extensions. */
  7523. if (!TLSX_SupportedGroups_Find(ssl, group, ssl->extensions)) {
  7524. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7525. return BAD_KEY_SHARE_DATA;
  7526. }
  7527. /* Check if the group was sent. */
  7528. if (TLSX_KeyShare_Find(ssl, group)) {
  7529. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7530. return BAD_KEY_SHARE_DATA;
  7531. }
  7532. /* Clear out unusable key shares. */
  7533. ret = TLSX_KeyShare_Empty(ssl);
  7534. if (ret != 0)
  7535. return ret;
  7536. }
  7537. ret = TLSX_KeyShare_Use(ssl, group, 0, NULL, NULL, &ssl->extensions);
  7538. if (ret == 0)
  7539. ssl->session->namedGroup = ssl->namedGroup = group;
  7540. }
  7541. else {
  7542. /* Not a message type that is allowed to have this extension. */
  7543. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  7544. return SANITY_MSG_E;
  7545. }
  7546. return ret;
  7547. }
  7548. /* Create a new key share entry and put it into the list.
  7549. *
  7550. * list The linked list of key share entries.
  7551. * group The named group.
  7552. * heap The memory to allocate with.
  7553. * keyShareEntry The new key share entry object.
  7554. * returns 0 on success and other values indicate failure.
  7555. */
  7556. static int TLSX_KeyShare_New(KeyShareEntry** list, int group, void *heap,
  7557. KeyShareEntry** keyShareEntry)
  7558. {
  7559. KeyShareEntry* kse;
  7560. KeyShareEntry** next;
  7561. kse = (KeyShareEntry*)XMALLOC(sizeof(KeyShareEntry), heap,
  7562. DYNAMIC_TYPE_TLSX);
  7563. if (kse == NULL)
  7564. return MEMORY_E;
  7565. XMEMSET(kse, 0, sizeof(*kse));
  7566. kse->group = (word16)group;
  7567. /* Add it to the back and maintain the links. */
  7568. while (*list != NULL) {
  7569. /* Assign to temporary to work around compiler bug found by customer. */
  7570. next = &((*list)->next);
  7571. list = next;
  7572. }
  7573. *list = kse;
  7574. *keyShareEntry = kse;
  7575. (void)heap;
  7576. return 0;
  7577. }
  7578. #ifdef HAVE_PQC
  7579. static int server_generate_pqc_ciphertext(WOLFSSL* ssl,
  7580. KeyShareEntry* keyShareEntry, byte* data, word16 len)
  7581. {
  7582. /* I am the server. The data parameter is the client's public key. I need
  7583. * to generate the public information (AKA ciphertext) and shared secret
  7584. * here. Note the "public information" is equivalent to a the public key in
  7585. * key exchange parlance. That's why it is being assigned to pubKey.
  7586. */
  7587. int type;
  7588. KyberKey kem[1];
  7589. byte* sharedSecret = NULL;
  7590. byte* ciphertext = NULL;
  7591. int ret = 0;
  7592. int oqs_group = 0;
  7593. int ecc_group = 0;
  7594. KeyShareEntry *ecc_kse = NULL;
  7595. ecc_key eccpubkey;
  7596. word32 outlen = 0;
  7597. word32 pubSz = 0;
  7598. word32 ctSz = 0;
  7599. word32 ssSz = 0;
  7600. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  7601. ret = kyber_id2type(oqs_group, &type);
  7602. if (ret != 0) {
  7603. WOLFSSL_MSG("Invalid Kyber algorithm specified.");
  7604. return BAD_FUNC_ARG;
  7605. }
  7606. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  7607. if (ret != 0) {
  7608. WOLFSSL_MSG("Could not do ECC public key initialization.");
  7609. return MEMORY_E;
  7610. }
  7611. ret = wc_KyberKey_Init(type, kem, ssl->heap, ssl->devId);
  7612. if (ret != 0) {
  7613. wc_ecc_free(&eccpubkey);
  7614. WOLFSSL_MSG("Error creating Kyber KEM");
  7615. return MEMORY_E;
  7616. }
  7617. if (ret == 0) {
  7618. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  7619. DYNAMIC_TYPE_TLSX);
  7620. if (ecc_kse == NULL) {
  7621. WOLFSSL_MSG("ecc_kse memory allocation failure");
  7622. ret = MEMORY_ERROR;
  7623. }
  7624. }
  7625. if (ret == 0) {
  7626. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  7627. }
  7628. if (ret == 0 && ecc_group != 0) {
  7629. ecc_kse->group = ecc_group;
  7630. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  7631. /* No message, TLSX_KeyShare_GenEccKey() will do it. */
  7632. }
  7633. if (ret == 0) {
  7634. ret = wc_KyberKey_PublicKeySize(kem, &pubSz);
  7635. }
  7636. if (ret == 0) {
  7637. ret = wc_KyberKey_CipherTextSize(kem, &ctSz);
  7638. }
  7639. if (ret == 0) {
  7640. ret = wc_KyberKey_SharedSecretSize(kem, &ssSz);
  7641. }
  7642. if (ret == 0 && len != pubSz + ecc_kse->pubKeyLen) {
  7643. WOLFSSL_MSG("Invalid public key.");
  7644. ret = BAD_FUNC_ARG;
  7645. }
  7646. if (ret == 0) {
  7647. sharedSecret = (byte*)XMALLOC(ecc_kse->keyLen + ssSz, ssl->heap,
  7648. DYNAMIC_TYPE_SECRET);
  7649. ciphertext = (byte*)XMALLOC(ecc_kse->pubKeyLen + ctSz, ssl->heap,
  7650. DYNAMIC_TYPE_TLSX);
  7651. if (sharedSecret == NULL || ciphertext == NULL) {
  7652. WOLFSSL_MSG("Ciphertext/shared secret memory allocation failure.");
  7653. ret = MEMORY_E;
  7654. }
  7655. }
  7656. if (ecc_group != 0) {
  7657. if (ret == 0) {
  7658. /* Point is validated by import function. */
  7659. ret = wc_ecc_import_x963(data, len - pubSz, &eccpubkey);
  7660. if (ret != 0) {
  7661. WOLFSSL_MSG("Bad ECC public key.");
  7662. }
  7663. }
  7664. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  7665. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  7666. !defined(HAVE_SELFTEST)
  7667. if (ret == 0) {
  7668. ret = wc_ecc_set_rng(ecc_kse->key, ssl->rng);
  7669. }
  7670. #endif
  7671. if (ret == 0) {
  7672. outlen = ecc_kse->keyLen;
  7673. PRIVATE_KEY_UNLOCK();
  7674. ret = wc_ecc_shared_secret(ecc_kse->key, &eccpubkey,
  7675. sharedSecret,
  7676. &outlen);
  7677. PRIVATE_KEY_LOCK();
  7678. if (outlen != ecc_kse->keyLen) {
  7679. WOLFSSL_MSG("Data length mismatch.");
  7680. ret = BAD_FUNC_ARG;
  7681. }
  7682. }
  7683. }
  7684. if (ret == 0) {
  7685. ret = wc_KyberKey_DecodePublicKey(kem, data + ecc_kse->pubKeyLen,
  7686. pubSz);
  7687. }
  7688. if (ret == 0) {
  7689. ret = wc_KyberKey_Encapsulate(kem, ciphertext + ecc_kse->pubKeyLen,
  7690. sharedSecret + outlen, ssl->rng);
  7691. if (ret != 0) {
  7692. WOLFSSL_MSG("wc_KyberKey encapsulation failure.");
  7693. }
  7694. }
  7695. if (ret == 0) {
  7696. if (keyShareEntry->ke != NULL) {
  7697. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7698. }
  7699. keyShareEntry->ke = sharedSecret;
  7700. keyShareEntry->keLen = outlen + ssSz;
  7701. sharedSecret = NULL;
  7702. if (ecc_kse->pubKeyLen > 0)
  7703. XMEMCPY(ciphertext, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  7704. keyShareEntry->pubKey = ciphertext;
  7705. keyShareEntry->pubKeyLen = (word32)(ecc_kse->pubKeyLen + ctSz);
  7706. ciphertext = NULL;
  7707. /* Set namedGroup so wolfSSL_get_curve_name() can function properly on
  7708. * the server side. */
  7709. ssl->namedGroup = keyShareEntry->group;
  7710. }
  7711. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  7712. if (sharedSecret != NULL)
  7713. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
  7714. if (ciphertext != NULL)
  7715. XFREE(ciphertext, ssl->heap, DYNAMIC_TYPE_TLSX);
  7716. wc_ecc_free(&eccpubkey);
  7717. wc_KyberKey_Free(kem);
  7718. return ret;
  7719. }
  7720. #endif /* HAVE_PQC */
  7721. /* Use the data to create a new key share object in the extensions.
  7722. *
  7723. * ssl The SSL/TLS object.
  7724. * group The named group.
  7725. * len The length of the public key data.
  7726. * data The public key data.
  7727. * kse The new key share entry object.
  7728. * returns 0 on success and other values indicate failure.
  7729. */
  7730. int TLSX_KeyShare_Use(const WOLFSSL* ssl, word16 group, word16 len, byte* data,
  7731. KeyShareEntry **kse, TLSX** extensions)
  7732. {
  7733. int ret = 0;
  7734. TLSX* extension;
  7735. KeyShareEntry* keyShareEntry = NULL;
  7736. /* Find the KeyShare extension if it exists. */
  7737. extension = TLSX_Find(*extensions, TLSX_KEY_SHARE);
  7738. if (extension == NULL) {
  7739. /* Push new KeyShare extension. */
  7740. ret = TLSX_Push(extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7741. if (ret != 0)
  7742. return ret;
  7743. extension = TLSX_Find(*extensions, TLSX_KEY_SHARE);
  7744. if (extension == NULL)
  7745. return MEMORY_E;
  7746. }
  7747. extension->resp = 0;
  7748. /* Try to find the key share entry with this group. */
  7749. keyShareEntry = (KeyShareEntry*)extension->data;
  7750. while (keyShareEntry != NULL) {
  7751. if (keyShareEntry->group == group)
  7752. break;
  7753. keyShareEntry = keyShareEntry->next;
  7754. }
  7755. /* Create a new key share entry if not found. */
  7756. if (keyShareEntry == NULL) {
  7757. ret = TLSX_KeyShare_New((KeyShareEntry**)&extension->data, group,
  7758. ssl->heap, &keyShareEntry);
  7759. if (ret != 0)
  7760. return ret;
  7761. }
  7762. #ifdef HAVE_PQC
  7763. if (WOLFSSL_NAMED_GROUP_IS_PQC(group) &&
  7764. ssl->options.side == WOLFSSL_SERVER_END) {
  7765. ret = server_generate_pqc_ciphertext((WOLFSSL*)ssl, keyShareEntry, data,
  7766. len);
  7767. if (ret != 0)
  7768. return ret;
  7769. }
  7770. else
  7771. #endif
  7772. if (data != NULL) {
  7773. if (keyShareEntry->ke != NULL) {
  7774. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7775. }
  7776. keyShareEntry->ke = data;
  7777. keyShareEntry->keLen = len;
  7778. }
  7779. else {
  7780. /* Generate a key pair. Casting to non-const since changes inside are
  7781. * minimal but would require an extensive redesign to refactor. Also
  7782. * this path shouldn't be taken when parsing a ClientHello in stateless
  7783. * mode. */
  7784. ret = TLSX_KeyShare_GenKey((WOLFSSL*)ssl, keyShareEntry);
  7785. if (ret != 0)
  7786. return ret;
  7787. }
  7788. if (kse != NULL)
  7789. *kse = keyShareEntry;
  7790. return 0;
  7791. }
  7792. /* Set an empty Key Share extension.
  7793. *
  7794. * ssl The SSL/TLS object.
  7795. * returns 0 on success and other values indicate failure.
  7796. */
  7797. int TLSX_KeyShare_Empty(WOLFSSL* ssl)
  7798. {
  7799. int ret = 0;
  7800. TLSX* extension;
  7801. /* Find the KeyShare extension if it exists. */
  7802. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7803. if (extension == NULL) {
  7804. /* Push new KeyShare extension. */
  7805. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7806. }
  7807. else if (extension->data != NULL) {
  7808. TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
  7809. extension->data = NULL;
  7810. }
  7811. return ret;
  7812. }
  7813. /* Returns whether this group is supported.
  7814. *
  7815. * namedGroup The named group to check.
  7816. * returns 1 when supported or 0 otherwise.
  7817. */
  7818. static int TLSX_KeyShare_IsSupported(int namedGroup)
  7819. {
  7820. switch (namedGroup) {
  7821. #ifdef HAVE_FFDHE_2048
  7822. case WOLFSSL_FFDHE_2048:
  7823. break;
  7824. #endif
  7825. #ifdef HAVE_FFDHE_3072
  7826. case WOLFSSL_FFDHE_3072:
  7827. break;
  7828. #endif
  7829. #ifdef HAVE_FFDHE_4096
  7830. case WOLFSSL_FFDHE_4096:
  7831. break;
  7832. #endif
  7833. #ifdef HAVE_FFDHE_6144
  7834. case WOLFSSL_FFDHE_6144:
  7835. break;
  7836. #endif
  7837. #ifdef HAVE_FFDHE_8192
  7838. case WOLFSSL_FFDHE_8192:
  7839. break;
  7840. #endif
  7841. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  7842. #ifdef HAVE_ECC_KOBLITZ
  7843. case WOLFSSL_ECC_SECP256K1:
  7844. break;
  7845. #endif
  7846. #ifndef NO_ECC_SECP
  7847. case WOLFSSL_ECC_SECP256R1:
  7848. break;
  7849. #endif /* !NO_ECC_SECP */
  7850. #ifdef HAVE_ECC_BRAINPOOL
  7851. case WOLFSSL_ECC_BRAINPOOLP256R1:
  7852. break;
  7853. #endif
  7854. #ifdef WOLFSSL_SM2
  7855. case WOLFSSL_ECC_SM2P256V1:
  7856. break;
  7857. #endif /* WOLFSSL_SM2 */
  7858. #endif
  7859. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  7860. case WOLFSSL_ECC_X25519:
  7861. break;
  7862. #endif
  7863. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  7864. case WOLFSSL_ECC_X448:
  7865. break;
  7866. #endif
  7867. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  7868. #ifndef NO_ECC_SECP
  7869. case WOLFSSL_ECC_SECP384R1:
  7870. break;
  7871. #endif /* !NO_ECC_SECP */
  7872. #ifdef HAVE_ECC_BRAINPOOL
  7873. case WOLFSSL_ECC_BRAINPOOLP384R1:
  7874. break;
  7875. #endif
  7876. #endif
  7877. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  7878. #ifndef NO_ECC_SECP
  7879. case WOLFSSL_ECC_SECP521R1:
  7880. break;
  7881. #endif /* !NO_ECC_SECP */
  7882. #endif
  7883. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  7884. #ifdef HAVE_ECC_KOBLITZ
  7885. case WOLFSSL_ECC_SECP160K1:
  7886. break;
  7887. #endif
  7888. #ifndef NO_ECC_SECP
  7889. case WOLFSSL_ECC_SECP160R1:
  7890. break;
  7891. #endif
  7892. #ifdef HAVE_ECC_SECPR2
  7893. case WOLFSSL_ECC_SECP160R2:
  7894. break;
  7895. #endif
  7896. #endif
  7897. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  7898. #ifdef HAVE_ECC_KOBLITZ
  7899. case WOLFSSL_ECC_SECP192K1:
  7900. break;
  7901. #endif
  7902. #ifndef NO_ECC_SECP
  7903. case WOLFSSL_ECC_SECP192R1:
  7904. break;
  7905. #endif
  7906. #endif
  7907. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  7908. #ifdef HAVE_ECC_KOBLITZ
  7909. case WOLFSSL_ECC_SECP224K1:
  7910. break;
  7911. #endif
  7912. #ifndef NO_ECC_SECP
  7913. case WOLFSSL_ECC_SECP224R1:
  7914. break;
  7915. #endif
  7916. #endif
  7917. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  7918. #ifdef HAVE_ECC_BRAINPOOL
  7919. case WOLFSSL_ECC_BRAINPOOLP512R1:
  7920. break;
  7921. #endif
  7922. #endif
  7923. #ifdef HAVE_PQC
  7924. #ifdef WOLFSSL_WC_KYBER
  7925. #ifdef WOLFSSL_KYBER512
  7926. case WOLFSSL_KYBER_LEVEL1:
  7927. #endif
  7928. #ifdef WOLFSSL_KYBER768
  7929. case WOLFSSL_KYBER_LEVEL3:
  7930. #endif
  7931. #ifdef WOLFSSL_KYBER1024
  7932. case WOLFSSL_KYBER_LEVEL5:
  7933. #endif
  7934. break;
  7935. #elif defined(HAVE_LIBOQS)
  7936. case WOLFSSL_KYBER_LEVEL1:
  7937. case WOLFSSL_KYBER_LEVEL3:
  7938. case WOLFSSL_KYBER_LEVEL5:
  7939. case WOLFSSL_P256_KYBER_LEVEL1:
  7940. case WOLFSSL_P384_KYBER_LEVEL3:
  7941. case WOLFSSL_P521_KYBER_LEVEL5:
  7942. {
  7943. int ret;
  7944. int id;
  7945. findEccPqc(NULL, &namedGroup, namedGroup);
  7946. ret = kyber_id2type(namedGroup, &id);
  7947. if (ret == NOT_COMPILED_IN) {
  7948. return 0;
  7949. }
  7950. if (! ext_kyber_enabled(id)) {
  7951. return 0;
  7952. }
  7953. break;
  7954. }
  7955. #elif defined(HAVE_PQM4)
  7956. case WOLFSSL_KYBER_LEVEL1:
  7957. break;
  7958. #endif
  7959. #endif /* HAVE_PQC */
  7960. default:
  7961. return 0;
  7962. }
  7963. return 1;
  7964. }
  7965. static const word16 preferredGroup[] = {
  7966. #if defined(HAVE_ECC) && (!defined(NO_ECC256) || \
  7967. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 256
  7968. WOLFSSL_ECC_SECP256R1,
  7969. #if !defined(HAVE_FIPS) && defined(WOLFSSL_SM2)
  7970. WOLFSSL_ECC_SM2P256V1,
  7971. #endif
  7972. #endif
  7973. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  7974. WOLFSSL_ECC_X25519,
  7975. #endif
  7976. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  7977. WOLFSSL_ECC_X448,
  7978. #endif
  7979. #if defined(HAVE_ECC) && (!defined(NO_ECC384) || \
  7980. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 384
  7981. WOLFSSL_ECC_SECP384R1,
  7982. #endif
  7983. #if defined(HAVE_ECC) && (!defined(NO_ECC521) || \
  7984. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 521
  7985. WOLFSSL_ECC_SECP521R1,
  7986. #endif
  7987. #if defined(HAVE_FFDHE_2048)
  7988. WOLFSSL_FFDHE_2048,
  7989. #endif
  7990. #if defined(HAVE_FFDHE_3072)
  7991. WOLFSSL_FFDHE_3072,
  7992. #endif
  7993. #if defined(HAVE_FFDHE_4096)
  7994. WOLFSSL_FFDHE_4096,
  7995. #endif
  7996. #if defined(HAVE_FFDHE_6144)
  7997. WOLFSSL_FFDHE_6144,
  7998. #endif
  7999. #if defined(HAVE_FFDHE_8192)
  8000. WOLFSSL_FFDHE_8192,
  8001. #endif
  8002. #ifdef WOLFSSL_WC_KYBER
  8003. #ifdef WOLFSSL_KYBER512
  8004. WOLFSSL_KYBER_LEVEL1,
  8005. #endif
  8006. #ifdef WOLFSSL_KYBER768
  8007. WOLFSSL_KYBER_LEVEL3,
  8008. #endif
  8009. #ifdef WOLFSSL_KYBER1024
  8010. WOLFSSL_KYBER_LEVEL5,
  8011. #endif
  8012. #elif defined(HAVE_LIBOQS)
  8013. /* These require a runtime call to TLSX_KeyShare_IsSupported to use */
  8014. WOLFSSL_KYBER_LEVEL1,
  8015. WOLFSSL_KYBER_LEVEL3,
  8016. WOLFSSL_KYBER_LEVEL5,
  8017. WOLFSSL_P256_KYBER_LEVEL1,
  8018. WOLFSSL_P384_KYBER_LEVEL3,
  8019. WOLFSSL_P521_KYBER_LEVEL5,
  8020. #elif defined(HAVE_PQM4)
  8021. WOLFSSL_KYBER_LEVEL1,
  8022. #endif
  8023. WOLFSSL_NAMED_GROUP_INVALID
  8024. };
  8025. #define PREFERRED_GROUP_SZ \
  8026. ((sizeof(preferredGroup)/sizeof(*preferredGroup)) - 1)
  8027. /* -1 for the invalid group */
  8028. /* Examines the application specified group ranking and returns the rank of the
  8029. * group.
  8030. * If no group ranking set then all groups are rank 0 (highest).
  8031. *
  8032. * ssl The SSL/TLS object.
  8033. * group The group to check ranking for.
  8034. * returns ranking from 0 to MAX_GROUP_COUNT-1 or -1 when group not in list.
  8035. */
  8036. static int TLSX_KeyShare_GroupRank(const WOLFSSL* ssl, int group)
  8037. {
  8038. byte i;
  8039. const word16* groups;
  8040. byte numGroups;
  8041. if (ssl->numGroups == 0) {
  8042. groups = preferredGroup;
  8043. numGroups = PREFERRED_GROUP_SZ;
  8044. }
  8045. else {
  8046. groups = ssl->group;
  8047. numGroups = ssl->numGroups;
  8048. }
  8049. #ifdef HAVE_LIBOQS
  8050. if (!TLSX_KeyShare_IsSupported(group))
  8051. return -1;
  8052. #endif
  8053. for (i = 0; i < numGroups; i++)
  8054. if (groups[i] == (word16)group)
  8055. return i;
  8056. return -1;
  8057. }
  8058. /* Set a key share that is supported by the client into extensions.
  8059. *
  8060. * ssl The SSL/TLS object.
  8061. * returns BAD_KEY_SHARE_DATA if no supported group has a key share,
  8062. * 0 if a supported group has a key share and other values indicate an error.
  8063. */
  8064. int TLSX_KeyShare_SetSupported(const WOLFSSL* ssl, TLSX** extensions)
  8065. {
  8066. int ret;
  8067. #ifdef HAVE_SUPPORTED_CURVES
  8068. TLSX* extension;
  8069. SupportedCurve* curve = NULL;
  8070. SupportedCurve* preferredCurve = NULL;
  8071. KeyShareEntry* kse = NULL;
  8072. int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
  8073. int rank;
  8074. extension = TLSX_Find(*extensions, TLSX_SUPPORTED_GROUPS);
  8075. if (extension != NULL)
  8076. curve = (SupportedCurve*)extension->data;
  8077. /* Use server's preference order. */
  8078. for (; curve != NULL; curve = curve->next) {
  8079. if (!TLSX_KeyShare_IsSupported(curve->name))
  8080. continue;
  8081. if (wolfSSL_curve_is_disabled(ssl, curve->name))
  8082. continue;
  8083. rank = TLSX_KeyShare_GroupRank(ssl, curve->name);
  8084. if (rank == -1)
  8085. continue;
  8086. if (rank < preferredRank) {
  8087. preferredCurve = curve;
  8088. preferredRank = rank;
  8089. }
  8090. }
  8091. curve = preferredCurve;
  8092. if (curve == NULL) {
  8093. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  8094. return BAD_KEY_SHARE_DATA;
  8095. }
  8096. #ifdef WOLFSSL_ASYNC_CRYPT
  8097. /* Check the old key share data list. */
  8098. extension = TLSX_Find(*extensions, TLSX_KEY_SHARE);
  8099. if (extension != NULL) {
  8100. kse = (KeyShareEntry*)extension->data;
  8101. /* We should not be computing keys if we are only going to advertise
  8102. * our choice here. */
  8103. if (kse != NULL && kse->lastRet == WC_PENDING_E) {
  8104. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  8105. return BAD_KEY_SHARE_DATA;
  8106. }
  8107. }
  8108. #endif
  8109. /* Push new KeyShare extension. This will also free the old one */
  8110. ret = TLSX_Push(extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  8111. if (ret != 0)
  8112. return ret;
  8113. /* Extension got pushed to head */
  8114. extension = *extensions;
  8115. /* Push the selected curve */
  8116. ret = TLSX_KeyShare_New((KeyShareEntry**)&extension->data, curve->name,
  8117. ssl->heap, &kse);
  8118. if (ret != 0)
  8119. return ret;
  8120. /* Set extension to be in response. */
  8121. extension->resp = 1;
  8122. #else
  8123. (void)ssl;
  8124. WOLFSSL_ERROR_VERBOSE(NOT_COMPILED_IN);
  8125. ret = NOT_COMPILED_IN;
  8126. #endif
  8127. return ret;
  8128. }
  8129. #ifdef WOLFSSL_DUAL_ALG_CERTS
  8130. /* Writes the CKS objects of a list in a buffer. */
  8131. static word16 CKS_WRITE(WOLFSSL* ssl, byte* output)
  8132. {
  8133. XMEMCPY(output, ssl->sigSpec, ssl->sigSpecSz);
  8134. return ssl->sigSpecSz;
  8135. }
  8136. static int TLSX_UseCKS(TLSX** extensions, WOLFSSL* ssl, void* heap)
  8137. {
  8138. int ret = 0;
  8139. TLSX* extension;
  8140. if (extensions == NULL) {
  8141. return BAD_FUNC_ARG;
  8142. }
  8143. extension = TLSX_Find(*extensions, TLSX_CKS);
  8144. /* If it is already present, do nothing. */
  8145. if (extension == NULL) {
  8146. /* The data required is in the ssl struct, so push it in. */
  8147. ret = TLSX_Push(extensions, TLSX_CKS, (void*)ssl, heap);
  8148. }
  8149. return ret;
  8150. }
  8151. int TLSX_CKS_Set(WOLFSSL* ssl, TLSX** extensions)
  8152. {
  8153. int ret;
  8154. TLSX* extension;
  8155. /* Push new KeyShare extension. This will also free the old one */
  8156. ret = TLSX_Push(extensions, TLSX_CKS, NULL, ssl->heap);
  8157. if (ret != 0)
  8158. return ret;
  8159. /* Extension got pushed to head */
  8160. extension = *extensions;
  8161. /* Need ssl->sigSpecSz during extension length calculation. */
  8162. extension->data = ssl;
  8163. /* Set extension to be in response. */
  8164. extension->resp = 1;
  8165. return ret;
  8166. }
  8167. int TLSX_CKS_Parse(WOLFSSL* ssl, byte* input, word16 length,
  8168. TLSX** extensions)
  8169. {
  8170. (void) extensions;
  8171. int ret;
  8172. int i, j;
  8173. /* Validating the input. */
  8174. if (length == 0)
  8175. return BUFFER_ERROR;
  8176. for (i = 0; i < length; i++) {
  8177. switch (input[i])
  8178. {
  8179. case WOLFSSL_CKS_SIGSPEC_NATIVE:
  8180. case WOLFSSL_CKS_SIGSPEC_ALTERNATIVE:
  8181. case WOLFSSL_CKS_SIGSPEC_BOTH:
  8182. /* These are all valid values; do nothing */
  8183. break;
  8184. case WOLFSSL_CKS_SIGSPEC_EXTERNAL:
  8185. default:
  8186. /* All other values (including external) are not. */
  8187. return WOLFSSL_NOT_IMPLEMENTED;
  8188. }
  8189. }
  8190. /* Extension data is valid, but if we are the server and we don't have an
  8191. * alt private key, do not respond with CKS extension. */
  8192. if (wolfSSL_is_server(ssl) && ssl->buffers.altKey == NULL) {
  8193. ssl->sigSpec = NULL;
  8194. ssl->sigSpecSz = 0;
  8195. return 0;
  8196. }
  8197. /* Copy as the lifetime of input seems to be ephemeral. */
  8198. ssl->peerSigSpec = (byte*)XMALLOC(length, ssl->heap, DYNAMIC_TYPE_TLSX);
  8199. if (ssl->peerSigSpec == NULL) {
  8200. return BUFFER_ERROR;
  8201. }
  8202. XMEMCPY(ssl->peerSigSpec, input, length);
  8203. ssl->peerSigSpecSz = length;
  8204. /* If there is no preference set, use theirs... */
  8205. if (ssl->sigSpec == NULL) {
  8206. ret = wolfSSL_UseCKS(ssl, ssl->peerSigSpec, 1);
  8207. if (ret == WOLFSSL_SUCCESS) {
  8208. ret = TLSX_UseCKS(&ssl->extensions, ssl, ssl->heap);
  8209. TLSX_SetResponse(ssl, TLSX_CKS);
  8210. }
  8211. return ret;
  8212. }
  8213. /* ...otherwise, prioritize our preference. */
  8214. for (i = 0; i < ssl->sigSpecSz; i++) {
  8215. for (j = 0; j < length; j++) {
  8216. if (ssl->sigSpec[i] == input[j]) {
  8217. /* Got the match, set to this one. */
  8218. ret = wolfSSL_UseCKS(ssl, &ssl->peerSigSpec[i], 1);
  8219. if (ret == WOLFSSL_SUCCESS) {
  8220. ret = TLSX_UseCKS(&ssl->extensions, ssl, ssl->heap);
  8221. TLSX_SetResponse(ssl, TLSX_CKS);
  8222. }
  8223. return ret;
  8224. }
  8225. }
  8226. }
  8227. /* No match found. Cannot continue. */
  8228. return MATCH_SUITE_ERROR;
  8229. }
  8230. #endif /* WOLFSSL_DUAL_ALG_CERTS */
  8231. /* Server side KSE processing */
  8232. int TLSX_KeyShare_Choose(const WOLFSSL *ssl, TLSX* extensions,
  8233. byte cipherSuite0, byte cipherSuite, KeyShareEntry** kse, byte* searched)
  8234. {
  8235. TLSX* extension;
  8236. KeyShareEntry* clientKSE = NULL;
  8237. KeyShareEntry* list = NULL;
  8238. KeyShareEntry* preferredKSE = NULL;
  8239. int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
  8240. int rank;
  8241. (void)cipherSuite0;
  8242. (void)cipherSuite;
  8243. if (ssl == NULL || ssl->options.side != WOLFSSL_SERVER_END)
  8244. return BAD_FUNC_ARG;
  8245. *searched = 0;
  8246. /* Find the KeyShare extension if it exists. */
  8247. extension = TLSX_Find(extensions, TLSX_KEY_SHARE);
  8248. if (extension != NULL)
  8249. list = (KeyShareEntry*)extension->data;
  8250. if (extension && extension->resp == 1) {
  8251. /* Outside of the async case this path should not be taken. */
  8252. int ret = INCOMPLETE_DATA;
  8253. #ifdef WOLFSSL_ASYNC_CRYPT
  8254. /* in async case make sure key generation is finalized */
  8255. KeyShareEntry* serverKSE = (KeyShareEntry*)extension->data;
  8256. if (serverKSE && serverKSE->lastRet == WC_PENDING_E) {
  8257. if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE)
  8258. *searched = 1;
  8259. ret = TLSX_KeyShare_GenKey((WOLFSSL*)ssl, serverKSE);
  8260. }
  8261. #endif
  8262. return ret;
  8263. }
  8264. /* Use server's preference order. */
  8265. for (clientKSE = list; clientKSE != NULL; clientKSE = clientKSE->next) {
  8266. if (clientKSE->ke == NULL)
  8267. continue;
  8268. #ifdef WOLFSSL_SM2
  8269. if ((cipherSuite0 == CIPHER_BYTE) &&
  8270. ((cipherSuite == TLS_SM4_GCM_SM3) ||
  8271. (cipherSuite == TLS_SM4_CCM_SM3))) {
  8272. if (clientKSE->group != WOLFSSL_ECC_SM2P256V1) {
  8273. continue;
  8274. }
  8275. }
  8276. else if (clientKSE->group == WOLFSSL_ECC_SM2P256V1) {
  8277. continue;
  8278. }
  8279. #endif
  8280. /* Check consistency now - extensions in any order. */
  8281. if (!TLSX_SupportedGroups_Find(ssl, clientKSE->group, extensions))
  8282. continue;
  8283. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(clientKSE->group)) {
  8284. /* Check max value supported. */
  8285. if (clientKSE->group > WOLFSSL_ECC_MAX) {
  8286. #ifdef HAVE_PQC
  8287. if (!WOLFSSL_NAMED_GROUP_IS_PQC(clientKSE->group))
  8288. #endif
  8289. continue;
  8290. }
  8291. if (wolfSSL_curve_is_disabled(ssl, clientKSE->group))
  8292. continue;
  8293. }
  8294. if (!TLSX_KeyShare_IsSupported(clientKSE->group))
  8295. continue;
  8296. rank = TLSX_KeyShare_GroupRank(ssl, clientKSE->group);
  8297. if (rank == -1)
  8298. continue;
  8299. if (rank < preferredRank) {
  8300. preferredKSE = clientKSE;
  8301. preferredRank = rank;
  8302. }
  8303. }
  8304. *kse = preferredKSE;
  8305. *searched = 1;
  8306. return 0;
  8307. }
  8308. /* Server side KSE processing */
  8309. int TLSX_KeyShare_Setup(WOLFSSL *ssl, KeyShareEntry* clientKSE)
  8310. {
  8311. int ret;
  8312. TLSX* extension;
  8313. KeyShareEntry* serverKSE;
  8314. KeyShareEntry* list = NULL;
  8315. if (ssl == NULL || ssl->options.side != WOLFSSL_SERVER_END)
  8316. return BAD_FUNC_ARG;
  8317. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  8318. if (extension == NULL)
  8319. return BAD_STATE_E;
  8320. if (clientKSE == NULL) {
  8321. #ifdef WOLFSSL_ASYNC_CRYPT
  8322. /* Not necessarily an error. The key may have already been setup. */
  8323. if (extension != NULL && extension->resp == 1) {
  8324. serverKSE = (KeyShareEntry*)extension->data;
  8325. if (serverKSE != NULL) {
  8326. /* in async case make sure key generation is finalized */
  8327. if (serverKSE->lastRet == WC_PENDING_E)
  8328. return TLSX_KeyShare_GenKey((WOLFSSL*)ssl, serverKSE);
  8329. else if (serverKSE->lastRet == 0)
  8330. return 0;
  8331. }
  8332. }
  8333. #endif
  8334. return BAD_FUNC_ARG;
  8335. }
  8336. /* Generate a new key pair except in the case of OQS KEM because we
  8337. * are going to encapsulate and that does not require us to generate a
  8338. * key pair.
  8339. */
  8340. ret = TLSX_KeyShare_New(&list, clientKSE->group, ssl->heap, &serverKSE);
  8341. if (ret != 0)
  8342. return ret;
  8343. if (clientKSE->key == NULL) {
  8344. #ifdef HAVE_PQC
  8345. if (WOLFSSL_NAMED_GROUP_IS_PQC(clientKSE->group)) {
  8346. /* Going to need the public key (AKA ciphertext). */
  8347. serverKSE->pubKey = clientKSE->pubKey;
  8348. clientKSE->pubKey = NULL;
  8349. serverKSE->pubKeyLen = clientKSE->pubKeyLen;
  8350. clientKSE->pubKeyLen = 0;
  8351. }
  8352. else
  8353. #endif
  8354. {
  8355. ret = TLSX_KeyShare_GenKey(ssl, serverKSE);
  8356. }
  8357. /* for async do setup of serverKSE below, but return WC_PENDING_E */
  8358. if (ret != 0
  8359. #ifdef WOLFSSL_ASYNC_CRYPT
  8360. && ret != WC_PENDING_E
  8361. #endif
  8362. ) {
  8363. TLSX_KeyShare_FreeAll(list, ssl->heap);
  8364. return ret;
  8365. }
  8366. }
  8367. else {
  8368. /* transfer buffers to serverKSE */
  8369. serverKSE->key = clientKSE->key;
  8370. clientKSE->key = NULL;
  8371. serverKSE->keyLen = clientKSE->keyLen;
  8372. serverKSE->pubKey = clientKSE->pubKey;
  8373. clientKSE->pubKey = NULL;
  8374. serverKSE->pubKeyLen = clientKSE->pubKeyLen;
  8375. #ifndef NO_DH
  8376. serverKSE->privKey = clientKSE->privKey;
  8377. clientKSE->privKey = NULL;
  8378. #endif
  8379. }
  8380. serverKSE->ke = clientKSE->ke;
  8381. serverKSE->keLen = clientKSE->keLen;
  8382. clientKSE->ke = NULL;
  8383. clientKSE->keLen = 0;
  8384. ssl->namedGroup = serverKSE->group;
  8385. TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
  8386. extension->data = (void *)serverKSE;
  8387. extension->resp = 1;
  8388. return ret;
  8389. }
  8390. /* Ensure there is a key pair that can be used for key exchange.
  8391. *
  8392. * ssl The SSL/TLS object.
  8393. * doHelloRetry If set to non-zero will do hello_retry
  8394. * returns 0 on success and other values indicate failure.
  8395. */
  8396. int TLSX_KeyShare_Establish(WOLFSSL *ssl, int* doHelloRetry)
  8397. {
  8398. int ret;
  8399. KeyShareEntry* clientKSE = NULL;
  8400. byte searched = 0;
  8401. *doHelloRetry = 0;
  8402. ret = TLSX_KeyShare_Choose(ssl, ssl->extensions, ssl->cipher.cipherSuite0,
  8403. ssl->cipher.cipherSuite, &clientKSE, &searched);
  8404. if (ret != 0 || !searched)
  8405. return ret;
  8406. /* No supported group found - send HelloRetryRequest. */
  8407. if (clientKSE == NULL) {
  8408. /* Set KEY_SHARE_ERROR to indicate HelloRetryRequest required. */
  8409. *doHelloRetry = 1;
  8410. return TLSX_KeyShare_SetSupported(ssl, &ssl->extensions);
  8411. }
  8412. return TLSX_KeyShare_Setup(ssl, clientKSE);
  8413. }
  8414. /* Derive the shared secret of the key exchange.
  8415. *
  8416. * ssl The SSL/TLS object.
  8417. * returns 0 on success and other values indicate failure.
  8418. */
  8419. int TLSX_KeyShare_DeriveSecret(WOLFSSL *ssl)
  8420. {
  8421. int ret;
  8422. TLSX* extension;
  8423. KeyShareEntry* list = NULL;
  8424. #ifdef WOLFSSL_ASYNC_CRYPT
  8425. ret = wolfSSL_AsyncPop(ssl, NULL);
  8426. /* Check for error */
  8427. if (ret != WC_NO_PENDING_E && ret < 0) {
  8428. return ret;
  8429. }
  8430. #endif
  8431. /* Find the KeyShare extension if it exists. */
  8432. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  8433. if (extension != NULL)
  8434. list = (KeyShareEntry*)extension->data;
  8435. if (list == NULL)
  8436. return KEY_SHARE_ERROR;
  8437. /* Calculate secret. */
  8438. ret = TLSX_KeyShare_Process(ssl, list);
  8439. return ret;
  8440. }
  8441. #define KS_FREE_ALL TLSX_KeyShare_FreeAll
  8442. #define KS_GET_SIZE TLSX_KeyShare_GetSize
  8443. #define KS_WRITE TLSX_KeyShare_Write
  8444. #define KS_PARSE TLSX_KeyShare_Parse
  8445. #else
  8446. #define KS_FREE_ALL(a, b) WC_DO_NOTHING
  8447. #define KS_GET_SIZE(a, b) 0
  8448. #define KS_WRITE(a, b, c) 0
  8449. #define KS_PARSE(a, b, c, d) 0
  8450. #endif /* WOLFSSL_TLS13 */
  8451. /******************************************************************************/
  8452. /* Pre-Shared Key */
  8453. /******************************************************************************/
  8454. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  8455. /* Free the pre-shared key dynamic data.
  8456. *
  8457. * list The linked list of key share entry objects.
  8458. * heap The heap used for allocation.
  8459. */
  8460. static void TLSX_PreSharedKey_FreeAll(PreSharedKey* list, void* heap)
  8461. {
  8462. PreSharedKey* current;
  8463. while ((current = list) != NULL) {
  8464. list = current->next;
  8465. XFREE(current->identity, heap, DYNAMIC_TYPE_TLSX);
  8466. XFREE(current, heap, DYNAMIC_TYPE_TLSX);
  8467. }
  8468. (void)heap;
  8469. }
  8470. /* Get the size of the encoded pre shared key extension.
  8471. *
  8472. * list The linked list of pre-shared key extensions.
  8473. * msgType The type of the message this extension is being written into.
  8474. * returns the number of bytes of the encoded pre-shared key extension or
  8475. * SANITY_MSG_E to indicate invalid message type.
  8476. */
  8477. static int TLSX_PreSharedKey_GetSize(PreSharedKey* list, byte msgType,
  8478. word16* pSz)
  8479. {
  8480. if (msgType == client_hello) {
  8481. /* Length of identities + Length of binders. */
  8482. word16 len = OPAQUE16_LEN + OPAQUE16_LEN;
  8483. while (list != NULL) {
  8484. /* Each entry has: identity, ticket age and binder. */
  8485. len += OPAQUE16_LEN + list->identityLen + OPAQUE32_LEN +
  8486. OPAQUE8_LEN + (word16)list->binderLen;
  8487. list = list->next;
  8488. }
  8489. *pSz += len;
  8490. return 0;
  8491. }
  8492. if (msgType == server_hello) {
  8493. *pSz += OPAQUE16_LEN;
  8494. return 0;
  8495. }
  8496. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8497. return SANITY_MSG_E;
  8498. }
  8499. /* The number of bytes to be written for the binders.
  8500. *
  8501. * list The linked list of pre-shared key extensions.
  8502. * msgType The type of the message this extension is being written into.
  8503. * returns the number of bytes of the encoded pre-shared key extension or
  8504. * SANITY_MSG_E to indicate invalid message type.
  8505. */
  8506. int TLSX_PreSharedKey_GetSizeBinders(PreSharedKey* list, byte msgType,
  8507. word16* pSz)
  8508. {
  8509. word16 len;
  8510. if (msgType != client_hello) {
  8511. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8512. return SANITY_MSG_E;
  8513. }
  8514. /* Length of all binders. */
  8515. len = OPAQUE16_LEN;
  8516. while (list != NULL) {
  8517. len += OPAQUE8_LEN + (word16)list->binderLen;
  8518. list = list->next;
  8519. }
  8520. *pSz = len;
  8521. return 0;
  8522. }
  8523. /* Writes the pre-shared key extension into the output buffer - binders only.
  8524. * Assumes that the the output buffer is big enough to hold data.
  8525. *
  8526. * list The linked list of key share entries.
  8527. * output The buffer to write into.
  8528. * msgType The type of the message this extension is being written into.
  8529. * returns the number of bytes written into the buffer.
  8530. */
  8531. int TLSX_PreSharedKey_WriteBinders(PreSharedKey* list, byte* output,
  8532. byte msgType, word16* pSz)
  8533. {
  8534. PreSharedKey* current = list;
  8535. word16 idx = 0;
  8536. word16 lenIdx;
  8537. word16 len;
  8538. if (msgType != client_hello) {
  8539. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8540. return SANITY_MSG_E;
  8541. }
  8542. /* Skip length of all binders. */
  8543. lenIdx = idx;
  8544. idx += OPAQUE16_LEN;
  8545. while (current != NULL) {
  8546. /* Binder data length. */
  8547. output[idx++] = (byte)current->binderLen;
  8548. /* Binder data. */
  8549. XMEMCPY(output + idx, current->binder, current->binderLen);
  8550. idx += (word16)current->binderLen;
  8551. current = current->next;
  8552. }
  8553. /* Length of the binders. */
  8554. len = idx - lenIdx - OPAQUE16_LEN;
  8555. c16toa(len, output + lenIdx);
  8556. *pSz = idx;
  8557. return 0;
  8558. }
  8559. /* Writes the pre-shared key extension into the output buffer.
  8560. * Assumes that the the output buffer is big enough to hold data.
  8561. *
  8562. * list The linked list of key share entries.
  8563. * output The buffer to write into.
  8564. * msgType The type of the message this extension is being written into.
  8565. * returns the number of bytes written into the buffer.
  8566. */
  8567. static int TLSX_PreSharedKey_Write(PreSharedKey* list, byte* output,
  8568. byte msgType, word16* pSz)
  8569. {
  8570. if (msgType == client_hello) {
  8571. PreSharedKey* current = list;
  8572. word16 idx = 0;
  8573. word16 lenIdx;
  8574. word16 len;
  8575. int ret;
  8576. /* Write identities only. Binders after HMACing over this. */
  8577. lenIdx = idx;
  8578. idx += OPAQUE16_LEN;
  8579. while (current != NULL) {
  8580. /* Identity length */
  8581. c16toa(current->identityLen, output + idx);
  8582. idx += OPAQUE16_LEN;
  8583. /* Identity data */
  8584. XMEMCPY(output + idx, current->identity, current->identityLen);
  8585. idx += current->identityLen;
  8586. /* Obfuscated ticket age. */
  8587. c32toa(current->ticketAge, output + idx);
  8588. idx += OPAQUE32_LEN;
  8589. current = current->next;
  8590. }
  8591. /* Length of the identities. */
  8592. len = idx - lenIdx - OPAQUE16_LEN;
  8593. c16toa(len, output + lenIdx);
  8594. /* Don't include binders here.
  8595. * The binders are based on the hash of all the ClientHello data up to
  8596. * and include the identities written above.
  8597. */
  8598. ret = TLSX_PreSharedKey_GetSizeBinders(list, msgType, &len);
  8599. if (ret < 0)
  8600. return ret;
  8601. *pSz += idx + len;
  8602. }
  8603. else if (msgType == server_hello) {
  8604. word16 i;
  8605. /* Find the index of the chosen identity. */
  8606. for (i=0; list != NULL && !list->chosen; i++)
  8607. list = list->next;
  8608. if (list == NULL) {
  8609. WOLFSSL_ERROR_VERBOSE(BUILD_MSG_ERROR);
  8610. return BUILD_MSG_ERROR;
  8611. }
  8612. /* The index of the identity chosen by the server from the list supplied
  8613. * by the client.
  8614. */
  8615. c16toa(i, output);
  8616. *pSz += OPAQUE16_LEN;
  8617. }
  8618. else {
  8619. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8620. return SANITY_MSG_E;
  8621. }
  8622. return 0;
  8623. }
  8624. int TLSX_PreSharedKey_Parse_ClientHello(TLSX** extensions, const byte* input,
  8625. word16 length, void* heap)
  8626. {
  8627. int ret;
  8628. word16 len;
  8629. word16 idx = 0;
  8630. TLSX* extension;
  8631. PreSharedKey* list;
  8632. TLSX_Remove(extensions, TLSX_PRE_SHARED_KEY, heap);
  8633. /* Length of identities and of binders. */
  8634. if ((int)(length - idx) < OPAQUE16_LEN + OPAQUE16_LEN)
  8635. return BUFFER_E;
  8636. /* Length of identities. */
  8637. ato16(input + idx, &len);
  8638. idx += OPAQUE16_LEN;
  8639. if (len < MIN_PSK_ID_LEN || length - idx < len)
  8640. return BUFFER_E;
  8641. /* Create a pre-shared key object for each identity. */
  8642. while (len > 0) {
  8643. const byte* identity;
  8644. word16 identityLen;
  8645. word32 age;
  8646. if (len < OPAQUE16_LEN)
  8647. return BUFFER_E;
  8648. /* Length of identity. */
  8649. ato16(input + idx, &identityLen);
  8650. idx += OPAQUE16_LEN;
  8651. if (len < OPAQUE16_LEN + identityLen + OPAQUE32_LEN ||
  8652. identityLen > MAX_PSK_ID_LEN)
  8653. return BUFFER_E;
  8654. /* Cache identity pointer. */
  8655. identity = input + idx;
  8656. idx += identityLen;
  8657. /* Ticket age. */
  8658. ato32(input + idx, &age);
  8659. idx += OPAQUE32_LEN;
  8660. ret = TLSX_PreSharedKey_Use(extensions, identity, identityLen, age, no_mac,
  8661. 0, 0, 1, NULL, heap);
  8662. if (ret != 0)
  8663. return ret;
  8664. /* Done with this identity. */
  8665. len -= OPAQUE16_LEN + identityLen + OPAQUE32_LEN;
  8666. }
  8667. /* Find the list of identities sent to server. */
  8668. extension = TLSX_Find(*extensions, TLSX_PRE_SHARED_KEY);
  8669. if (extension == NULL)
  8670. return PSK_KEY_ERROR;
  8671. list = (PreSharedKey*)extension->data;
  8672. /* Length of binders. */
  8673. if (idx + OPAQUE16_LEN > length)
  8674. return BUFFER_E;
  8675. ato16(input + idx, &len);
  8676. idx += OPAQUE16_LEN;
  8677. if (len < MIN_PSK_BINDERS_LEN || length - idx < len)
  8678. return BUFFER_E;
  8679. /* Set binder for each identity. */
  8680. while (list != NULL && len > 0) {
  8681. /* Length of binder */
  8682. list->binderLen = input[idx++];
  8683. if (list->binderLen < WC_SHA256_DIGEST_SIZE ||
  8684. list->binderLen > WC_MAX_DIGEST_SIZE)
  8685. return BUFFER_E;
  8686. if (len < OPAQUE8_LEN + list->binderLen)
  8687. return BUFFER_E;
  8688. /* Copy binder into static buffer. */
  8689. XMEMCPY(list->binder, input + idx, list->binderLen);
  8690. idx += (word16)list->binderLen;
  8691. /* Done with binder entry. */
  8692. len -= OPAQUE8_LEN + (word16)list->binderLen;
  8693. /* Next identity. */
  8694. list = list->next;
  8695. }
  8696. if (list != NULL || len != 0)
  8697. return BUFFER_E;
  8698. return 0;
  8699. }
  8700. /* Parse the pre-shared key extension.
  8701. * Different formats in different messages.
  8702. *
  8703. * ssl The SSL/TLS object.
  8704. * input The extension data.
  8705. * length The length of the extension data.
  8706. * msgType The type of the message this extension is being parsed from.
  8707. * returns 0 on success and other values indicate failure.
  8708. */
  8709. static int TLSX_PreSharedKey_Parse(WOLFSSL* ssl, const byte* input,
  8710. word16 length, byte msgType)
  8711. {
  8712. if (msgType == client_hello) {
  8713. return TLSX_PreSharedKey_Parse_ClientHello(&ssl->extensions, input,
  8714. length, ssl->heap);
  8715. }
  8716. if (msgType == server_hello) {
  8717. word16 idx;
  8718. PreSharedKey* list;
  8719. TLSX* extension;
  8720. /* Index of identity chosen by server. */
  8721. if (length != OPAQUE16_LEN)
  8722. return BUFFER_E;
  8723. ato16(input, &idx);
  8724. #ifdef WOLFSSL_EARLY_DATA
  8725. ssl->options.pskIdIndex = idx + 1;
  8726. #endif
  8727. /* Find the list of identities sent to server. */
  8728. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8729. if (extension == NULL)
  8730. return PSK_KEY_ERROR;
  8731. list = (PreSharedKey*)extension->data;
  8732. /* Mark the identity as chosen. */
  8733. for (; list != NULL && idx > 0; idx--)
  8734. list = list->next;
  8735. if (list == NULL) {
  8736. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8737. return PSK_KEY_ERROR;
  8738. }
  8739. list->chosen = 1;
  8740. #ifdef HAVE_SESSION_TICKET
  8741. if (list->resumption) {
  8742. /* Check that the session's details are the same as the server's. */
  8743. if (ssl->options.cipherSuite0 != ssl->session->cipherSuite0 ||
  8744. ssl->options.cipherSuite != ssl->session->cipherSuite ||
  8745. ssl->session->version.major != ssl->ctx->method->version.major ||
  8746. ssl->session->version.minor != ssl->ctx->method->version.minor) {
  8747. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8748. return PSK_KEY_ERROR;
  8749. }
  8750. }
  8751. #endif
  8752. return 0;
  8753. }
  8754. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8755. return SANITY_MSG_E;
  8756. }
  8757. /* Create a new pre-shared key and put it into the list.
  8758. *
  8759. * list The linked list of pre-shared key.
  8760. * identity The identity.
  8761. * len The length of the identity data.
  8762. * heap The memory to allocate with.
  8763. * preSharedKey The new pre-shared key object.
  8764. * returns 0 on success and other values indicate failure.
  8765. */
  8766. static int TLSX_PreSharedKey_New(PreSharedKey** list, const byte* identity,
  8767. word16 len, void *heap,
  8768. PreSharedKey** preSharedKey)
  8769. {
  8770. PreSharedKey* psk;
  8771. PreSharedKey** next;
  8772. psk = (PreSharedKey*)XMALLOC(sizeof(PreSharedKey), heap, DYNAMIC_TYPE_TLSX);
  8773. if (psk == NULL)
  8774. return MEMORY_E;
  8775. XMEMSET(psk, 0, sizeof(*psk));
  8776. /* Make a copy of the identity data. */
  8777. psk->identity = (byte*)XMALLOC(len + NULL_TERM_LEN, heap,
  8778. DYNAMIC_TYPE_TLSX);
  8779. if (psk->identity == NULL) {
  8780. XFREE(psk, heap, DYNAMIC_TYPE_TLSX);
  8781. return MEMORY_E;
  8782. }
  8783. XMEMCPY(psk->identity, identity, len);
  8784. psk->identityLen = len;
  8785. /* Use a NULL terminator in case it is a C string */
  8786. psk->identity[psk->identityLen] = '\0';
  8787. /* Add it to the end and maintain the links. */
  8788. while (*list != NULL) {
  8789. /* Assign to temporary to work around compiler bug found by customer. */
  8790. next = &((*list)->next);
  8791. list = next;
  8792. }
  8793. *list = psk;
  8794. *preSharedKey = psk;
  8795. (void)heap;
  8796. return 0;
  8797. }
  8798. static WC_INLINE byte GetHmacLength(int hmac)
  8799. {
  8800. switch (hmac) {
  8801. #ifndef NO_SHA256
  8802. case sha256_mac:
  8803. return WC_SHA256_DIGEST_SIZE;
  8804. #endif
  8805. #ifdef WOLFSSL_SHA384
  8806. case sha384_mac:
  8807. return WC_SHA384_DIGEST_SIZE;
  8808. #endif
  8809. #ifdef WOLFSSL_SHA512
  8810. case sha512_mac:
  8811. return WC_SHA512_DIGEST_SIZE;
  8812. #endif
  8813. #ifdef WOLFSSL_SM3
  8814. case sm3_mac:
  8815. return WC_SM3_DIGEST_SIZE;
  8816. #endif
  8817. default:
  8818. break;
  8819. }
  8820. return 0;
  8821. }
  8822. /* Use the data to create a new pre-shared key object in the extensions.
  8823. *
  8824. * ssl The SSL/TLS object.
  8825. * identity The identity.
  8826. * len The length of the identity data.
  8827. * age The age of the identity.
  8828. * hmac The HMAC algorithm.
  8829. * cipherSuite0 The first byte of the cipher suite to use.
  8830. * cipherSuite The second byte of the cipher suite to use.
  8831. * resumption The PSK is for resumption of a session.
  8832. * preSharedKey The new pre-shared key object.
  8833. * returns 0 on success and other values indicate failure.
  8834. */
  8835. int TLSX_PreSharedKey_Use(TLSX** extensions, const byte* identity, word16 len,
  8836. word32 age, byte hmac, byte cipherSuite0,
  8837. byte cipherSuite, byte resumption,
  8838. PreSharedKey **preSharedKey, void* heap)
  8839. {
  8840. int ret = 0;
  8841. TLSX* extension;
  8842. PreSharedKey* psk = NULL;
  8843. /* Find the pre-shared key extension if it exists. */
  8844. extension = TLSX_Find(*extensions, TLSX_PRE_SHARED_KEY);
  8845. if (extension == NULL) {
  8846. /* Push new pre-shared key extension. */
  8847. ret = TLSX_Push(extensions, TLSX_PRE_SHARED_KEY, NULL, heap);
  8848. if (ret != 0)
  8849. return ret;
  8850. extension = TLSX_Find(*extensions, TLSX_PRE_SHARED_KEY);
  8851. if (extension == NULL)
  8852. return MEMORY_E;
  8853. }
  8854. /* Try to find the pre-shared key with this identity. */
  8855. psk = (PreSharedKey*)extension->data;
  8856. while (psk != NULL) {
  8857. if ((psk->identityLen == len) &&
  8858. (XMEMCMP(psk->identity, identity, len) == 0)) {
  8859. break;
  8860. }
  8861. psk = psk->next;
  8862. }
  8863. /* Create a new pre-shared key object if not found. */
  8864. if (psk == NULL) {
  8865. ret = TLSX_PreSharedKey_New((PreSharedKey**)&extension->data, identity,
  8866. len, heap, &psk);
  8867. if (ret != 0)
  8868. return ret;
  8869. }
  8870. /* Update/set age and HMAC algorithm. */
  8871. psk->ticketAge = age;
  8872. psk->hmac = hmac;
  8873. psk->cipherSuite0 = cipherSuite0;
  8874. psk->cipherSuite = cipherSuite;
  8875. psk->resumption = resumption;
  8876. psk->binderLen = GetHmacLength(psk->hmac);
  8877. if (preSharedKey != NULL)
  8878. *preSharedKey = psk;
  8879. return 0;
  8880. }
  8881. #define PSK_FREE_ALL TLSX_PreSharedKey_FreeAll
  8882. #define PSK_GET_SIZE TLSX_PreSharedKey_GetSize
  8883. #define PSK_WRITE TLSX_PreSharedKey_Write
  8884. #define PSK_PARSE TLSX_PreSharedKey_Parse
  8885. #else
  8886. #define PSK_FREE_ALL(a, b) WC_DO_NOTHING
  8887. #define PSK_GET_SIZE(a, b, c) 0
  8888. #define PSK_WRITE(a, b, c, d) 0
  8889. #define PSK_PARSE(a, b, c, d) 0
  8890. #endif
  8891. /******************************************************************************/
  8892. /* PSK Key Exchange Modes */
  8893. /******************************************************************************/
  8894. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  8895. /* Get the size of the encoded PSK KE modes extension.
  8896. * Only in ClientHello.
  8897. *
  8898. * modes The PSK KE mode bit string.
  8899. * msgType The type of the message this extension is being written into.
  8900. * returns the number of bytes of the encoded PSK KE mode extension.
  8901. */
  8902. static int TLSX_PskKeModes_GetSize(byte modes, byte msgType, word16* pSz)
  8903. {
  8904. if (msgType == client_hello) {
  8905. /* Format: Len | Modes* */
  8906. word16 len = OPAQUE8_LEN;
  8907. /* Check whether each possible mode is to be written. */
  8908. if (modes & (1 << PSK_KE))
  8909. len += OPAQUE8_LEN;
  8910. if (modes & (1 << PSK_DHE_KE))
  8911. len += OPAQUE8_LEN;
  8912. *pSz += len;
  8913. return 0;
  8914. }
  8915. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8916. return SANITY_MSG_E;
  8917. }
  8918. /* Writes the PSK KE modes extension into the output buffer.
  8919. * Assumes that the the output buffer is big enough to hold data.
  8920. * Only in ClientHello.
  8921. *
  8922. * modes The PSK KE mode bit string.
  8923. * output The buffer to write into.
  8924. * msgType The type of the message this extension is being written into.
  8925. * returns the number of bytes written into the buffer.
  8926. */
  8927. static int TLSX_PskKeModes_Write(byte modes, byte* output, byte msgType,
  8928. word16* pSz)
  8929. {
  8930. if (msgType == client_hello) {
  8931. /* Format: Len | Modes* */
  8932. word16 idx = OPAQUE8_LEN;
  8933. /* Write out each possible mode. */
  8934. if (modes & (1 << PSK_KE))
  8935. output[idx++] = PSK_KE;
  8936. if (modes & (1 << PSK_DHE_KE))
  8937. output[idx++] = PSK_DHE_KE;
  8938. /* Write out length of mode list. */
  8939. output[0] = (byte)(idx - OPAQUE8_LEN);
  8940. *pSz += idx;
  8941. return 0;
  8942. }
  8943. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8944. return SANITY_MSG_E;
  8945. }
  8946. int TLSX_PskKeyModes_Parse_Modes(const byte* input, word16 length, byte msgType,
  8947. byte* modes)
  8948. {
  8949. if (msgType == client_hello) {
  8950. /* Format: Len | Modes* */
  8951. int idx = 0;
  8952. word16 len;
  8953. *modes = 0;
  8954. /* Ensure length byte exists. */
  8955. if (length < OPAQUE8_LEN)
  8956. return BUFFER_E;
  8957. /* Get length of mode list and ensure that is the only data. */
  8958. len = input[0];
  8959. if (length - OPAQUE8_LEN != len)
  8960. return BUFFER_E;
  8961. idx = OPAQUE8_LEN;
  8962. /* Set a bit for each recognized modes. */
  8963. while (len > 0) {
  8964. /* Ignore unrecognized modes. */
  8965. if (input[idx] <= PSK_DHE_KE)
  8966. *modes |= 1 << input[idx];
  8967. idx++;
  8968. len--;
  8969. }
  8970. return 0;
  8971. }
  8972. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8973. return SANITY_MSG_E;
  8974. }
  8975. /* Parse the PSK KE modes extension.
  8976. * Only in ClientHello.
  8977. *
  8978. * ssl The SSL/TLS object.
  8979. * input The extension data.
  8980. * length The length of the extension data.
  8981. * msgType The type of the message this extension is being parsed from.
  8982. * returns 0 on success and other values indicate failure.
  8983. */
  8984. static int TLSX_PskKeModes_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  8985. byte msgType)
  8986. {
  8987. int ret;
  8988. byte modes;
  8989. ret = TLSX_PskKeyModes_Parse_Modes(input, length, msgType, &modes);
  8990. if (ret == 0)
  8991. ret = TLSX_PskKeyModes_Use(ssl, modes);
  8992. if (ret != 0) {
  8993. WOLFSSL_ERROR_VERBOSE(ret);
  8994. }
  8995. return ret;
  8996. }
  8997. /* Use the data to create a new PSK Key Exchange Modes object in the extensions.
  8998. *
  8999. * ssl The SSL/TLS object.
  9000. * modes The PSK key exchange modes.
  9001. * returns 0 on success and other values indicate failure.
  9002. */
  9003. int TLSX_PskKeyModes_Use(WOLFSSL* ssl, byte modes)
  9004. {
  9005. int ret = 0;
  9006. TLSX* extension;
  9007. /* Find the PSK key exchange modes extension if it exists. */
  9008. extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
  9009. if (extension == NULL) {
  9010. /* Push new PSK key exchange modes extension. */
  9011. ret = TLSX_Push(&ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES, NULL,
  9012. ssl->heap);
  9013. if (ret != 0)
  9014. return ret;
  9015. extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
  9016. if (extension == NULL)
  9017. return MEMORY_E;
  9018. }
  9019. extension->val = modes;
  9020. return 0;
  9021. }
  9022. #define PKM_GET_SIZE TLSX_PskKeModes_GetSize
  9023. #define PKM_WRITE TLSX_PskKeModes_Write
  9024. #define PKM_PARSE TLSX_PskKeModes_Parse
  9025. #else
  9026. #define PKM_GET_SIZE(a, b, c) 0
  9027. #define PKM_WRITE(a, b, c, d) 0
  9028. #define PKM_PARSE(a, b, c, d) 0
  9029. #endif
  9030. /******************************************************************************/
  9031. /* Post-Handshake Authentication */
  9032. /******************************************************************************/
  9033. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  9034. /* Get the size of the encoded Post-Handshake Authentication extension.
  9035. * Only in ClientHello.
  9036. *
  9037. * msgType The type of the message this extension is being written into.
  9038. * returns the number of bytes of the encoded Post-Handshake Authentication
  9039. * extension.
  9040. */
  9041. static int TLSX_PostHandAuth_GetSize(byte msgType, word16* pSz)
  9042. {
  9043. if (msgType == client_hello) {
  9044. *pSz += 0;
  9045. return 0;
  9046. }
  9047. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9048. return SANITY_MSG_E;
  9049. }
  9050. /* Writes the Post-Handshake Authentication extension into the output buffer.
  9051. * Assumes that the the output buffer is big enough to hold data.
  9052. * Only in ClientHello.
  9053. *
  9054. * output The buffer to write into.
  9055. * msgType The type of the message this extension is being written into.
  9056. * returns the number of bytes written into the buffer.
  9057. */
  9058. static int TLSX_PostHandAuth_Write(byte* output, byte msgType, word16* pSz)
  9059. {
  9060. (void)output;
  9061. if (msgType == client_hello) {
  9062. *pSz += 0;
  9063. return 0;
  9064. }
  9065. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9066. return SANITY_MSG_E;
  9067. }
  9068. /* Parse the Post-Handshake Authentication extension.
  9069. * Only in ClientHello.
  9070. *
  9071. * ssl The SSL/TLS object.
  9072. * input The extension data.
  9073. * length The length of the extension data.
  9074. * msgType The type of the message this extension is being parsed from.
  9075. * returns 0 on success and other values indicate failure.
  9076. */
  9077. static int TLSX_PostHandAuth_Parse(WOLFSSL* ssl, const byte* input,
  9078. word16 length, byte msgType)
  9079. {
  9080. (void)input;
  9081. if (msgType == client_hello) {
  9082. /* Ensure extension is empty. */
  9083. if (length != 0)
  9084. return BUFFER_E;
  9085. ssl->options.postHandshakeAuth = 1;
  9086. return 0;
  9087. }
  9088. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9089. return SANITY_MSG_E;
  9090. }
  9091. /* Create a new Post-handshake authentication object in the extensions.
  9092. *
  9093. * ssl The SSL/TLS object.
  9094. * returns 0 on success and other values indicate failure.
  9095. */
  9096. static int TLSX_PostHandAuth_Use(WOLFSSL* ssl)
  9097. {
  9098. int ret = 0;
  9099. TLSX* extension;
  9100. /* Find the PSK key exchange modes extension if it exists. */
  9101. extension = TLSX_Find(ssl->extensions, TLSX_POST_HANDSHAKE_AUTH);
  9102. if (extension == NULL) {
  9103. /* Push new Post-handshake Authentication extension. */
  9104. ret = TLSX_Push(&ssl->extensions, TLSX_POST_HANDSHAKE_AUTH, NULL,
  9105. ssl->heap);
  9106. if (ret != 0)
  9107. return ret;
  9108. }
  9109. return 0;
  9110. }
  9111. #define PHA_GET_SIZE TLSX_PostHandAuth_GetSize
  9112. #define PHA_WRITE TLSX_PostHandAuth_Write
  9113. #define PHA_PARSE TLSX_PostHandAuth_Parse
  9114. #else
  9115. #define PHA_GET_SIZE(a, b) 0
  9116. #define PHA_WRITE(a, b, c) 0
  9117. #define PHA_PARSE(a, b, c, d) 0
  9118. #endif
  9119. /******************************************************************************/
  9120. /* Early Data Indication */
  9121. /******************************************************************************/
  9122. #ifdef WOLFSSL_EARLY_DATA
  9123. /* Get the size of the encoded Early Data Indication extension.
  9124. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  9125. *
  9126. * msgType The type of the message this extension is being written into.
  9127. * returns the number of bytes of the encoded Early Data Indication extension.
  9128. */
  9129. static int TLSX_EarlyData_GetSize(byte msgType, word16* pSz)
  9130. {
  9131. int ret = 0;
  9132. if (msgType == client_hello || msgType == encrypted_extensions)
  9133. *pSz += 0;
  9134. else if (msgType == session_ticket)
  9135. *pSz += OPAQUE32_LEN;
  9136. else {
  9137. ret = SANITY_MSG_E;
  9138. WOLFSSL_ERROR_VERBOSE(ret);
  9139. }
  9140. return ret;
  9141. }
  9142. /* Writes the Early Data Indicator extension into the output buffer.
  9143. * Assumes that the the output buffer is big enough to hold data.
  9144. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  9145. *
  9146. * maxSz The maximum early data size.
  9147. * output The buffer to write into.
  9148. * msgType The type of the message this extension is being written into.
  9149. * returns the number of bytes written into the buffer.
  9150. */
  9151. static int TLSX_EarlyData_Write(word32 maxSz, byte* output, byte msgType,
  9152. word16* pSz)
  9153. {
  9154. if (msgType == client_hello || msgType == encrypted_extensions)
  9155. return 0;
  9156. else if (msgType == session_ticket) {
  9157. c32toa(maxSz, output);
  9158. *pSz += OPAQUE32_LEN;
  9159. return 0;
  9160. }
  9161. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9162. return SANITY_MSG_E;
  9163. }
  9164. /* Parse the Early Data Indicator extension.
  9165. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  9166. *
  9167. * ssl The SSL/TLS object.
  9168. * input The extension data.
  9169. * length The length of the extension data.
  9170. * msgType The type of the message this extension is being parsed from.
  9171. * returns 0 on success and other values indicate failure.
  9172. */
  9173. static int TLSX_EarlyData_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  9174. byte msgType)
  9175. {
  9176. WOLFSSL_ENTER("TLSX_EarlyData_Parse");
  9177. if (msgType == client_hello) {
  9178. if (length != 0)
  9179. return BUFFER_E;
  9180. if (ssl->earlyData == expecting_early_data) {
  9181. if (ssl->options.maxEarlyDataSz != 0)
  9182. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_ACCEPTED;
  9183. else
  9184. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_REJECTED;
  9185. return TLSX_EarlyData_Use(ssl, 0, 0);
  9186. }
  9187. ssl->earlyData = early_data_ext;
  9188. return 0;
  9189. }
  9190. if (msgType == encrypted_extensions) {
  9191. if (length != 0)
  9192. return BUFFER_E;
  9193. /* Ensure the index of PSK identity chosen by server is 0.
  9194. * Index is plus one to handle 'not set' value of 0.
  9195. */
  9196. if (ssl->options.pskIdIndex != 1) {
  9197. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  9198. return PSK_KEY_ERROR;
  9199. }
  9200. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  9201. /* the extension from server comes in */
  9202. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_ACCEPTED;
  9203. }
  9204. return TLSX_EarlyData_Use(ssl, 1, 1);
  9205. }
  9206. if (msgType == session_ticket) {
  9207. word32 maxSz;
  9208. if (length != OPAQUE32_LEN)
  9209. return BUFFER_E;
  9210. ato32(input, &maxSz);
  9211. ssl->session->maxEarlyDataSz = maxSz;
  9212. return 0;
  9213. }
  9214. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9215. return SANITY_MSG_E;
  9216. }
  9217. /* Use the data to create a new Early Data object in the extensions.
  9218. *
  9219. * ssl The SSL/TLS object.
  9220. * maxSz The maximum early data size.
  9221. * is_response if this extension is part of a response
  9222. * returns 0 on success and other values indicate failure.
  9223. */
  9224. int TLSX_EarlyData_Use(WOLFSSL* ssl, word32 maxSz, int is_response)
  9225. {
  9226. int ret = 0;
  9227. TLSX* extension;
  9228. /* Find the early data extension if it exists. */
  9229. extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
  9230. if (extension == NULL) {
  9231. /* Push new early data extension. */
  9232. ret = TLSX_Push(&ssl->extensions, TLSX_EARLY_DATA, NULL, ssl->heap);
  9233. if (ret != 0)
  9234. return ret;
  9235. extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
  9236. if (extension == NULL)
  9237. return MEMORY_E;
  9238. }
  9239. extension->resp = is_response;
  9240. /* In QUIC, earlydata size is either 0 or 0xffffffff.
  9241. * Override any size between, possibly left from our initial value */
  9242. extension->val = (WOLFSSL_IS_QUIC(ssl) && is_response && maxSz > 0) ?
  9243. WOLFSSL_MAX_32BIT : maxSz;
  9244. return 0;
  9245. }
  9246. #define EDI_GET_SIZE TLSX_EarlyData_GetSize
  9247. #define EDI_WRITE TLSX_EarlyData_Write
  9248. #define EDI_PARSE TLSX_EarlyData_Parse
  9249. #else
  9250. #define EDI_GET_SIZE(a, b) 0
  9251. #define EDI_WRITE(a, b, c, d) 0
  9252. #define EDI_PARSE(a, b, c, d) 0
  9253. #endif
  9254. /******************************************************************************/
  9255. /* QUIC transport parameter extension */
  9256. /******************************************************************************/
  9257. #ifdef WOLFSSL_QUIC
  9258. static word16 TLSX_QuicTP_GetSize(TLSX* extension)
  9259. {
  9260. const QuicTransportParam *tp = (QuicTransportParam*)extension->data;
  9261. return tp ? tp->len : 0;
  9262. }
  9263. int TLSX_QuicTP_Use(WOLFSSL* ssl, TLSX_Type ext_type, int is_response)
  9264. {
  9265. int ret = 0;
  9266. TLSX* extension;
  9267. WOLFSSL_ENTER("TLSX_QuicTP_Use");
  9268. if (ssl->quic.transport_local == NULL) {
  9269. /* RFC9000, ch 7.3: "An endpoint MUST treat the absence of [...]
  9270. * from either endpoint [...] as a connection error of type
  9271. * TRANSPORT_PARAMETER_ERROR."
  9272. */
  9273. ret = QUIC_TP_MISSING_E;
  9274. goto cleanup;
  9275. }
  9276. extension = TLSX_Find(ssl->extensions, ext_type);
  9277. if (extension == NULL) {
  9278. ret = TLSX_Push(&ssl->extensions, ext_type, NULL, ssl->heap);
  9279. if (ret != 0)
  9280. goto cleanup;
  9281. extension = TLSX_Find(ssl->extensions, ext_type);
  9282. if (extension == NULL) {
  9283. ret = MEMORY_E;
  9284. goto cleanup;
  9285. }
  9286. }
  9287. if (extension->data) {
  9288. QuicTransportParam_free((QuicTransportParam*)extension->data, ssl->heap);
  9289. extension->data = NULL;
  9290. }
  9291. extension->resp = is_response;
  9292. extension->data = (void*)QuicTransportParam_dup(ssl->quic.transport_local, ssl->heap);
  9293. if (!extension->data) {
  9294. ret = MEMORY_E;
  9295. goto cleanup;
  9296. }
  9297. cleanup:
  9298. WOLFSSL_LEAVE("TLSX_QuicTP_Use", ret);
  9299. return ret;
  9300. }
  9301. static word16 TLSX_QuicTP_Write(QuicTransportParam *tp, byte* output)
  9302. {
  9303. word16 len = 0;
  9304. WOLFSSL_ENTER("TLSX_QuicTP_Write");
  9305. if (tp && tp->len) {
  9306. XMEMCPY(output, tp->data, tp->len);
  9307. len = tp->len;
  9308. }
  9309. WOLFSSL_LEAVE("TLSX_QuicTP_Write", len);
  9310. return len;
  9311. }
  9312. static int TLSX_QuicTP_Parse(WOLFSSL *ssl, const byte *input, size_t len, int ext_type, int msgType)
  9313. {
  9314. const QuicTransportParam *tp, **ptp;
  9315. (void)msgType;
  9316. tp = QuicTransportParam_new(input, len, ssl->heap);
  9317. if (!tp) {
  9318. return MEMORY_E;
  9319. }
  9320. ptp = (ext_type == TLSX_KEY_QUIC_TP_PARAMS_DRAFT) ?
  9321. &ssl->quic.transport_peer_draft : &ssl->quic.transport_peer;
  9322. if (*ptp) {
  9323. QTP_FREE(*ptp, ssl->heap);
  9324. }
  9325. *ptp = tp;
  9326. return 0;
  9327. }
  9328. #define QTP_GET_SIZE TLSX_QuicTP_GetSize
  9329. #define QTP_USE TLSX_QuicTP_Use
  9330. #define QTP_WRITE TLSX_QuicTP_Write
  9331. #define QTP_PARSE TLSX_QuicTP_Parse
  9332. #endif /* WOLFSSL_QUIC */
  9333. #if defined(WOLFSSL_DTLS_CID)
  9334. #define CID_GET_SIZE TLSX_ConnectionID_GetSize
  9335. #define CID_WRITE TLSX_ConnectionID_Write
  9336. #define CID_PARSE TLSX_ConnectionID_Parse
  9337. #define CID_FREE TLSX_ConnectionID_Free
  9338. #else
  9339. #define CID_GET_SIZE(a) 0
  9340. #define CID_WRITE(a, b) 0
  9341. #define CID_PARSE(a, b, c, d) 0
  9342. #define CID_FREE(a, b) 0
  9343. #endif /* defined(WOLFSSL_DTLS_CID) */
  9344. #if defined(HAVE_RPK)
  9345. /******************************************************************************/
  9346. /* Client_Certificate_Type extension */
  9347. /******************************************************************************/
  9348. /* return 1 if specified type is included in the given list, otherwise 0 */
  9349. static int IsCertTypeListed(byte type, byte cnt, const byte* list)
  9350. {
  9351. int ret = 0;
  9352. int i;
  9353. if (cnt == 0 || list == NULL)
  9354. return ret;
  9355. if (cnt > 0 && cnt <= MAX_CLIENT_CERT_TYPE_CNT) {
  9356. for (i = 0; i < cnt; i++) {
  9357. if (list[i] == type)
  9358. return 1;
  9359. }
  9360. }
  9361. return 0;
  9362. }
  9363. /* Search both arrays from above to find a common value between the two given
  9364. * arrays(a and b). return 1 if it finds a common value, otherwise return 0.
  9365. */
  9366. static int GetCommonItem(const byte* a, byte aLen, const byte* b, byte bLen,
  9367. byte* type)
  9368. {
  9369. int i, j;
  9370. if (a == NULL || b == NULL)
  9371. return 0;
  9372. for (i = 0; i < aLen; i++) {
  9373. for (j = 0; j < bLen; j++) {
  9374. if (a[i] == b[j]) {
  9375. *type = a[i];
  9376. return 1;
  9377. }
  9378. }
  9379. }
  9380. return 0;
  9381. }
  9382. /* Creates a "client certificate type" extension if necessary.
  9383. * Returns 0 if no error occurred, negative value otherwise.
  9384. * A return of 0, it does not indicae that the extension was created.
  9385. */
  9386. static int TLSX_ClientCertificateType_Use(WOLFSSL* ssl, byte isServer)
  9387. {
  9388. int ret = 0;
  9389. if (ssl == NULL)
  9390. return BAD_FUNC_ARG;
  9391. if (isServer) {
  9392. /* [in server side]
  9393. */
  9394. if (IsCertTypeListed(WOLFSSL_CERT_TYPE_RPK,
  9395. ssl->options.rpkConfig.preferred_ClientCertTypeCnt,
  9396. ssl->options.rpkConfig.preferred_ClientCertTypes)) {
  9397. WOLFSSL_MSG("Adding Client Certificate Type extension");
  9398. ret = TLSX_Push(&ssl->extensions, TLSX_CLIENT_CERTIFICATE_TYPE, ssl,
  9399. ssl->heap);
  9400. if (ret == 0) {
  9401. TLSX_SetResponse(ssl, TLSX_CLIENT_CERTIFICATE_TYPE);
  9402. }
  9403. }
  9404. }
  9405. else {
  9406. /* [in client side]
  9407. * This extension MUST be omitted from the ClientHello unless the RPK
  9408. * certificate is preferred by the user and actually loaded.
  9409. */
  9410. if (IsCertTypeListed(WOLFSSL_CERT_TYPE_RPK,
  9411. ssl->options.rpkConfig.preferred_ClientCertTypeCnt,
  9412. ssl->options.rpkConfig.preferred_ClientCertTypes)) {
  9413. if (ssl->options.rpkState.isRPKLoaded) {
  9414. ssl->options.rpkState.sending_ClientCertTypeCnt = 1;
  9415. ssl->options.rpkState.sending_ClientCertTypes[0] =
  9416. WOLFSSL_CERT_TYPE_RPK;
  9417. /* Push new client_certificate_type extension. */
  9418. WOLFSSL_MSG("Adding Client Certificate Type extension");
  9419. ret = TLSX_Push(&ssl->extensions, TLSX_CLIENT_CERTIFICATE_TYPE,
  9420. ssl, ssl->heap);
  9421. }
  9422. else {
  9423. WOLFSSL_MSG("Willing to use RPK cert but not loaded it");
  9424. }
  9425. }
  9426. else {
  9427. WOLFSSL_MSG("No will to use RPK cert");
  9428. }
  9429. }
  9430. return ret;
  9431. }
  9432. /* Parse a "client certificate type" extension received from peer.
  9433. * returns 0 on success and other values indicate failure.
  9434. */
  9435. static int TLSX_ClientCertificateType_Parse(WOLFSSL* ssl, const byte* input,
  9436. word16 length, byte msgType)
  9437. {
  9438. byte typeCnt;
  9439. int idx = 0;
  9440. int ret = 0;
  9441. int i;
  9442. int populate = 0;
  9443. byte cmnType;
  9444. if (msgType == client_hello) {
  9445. /* [parse ClientHello in server end]
  9446. * case 1) if peer verify is disabled, this extension must be omitted
  9447. * from ServerHello.
  9448. * case 2) if user have not set his preference, find X509 in parsed
  9449. * result, then populate "Client Certificate Type" extension.
  9450. * case 3) if user have not set his preference and X509 isn't included
  9451. * in parsed result, send "unsupported certificate" alert.
  9452. * case 4) if user have set his preference, find a common cert type
  9453. * in users preference and received cert types.
  9454. * case 5) if user have set his preference, but no common cert type
  9455. * found.
  9456. */
  9457. /* case 1 */
  9458. if (ssl->options.verifyNone) {
  9459. return ret;
  9460. }
  9461. /* parse extension */
  9462. if (length < OPAQUE8_LEN)
  9463. return BUFFER_E;
  9464. typeCnt = input[idx];
  9465. if (typeCnt > MAX_CLIENT_CERT_TYPE_CNT)
  9466. return BUFFER_E;
  9467. if ((typeCnt + 1) * OPAQUE8_LEN != length){
  9468. return BUFFER_E;
  9469. }
  9470. ssl->options.rpkState.received_ClientCertTypeCnt = input[idx];
  9471. idx += OPAQUE8_LEN;
  9472. for (i = 0; i < typeCnt; i++) {
  9473. ssl->options.rpkState.received_ClientCertTypes[i] = input[idx];
  9474. idx += OPAQUE8_LEN;
  9475. }
  9476. if (ssl->options.rpkConfig.preferred_ClientCertTypeCnt == 0) {
  9477. /* case 2 */
  9478. if (IsCertTypeListed(WOLFSSL_CERT_TYPE_X509,
  9479. ssl->options.rpkState.received_ClientCertTypeCnt,
  9480. ssl->options.rpkState.received_ClientCertTypes)) {
  9481. ssl->options.rpkState.sending_ClientCertTypeCnt = 1;
  9482. ssl->options.rpkState.sending_ClientCertTypes[0] =
  9483. WOLFSSL_CERT_TYPE_X509;
  9484. populate = 1;
  9485. }
  9486. /* case 3 */
  9487. else {
  9488. WOLFSSL_MSG("No common cert type found in client_certificate_type ext");
  9489. SendAlert(ssl, alert_fatal, unsupported_certificate);
  9490. return UNSUPPORTED_CERTIFICATE;
  9491. }
  9492. }
  9493. else if (ssl->options.rpkConfig.preferred_ClientCertTypeCnt > 0) {
  9494. /* case 4 */
  9495. if (GetCommonItem(
  9496. ssl->options.rpkConfig.preferred_ClientCertTypes,
  9497. ssl->options.rpkConfig.preferred_ClientCertTypeCnt,
  9498. ssl->options.rpkState.received_ClientCertTypes,
  9499. ssl->options.rpkState.received_ClientCertTypeCnt,
  9500. &cmnType)) {
  9501. ssl->options.rpkState.sending_ClientCertTypeCnt = 1;
  9502. ssl->options.rpkState.sending_ClientCertTypes[0] = cmnType;
  9503. populate = 1;
  9504. }
  9505. /* case 5 */
  9506. else {
  9507. WOLFSSL_MSG("No common cert type found in client_certificate_type ext");
  9508. SendAlert(ssl, alert_fatal, unsupported_certificate);
  9509. return UNSUPPORTED_CERTIFICATE;
  9510. }
  9511. }
  9512. /* populate client_certificate_type extension */
  9513. if (populate) {
  9514. WOLFSSL_MSG("Adding Client Certificate Type extension");
  9515. ret = TLSX_Push(&ssl->extensions, TLSX_CLIENT_CERTIFICATE_TYPE, ssl,
  9516. ssl->heap);
  9517. if (ret == 0) {
  9518. TLSX_SetResponse(ssl, TLSX_CLIENT_CERTIFICATE_TYPE);
  9519. }
  9520. }
  9521. }
  9522. else if (msgType == server_hello || msgType == encrypted_extensions) {
  9523. /* parse it in client side */
  9524. if (length == 1) {
  9525. ssl->options.rpkState.received_ClientCertTypeCnt = 1;
  9526. ssl->options.rpkState.received_ClientCertTypes[0] = *input;
  9527. }
  9528. else {
  9529. return BUFFER_E;
  9530. }
  9531. }
  9532. return ret;
  9533. }
  9534. /* Write out the "client certificate type" extension data into the given buffer.
  9535. * return the size wrote in the buffer on success, negative value on error.
  9536. */
  9537. static word16 TLSX_ClientCertificateType_Write(void* data, byte* output,
  9538. byte msgType)
  9539. {
  9540. WOLFSSL* ssl = (WOLFSSL*)data;
  9541. word16 idx = 0;
  9542. byte cnt = 0;
  9543. int i;
  9544. /* skip to write extension if count is zero */
  9545. cnt = ssl->options.rpkState.sending_ClientCertTypeCnt;
  9546. if (cnt == 0)
  9547. return 0;
  9548. if (msgType == client_hello) {
  9549. /* client side */
  9550. *(output + idx) = cnt;
  9551. idx += OPAQUE8_LEN;
  9552. for (i = 0; i < cnt; i++) {
  9553. *(output + idx) = ssl->options.rpkState.sending_ClientCertTypes[i];
  9554. idx += OPAQUE8_LEN;
  9555. }
  9556. return idx;
  9557. }
  9558. else if (msgType == server_hello || msgType == encrypted_extensions) {
  9559. /* sever side */
  9560. if (cnt == 1) {
  9561. *(output + idx) = ssl->options.rpkState.sending_ClientCertTypes[0];
  9562. idx += OPAQUE8_LEN;
  9563. }
  9564. }
  9565. return idx;
  9566. }
  9567. /* Calculate then return the size of the "client certificate type" extension
  9568. * data.
  9569. * return the extension data size on success, negative value on error.
  9570. */
  9571. static int TLSX_ClientCertificateType_GetSize(WOLFSSL* ssl, byte msgType)
  9572. {
  9573. int ret = 0;
  9574. byte cnt;
  9575. if (ssl == NULL)
  9576. return BAD_FUNC_ARG;
  9577. if (msgType == client_hello) {
  9578. /* client side */
  9579. cnt = ssl->options.rpkState.sending_ClientCertTypeCnt;
  9580. ret = (int)(OPAQUE8_LEN + cnt * OPAQUE8_LEN);
  9581. }
  9582. else if (msgType == server_hello || msgType == encrypted_extensions) {
  9583. /* sever side */
  9584. cnt = ssl->options.rpkState.sending_ClientCertTypeCnt;/* must be one */
  9585. ret = OPAQUE8_LEN;
  9586. }
  9587. else {
  9588. return SANITY_MSG_E;
  9589. }
  9590. return ret;
  9591. }
  9592. #define CCT_GET_SIZE TLSX_ClientCertificateType_GetSize
  9593. #define CCT_WRITE TLSX_ClientCertificateType_Write
  9594. #define CCT_PARSE TLSX_ClientCertificateType_Parse
  9595. #else
  9596. #define CCT_GET_SIZE(a) 0
  9597. #define CCT_WRITE(a, b) 0
  9598. #define CCT_PARSE(a, b, c, d) 0
  9599. #endif /* HAVE_RPK */
  9600. #if defined(HAVE_RPK)
  9601. /******************************************************************************/
  9602. /* Server_Certificate_Type extension */
  9603. /******************************************************************************/
  9604. /* Creates a "server certificate type" extension if necessary.
  9605. * Returns 0 if no error occurred, negative value otherwise.
  9606. * A return of 0, it does not indicae that the extension was created.
  9607. */
  9608. static int TLSX_ServerCertificateType_Use(WOLFSSL* ssl, byte isServer)
  9609. {
  9610. int ret = 0;
  9611. byte ctype;
  9612. if (ssl == NULL)
  9613. return BAD_FUNC_ARG;
  9614. if (isServer) {
  9615. /* [in server side] */
  9616. /* find common cert type to both end */
  9617. if (GetCommonItem(
  9618. ssl->options.rpkConfig.preferred_ServerCertTypes,
  9619. ssl->options.rpkConfig.preferred_ServerCertTypeCnt,
  9620. ssl->options.rpkState.received_ServerCertTypes,
  9621. ssl->options.rpkState.received_ServerCertTypeCnt,
  9622. &ctype)) {
  9623. ssl->options.rpkState.sending_ServerCertTypeCnt = 1;
  9624. ssl->options.rpkState.sending_ServerCertTypes[0] = ctype;
  9625. /* Push new server_certificate_type extension. */
  9626. WOLFSSL_MSG("Adding Server Certificate Type extension");
  9627. ret = TLSX_Push(&ssl->extensions, TLSX_SERVER_CERTIFICATE_TYPE, ssl,
  9628. ssl->heap);
  9629. if (ret == 0) {
  9630. TLSX_SetResponse(ssl, TLSX_SERVER_CERTIFICATE_TYPE);
  9631. }
  9632. }
  9633. else {
  9634. /* no common cert type found */
  9635. WOLFSSL_MSG("No common cert type found in server_certificate_type ext");
  9636. SendAlert(ssl, alert_fatal, unsupported_certificate);
  9637. ret = UNSUPPORTED_CERTIFICATE;
  9638. }
  9639. }
  9640. else {
  9641. /* [in client side] */
  9642. if (IsCertTypeListed(WOLFSSL_CERT_TYPE_RPK,
  9643. ssl->options.rpkConfig.preferred_ServerCertTypeCnt,
  9644. ssl->options.rpkConfig.preferred_ServerCertTypes)) {
  9645. ssl->options.rpkState.sending_ServerCertTypeCnt =
  9646. ssl->options.rpkConfig.preferred_ServerCertTypeCnt;
  9647. XMEMCPY(ssl->options.rpkState.sending_ServerCertTypes,
  9648. ssl->options.rpkConfig.preferred_ServerCertTypes,
  9649. ssl->options.rpkConfig.preferred_ServerCertTypeCnt);
  9650. /* Push new server_certificate_type extension. */
  9651. WOLFSSL_MSG("Adding Server Certificate Type extension");
  9652. ret = TLSX_Push(&ssl->extensions, TLSX_SERVER_CERTIFICATE_TYPE, ssl,
  9653. ssl->heap);
  9654. }
  9655. else {
  9656. WOLFSSL_MSG("No will to accept RPK cert");
  9657. }
  9658. }
  9659. return ret;
  9660. }
  9661. /* Parse a "server certificate type" extension received from peer.
  9662. * returns 0 on success and other values indicate failure.
  9663. */
  9664. static int TLSX_ServerCertificateType_Parse(WOLFSSL* ssl, const byte* input,
  9665. word16 length, byte msgType)
  9666. {
  9667. byte typeCnt;
  9668. int idx = 0;
  9669. int ret = 0;
  9670. int i;
  9671. if (msgType == client_hello) {
  9672. /* in server side */
  9673. if (length < OPAQUE8_LEN)
  9674. return BUFFER_E;
  9675. typeCnt = input[idx];
  9676. if (typeCnt > MAX_SERVER_CERT_TYPE_CNT)
  9677. return BUFFER_E;
  9678. if ((typeCnt + 1) * OPAQUE8_LEN != length){
  9679. return BUFFER_E;
  9680. }
  9681. ssl->options.rpkState.received_ServerCertTypeCnt = input[idx];
  9682. idx += OPAQUE8_LEN;
  9683. for (i = 0; i < typeCnt; i++) {
  9684. ssl->options.rpkState.received_ServerCertTypes[i] = input[idx];
  9685. idx += OPAQUE8_LEN;
  9686. }
  9687. ret = TLSX_ServerCertificateType_Use(ssl, 1);
  9688. if (ret == 0) {
  9689. TLSX_SetResponse(ssl, TLSX_SERVER_CERTIFICATE_TYPE);
  9690. }
  9691. }
  9692. else if (msgType == server_hello || msgType == encrypted_extensions) {
  9693. /* in client side */
  9694. if (length != 1) /* length slould be 1 */
  9695. return BUFFER_E;
  9696. ssl->options.rpkState.received_ServerCertTypeCnt = 1;
  9697. ssl->options.rpkState.received_ServerCertTypes[0] = *input;
  9698. }
  9699. return 0;
  9700. }
  9701. /* Write out the "server certificate type" extension data into the given buffer.
  9702. * return the size wrote in the buffer on success, negative value on error.
  9703. */
  9704. static word16 TLSX_ServerCertificateType_Write(void* data, byte* output,
  9705. byte msgType)
  9706. {
  9707. WOLFSSL* ssl = (WOLFSSL*)data;
  9708. word16 idx = 0;
  9709. int cnt = 0;
  9710. int i;
  9711. /* skip to write extension if count is zero */
  9712. cnt = ssl->options.rpkState.sending_ServerCertTypeCnt;
  9713. if (cnt == 0)
  9714. return 0;
  9715. if (msgType == client_hello) {
  9716. /* in client side */
  9717. *(output + idx) = cnt;
  9718. idx += OPAQUE8_LEN;
  9719. for (i = 0; i < cnt; i++) {
  9720. *(output + idx) = ssl->options.rpkState.sending_ServerCertTypes[i];
  9721. idx += OPAQUE8_LEN;
  9722. }
  9723. }
  9724. else if (msgType == server_hello || msgType == encrypted_extensions) {
  9725. /* in server side */
  9726. /* ensure cnt is one */
  9727. if (cnt != 1)
  9728. return 0;
  9729. *(output + idx) = ssl->options.rpkState.sending_ServerCertTypes[0];
  9730. idx += OPAQUE8_LEN;
  9731. }
  9732. return idx;
  9733. }
  9734. /* Calculate then return the size of the "server certificate type" extension
  9735. * data.
  9736. * return the extension data size on success, negative value on error.
  9737. */
  9738. static int TLSX_ServerCertificateType_GetSize(WOLFSSL* ssl, byte msgType)
  9739. {
  9740. int ret = 0;
  9741. int cnt;
  9742. if (ssl == NULL)
  9743. return BAD_FUNC_ARG;
  9744. if (msgType == client_hello) {
  9745. /* in clent side */
  9746. cnt = ssl->options.rpkState.sending_ServerCertTypeCnt;
  9747. if (cnt > 0) {
  9748. ret = (int)(OPAQUE8_LEN + cnt * OPAQUE8_LEN);
  9749. }
  9750. }
  9751. else if (msgType == server_hello || msgType == encrypted_extensions) {
  9752. /* in server side */
  9753. ret = (int)OPAQUE8_LEN;
  9754. }
  9755. else {
  9756. return SANITY_MSG_E;
  9757. }
  9758. return ret;
  9759. }
  9760. #define SCT_GET_SIZE TLSX_ServerCertificateType_GetSize
  9761. #define SCT_WRITE TLSX_ServerCertificateType_Write
  9762. #define SCT_PARSE TLSX_ServerCertificateType_Parse
  9763. #else
  9764. #define SCT_GET_SIZE(a) 0
  9765. #define SCT_WRITE(a, b) 0
  9766. #define SCT_PARSE(a, b, c, d) 0
  9767. #endif /* HAVE_RPK */
  9768. /******************************************************************************/
  9769. /* TLS Extensions Framework */
  9770. /******************************************************************************/
  9771. /** Finds an extension in the provided list. */
  9772. TLSX* TLSX_Find(TLSX* list, TLSX_Type type)
  9773. {
  9774. TLSX* extension = list;
  9775. while (extension && extension->type != type)
  9776. extension = extension->next;
  9777. return extension;
  9778. }
  9779. /** Remove an extension. */
  9780. void TLSX_Remove(TLSX** list, TLSX_Type type, void* heap)
  9781. {
  9782. TLSX* extension;
  9783. TLSX** next;
  9784. if (list == NULL)
  9785. return;
  9786. extension = *list;
  9787. next = list;
  9788. while (extension && extension->type != type) {
  9789. next = &extension->next;
  9790. extension = extension->next;
  9791. }
  9792. if (extension) {
  9793. *next = extension->next;
  9794. extension->next = NULL;
  9795. TLSX_FreeAll(extension, heap);
  9796. }
  9797. }
  9798. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  9799. #define GREASE_ECH_SIZE 160
  9800. #define MAX_PUBLIC_NAME_SZ 256
  9801. #define TLS_INFO_CONST_STRING "tls ech"
  9802. #define TLS_INFO_CONST_STRING_SZ 7
  9803. /* return status after setting up ech to write a grease ech */
  9804. static int TLSX_GreaseECH_Use(TLSX** extensions, void* heap, WC_RNG* rng)
  9805. {
  9806. int ret = 0;
  9807. WOLFSSL_ECH* ech;
  9808. if (extensions == NULL)
  9809. return BAD_FUNC_ARG;
  9810. ech = (WOLFSSL_ECH*)XMALLOC(sizeof(WOLFSSL_ECH), heap,
  9811. DYNAMIC_TYPE_TMP_BUFFER);
  9812. if (ech == NULL)
  9813. return MEMORY_E;
  9814. ForceZero(ech, sizeof(WOLFSSL_ECH));
  9815. ech->state = ECH_WRITE_GREASE;
  9816. /* 0 for outer */
  9817. ech->type = ECH_TYPE_OUTER;
  9818. /* kemId */
  9819. ech->kemId = DHKEM_X25519_HKDF_SHA256;
  9820. /* cipherSuite kdf */
  9821. ech->cipherSuite.kdfId = HKDF_SHA256;
  9822. /* cipherSuite aead */
  9823. ech->cipherSuite.aeadId = HPKE_AES_128_GCM;
  9824. /* random configId */
  9825. ret = wc_RNG_GenerateByte(rng, &(ech->configId));
  9826. /* curve25519 encLen */
  9827. ech->encLen = DHKEM_X25519_ENC_LEN;
  9828. if (ret == 0)
  9829. ret = TLSX_Push(extensions, TLSX_ECH, ech, heap);
  9830. if (ret != 0) {
  9831. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9832. }
  9833. return ret;
  9834. }
  9835. /* return status after setting up ech to write real ech */
  9836. static int TLSX_ECH_Use(WOLFSSL_EchConfig* echConfig, TLSX** extensions,
  9837. void* heap, WC_RNG* rng)
  9838. {
  9839. int ret = 0;
  9840. int suiteIndex;
  9841. WOLFSSL_ECH* ech;
  9842. if (extensions == NULL)
  9843. return BAD_FUNC_ARG;
  9844. /* find a supported cipher suite */
  9845. suiteIndex = EchConfigGetSupportedCipherSuite(echConfig);
  9846. if (suiteIndex < 0)
  9847. return suiteIndex;
  9848. ech = (WOLFSSL_ECH*)XMALLOC(sizeof(WOLFSSL_ECH), heap,
  9849. DYNAMIC_TYPE_TMP_BUFFER);
  9850. if (ech == NULL)
  9851. return MEMORY_E;
  9852. ForceZero(ech, sizeof(WOLFSSL_ECH));
  9853. ech->state = ECH_WRITE_REAL;
  9854. ech->echConfig = echConfig;
  9855. /* 0 for outer */
  9856. ech->type = ECH_TYPE_OUTER;
  9857. /* kemId */
  9858. ech->kemId = echConfig->kemId;
  9859. /* cipherSuite kdf */
  9860. ech->cipherSuite.kdfId = echConfig->cipherSuites[suiteIndex].kdfId;
  9861. /* cipherSuite aead */
  9862. ech->cipherSuite.aeadId = echConfig->cipherSuites[suiteIndex].aeadId;
  9863. /* configId */
  9864. ech->configId = echConfig->configId;
  9865. /* encLen */
  9866. switch (echConfig->kemId)
  9867. {
  9868. case DHKEM_P256_HKDF_SHA256:
  9869. ech->encLen = DHKEM_P256_ENC_LEN;
  9870. break;
  9871. case DHKEM_P384_HKDF_SHA384:
  9872. ech->encLen = DHKEM_P384_ENC_LEN;
  9873. break;
  9874. case DHKEM_P521_HKDF_SHA512:
  9875. ech->encLen = DHKEM_P521_ENC_LEN;
  9876. break;
  9877. case DHKEM_X25519_HKDF_SHA256:
  9878. ech->encLen = DHKEM_X25519_ENC_LEN;
  9879. break;
  9880. case DHKEM_X448_HKDF_SHA512:
  9881. ech->encLen = DHKEM_X448_ENC_LEN;
  9882. break;
  9883. }
  9884. /* setup hpke */
  9885. ech->hpke = (Hpke*)XMALLOC(sizeof(Hpke), heap, DYNAMIC_TYPE_TMP_BUFFER);
  9886. if (ech->hpke == NULL) {
  9887. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9888. return MEMORY_E;
  9889. }
  9890. ret = wc_HpkeInit(ech->hpke, ech->kemId, ech->cipherSuite.kdfId,
  9891. ech->cipherSuite.aeadId, heap);
  9892. /* setup the ephemeralKey */
  9893. if (ret == 0)
  9894. ret = wc_HpkeGenerateKeyPair(ech->hpke, &ech->ephemeralKey, rng);
  9895. if (ret == 0)
  9896. ret = TLSX_Push(extensions, TLSX_ECH, ech, heap);
  9897. if (ret != 0) {
  9898. XFREE(ech->hpke, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9899. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9900. }
  9901. return ret;
  9902. }
  9903. /* return status after setting up ech to read and decrypt */
  9904. static int TLSX_ServerECH_Use(TLSX** extensions, void* heap,
  9905. WOLFSSL_EchConfig* configs)
  9906. {
  9907. int ret;
  9908. WOLFSSL_ECH* ech;
  9909. TLSX* echX;
  9910. if (extensions == NULL)
  9911. return BAD_FUNC_ARG;
  9912. /* if we already have ech don't override it */
  9913. echX = TLSX_Find(*extensions, TLSX_ECH);
  9914. if (echX != NULL)
  9915. return 0;
  9916. ech = (WOLFSSL_ECH*)XMALLOC(sizeof(WOLFSSL_ECH), heap,
  9917. DYNAMIC_TYPE_TMP_BUFFER);
  9918. if (ech == NULL)
  9919. return MEMORY_E;
  9920. ForceZero(ech, sizeof(WOLFSSL_ECH));
  9921. ech->state = ECH_WRITE_NONE;
  9922. /* 0 for outer */
  9923. ech->type = ECH_TYPE_OUTER;
  9924. ech->echConfig = configs;
  9925. /* setup the rest of the settings when we receive ech from the client */
  9926. ret = TLSX_Push(extensions, TLSX_ECH, ech, heap);
  9927. if (ret != 0)
  9928. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9929. return ret;
  9930. }
  9931. /* return length after writing the ech */
  9932. static int TLSX_ECH_Write(WOLFSSL_ECH* ech, byte* writeBuf, word16* offset)
  9933. {
  9934. int ret = 0;
  9935. int rngRet = -1;
  9936. word32 configsLen = 0;
  9937. void* ephemeralKey = NULL;
  9938. byte* writeBuf_p = writeBuf;
  9939. #ifdef WOLFSSL_SMALL_STACK
  9940. Hpke* hpke = NULL;
  9941. WC_RNG* rng = NULL;
  9942. #else
  9943. Hpke hpke[1];
  9944. WC_RNG rng[1];
  9945. #endif
  9946. WOLFSSL_MSG("TLSX_ECH_Write");
  9947. if (ech->state == ECH_WRITE_NONE || ech->state == ECH_PARSED_INTERNAL)
  9948. return 0;
  9949. if (ech->state == ECH_WRITE_RETRY_CONFIGS) {
  9950. /* get size then write */
  9951. ret = GetEchConfigsEx(ech->echConfig, NULL, &configsLen);
  9952. if (ret != LENGTH_ONLY_E)
  9953. return ret;
  9954. ret = GetEchConfigsEx(ech->echConfig, writeBuf, &configsLen);
  9955. if (ret != WOLFSSL_SUCCESS)
  9956. return ret;
  9957. *offset += configsLen;
  9958. return 0;
  9959. }
  9960. #ifdef WOLFSSL_SMALL_STACK
  9961. hpke = (Hpke*)XMALLOC(sizeof(Hpke), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9962. if (hpke == NULL)
  9963. return MEMORY_E;
  9964. rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
  9965. if (rng == NULL) {
  9966. XFREE(hpke, NULL, DYNAMIC_TYPE_RNG);
  9967. return MEMORY_E;
  9968. }
  9969. #endif
  9970. /* type */
  9971. *writeBuf_p = ech->type;
  9972. writeBuf_p += sizeof(ech->type);
  9973. /* outer has body, inner does not */
  9974. if (ech->type == ECH_TYPE_OUTER) {
  9975. /* kdfId */
  9976. c16toa(ech->cipherSuite.kdfId, writeBuf_p);
  9977. writeBuf_p += sizeof(ech->cipherSuite.kdfId);
  9978. /* aeadId */
  9979. c16toa(ech->cipherSuite.aeadId, writeBuf_p);
  9980. writeBuf_p += sizeof(ech->cipherSuite.aeadId);
  9981. /* configId */
  9982. *writeBuf_p = ech->configId;
  9983. writeBuf_p += sizeof(ech->configId);
  9984. /* encLen */
  9985. c16toa(ech->encLen, writeBuf_p);
  9986. writeBuf_p += 2;
  9987. if (ech->state == ECH_WRITE_GREASE) {
  9988. /* hpke init */
  9989. ret = wc_HpkeInit(hpke, ech->kemId, ech->cipherSuite.kdfId,
  9990. ech->cipherSuite.aeadId, NULL);
  9991. if (ret == 0)
  9992. rngRet = ret = wc_InitRng(rng);
  9993. /* create the ephemeralKey */
  9994. if (ret == 0)
  9995. ret = wc_HpkeGenerateKeyPair(hpke, &ephemeralKey, rng);
  9996. /* enc */
  9997. if (ret == 0) {
  9998. ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey, writeBuf_p,
  9999. &ech->encLen);
  10000. writeBuf_p += ech->encLen;
  10001. }
  10002. if (ret == 0) {
  10003. /* innerClientHelloLen */
  10004. c16toa(GREASE_ECH_SIZE + ((writeBuf_p + 2 - writeBuf) % 32),
  10005. writeBuf_p);
  10006. writeBuf_p += 2;
  10007. /* innerClientHello */
  10008. ret = wc_RNG_GenerateBlock(rng, writeBuf_p, GREASE_ECH_SIZE +
  10009. ((writeBuf_p - writeBuf) % 32));
  10010. writeBuf_p += GREASE_ECH_SIZE + ((writeBuf_p - writeBuf) % 32);
  10011. }
  10012. if (rngRet == 0)
  10013. wc_FreeRng(rng);
  10014. if (ephemeralKey != NULL)
  10015. wc_HpkeFreeKey(hpke, hpke->kem, ephemeralKey, hpke->heap);
  10016. }
  10017. else {
  10018. /* write enc to writeBuf_p */
  10019. ret = wc_HpkeSerializePublicKey(ech->hpke, ech->ephemeralKey,
  10020. writeBuf_p, &ech->encLen);
  10021. writeBuf_p += ech->encLen;
  10022. /* innerClientHelloLen */
  10023. c16toa(ech->innerClientHelloLen, writeBuf_p);
  10024. writeBuf_p += 2;
  10025. /* set payload offset for when we finalize */
  10026. ech->outerClientPayload = writeBuf_p;
  10027. /* write zeros for payload */
  10028. XMEMSET(writeBuf_p, 0, ech->innerClientHelloLen);
  10029. writeBuf_p += ech->innerClientHelloLen;
  10030. }
  10031. }
  10032. #ifdef WOLFSSL_SMALL_STACK
  10033. XFREE(hpke, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10034. XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
  10035. #endif
  10036. if (ret == 0)
  10037. *offset += (writeBuf_p - writeBuf);
  10038. return ret;
  10039. }
  10040. /* return the size needed for the ech extension */
  10041. static int TLSX_ECH_GetSize(WOLFSSL_ECH* ech)
  10042. {
  10043. int ret;
  10044. word32 size;
  10045. if (ech->state == ECH_WRITE_GREASE) {
  10046. size = sizeof(ech->type) + sizeof(ech->cipherSuite) +
  10047. sizeof(ech->configId) + sizeof(word16) + ech->encLen +
  10048. sizeof(word16);
  10049. size += GREASE_ECH_SIZE + (size % 32);
  10050. }
  10051. else if (ech->state == ECH_WRITE_NONE ||
  10052. ech->state == ECH_PARSED_INTERNAL) {
  10053. size = 0;
  10054. }
  10055. else if (ech->state == ECH_WRITE_RETRY_CONFIGS) {
  10056. /* get the size of the raw configs */
  10057. ret = GetEchConfigsEx(ech->echConfig, NULL, &size);
  10058. if (ret != LENGTH_ONLY_E)
  10059. return ret;
  10060. }
  10061. else if (ech->type == ECH_TYPE_INNER)
  10062. {
  10063. size = sizeof(ech->type);
  10064. }
  10065. else
  10066. {
  10067. size = sizeof(ech->type) + sizeof(ech->cipherSuite) +
  10068. sizeof(ech->configId) + sizeof(word16) + ech->encLen +
  10069. sizeof(word16) + ech->innerClientHelloLen;
  10070. }
  10071. return (int)size;
  10072. }
  10073. /* return status after attempting to open the hpke encrypted ech extension, if
  10074. * successful the inner client hello will be stored in
  10075. * ech->innerClientHelloLen */
  10076. static int TLSX_ExtractEch(WOLFSSL_ECH* ech, WOLFSSL_EchConfig* echConfig,
  10077. byte* aad, word32 aadLen, void* heap)
  10078. {
  10079. int ret = 0;
  10080. int expectedEncLen;
  10081. int i;
  10082. word32 rawConfigLen = 0;
  10083. byte* info = NULL;
  10084. word32 infoLen = 0;
  10085. if (ech == NULL || echConfig == NULL || aad == NULL)
  10086. return BAD_FUNC_ARG;
  10087. /* verify the kem and key len */
  10088. switch (echConfig->kemId)
  10089. {
  10090. case DHKEM_P256_HKDF_SHA256:
  10091. expectedEncLen = DHKEM_P256_ENC_LEN;
  10092. break;
  10093. case DHKEM_P384_HKDF_SHA384:
  10094. expectedEncLen = DHKEM_P384_ENC_LEN;
  10095. break;
  10096. case DHKEM_P521_HKDF_SHA512:
  10097. expectedEncLen = DHKEM_P521_ENC_LEN;
  10098. break;
  10099. case DHKEM_X25519_HKDF_SHA256:
  10100. expectedEncLen = DHKEM_X25519_ENC_LEN;
  10101. break;
  10102. case DHKEM_X448_HKDF_SHA512:
  10103. expectedEncLen = DHKEM_X448_ENC_LEN;
  10104. break;
  10105. default:
  10106. expectedEncLen = 0;
  10107. break;
  10108. }
  10109. if (expectedEncLen != ech->encLen)
  10110. return BAD_FUNC_ARG;
  10111. /* verify the cipher suite */
  10112. for (i = 0; i < echConfig->numCipherSuites; i++) {
  10113. if (echConfig->cipherSuites[i].kdfId == ech->cipherSuite.kdfId &&
  10114. echConfig->cipherSuites[i].aeadId == ech->cipherSuite.aeadId) {
  10115. break;
  10116. }
  10117. }
  10118. if (i >= echConfig->numCipherSuites) {
  10119. return BAD_FUNC_ARG;
  10120. }
  10121. ech->hpke = (Hpke*)XMALLOC(sizeof(Hpke), heap, DYNAMIC_TYPE_TMP_BUFFER);
  10122. if (ech->hpke == NULL)
  10123. return MEMORY_E;
  10124. ret = wc_HpkeInit(ech->hpke, echConfig->kemId, ech->cipherSuite.kdfId,
  10125. ech->cipherSuite.aeadId, heap);
  10126. /* get the rawConfigLen */
  10127. if (ret == 0)
  10128. ret = GetEchConfig(echConfig, NULL, &rawConfigLen);
  10129. if (ret == LENGTH_ONLY_E)
  10130. ret = 0;
  10131. /* create info */
  10132. if (ret == 0) {
  10133. infoLen = TLS_INFO_CONST_STRING_SZ + 1 + rawConfigLen;
  10134. info = (byte*)XMALLOC(infoLen, heap, DYNAMIC_TYPE_TMP_BUFFER);
  10135. if (info == NULL)
  10136. ret = MEMORY_E;
  10137. else {
  10138. XMEMCPY(info, (byte*)TLS_INFO_CONST_STRING,
  10139. TLS_INFO_CONST_STRING_SZ + 1);
  10140. ret = GetEchConfig(echConfig, info +
  10141. TLS_INFO_CONST_STRING_SZ + 1, &rawConfigLen);
  10142. }
  10143. }
  10144. /* decrypt the ech payload */
  10145. if (ret == 0)
  10146. ret = wc_HpkeOpenBase(ech->hpke, echConfig->receiverPrivkey, ech->enc,
  10147. ech->encLen, info, infoLen, aad, aadLen, ech->outerClientPayload,
  10148. ech->innerClientHelloLen,
  10149. ech->innerClientHello + HANDSHAKE_HEADER_SZ);
  10150. if (ret != 0) {
  10151. XFREE(ech->hpke, heap, DYNAMIC_TYPE_TMP_BUFFER);
  10152. ech->hpke = NULL;
  10153. }
  10154. if (info != NULL)
  10155. XFREE(info, heap, DYNAMIC_TYPE_TMP_BUFFER);
  10156. return ret;
  10157. }
  10158. /* parse the ech extension, if internal update ech->state and return, if
  10159. * external attempt to extract the inner client_hello, return the status */
  10160. static int TLSX_ECH_Parse(WOLFSSL* ssl, const byte* readBuf, word16 size,
  10161. byte msgType)
  10162. {
  10163. int ret = 0;
  10164. int i;
  10165. TLSX* echX;
  10166. WOLFSSL_ECH* ech;
  10167. WOLFSSL_EchConfig* echConfig;
  10168. byte* aadCopy;
  10169. byte* readBuf_p = (byte*)readBuf;
  10170. WOLFSSL_MSG("TLSX_ECH_Parse");
  10171. if (size == 0)
  10172. return BAD_FUNC_ARG;
  10173. if (msgType == encrypted_extensions) {
  10174. ret = wolfSSL_SetEchConfigs(ssl, readBuf, size);
  10175. if (ret == WOLFSSL_SUCCESS)
  10176. ret = 0;
  10177. }
  10178. else if (msgType == client_hello && ssl->ctx->echConfigs != NULL) {
  10179. echX = TLSX_Find(ssl->extensions, TLSX_ECH);
  10180. if (echX == NULL)
  10181. return BAD_FUNC_ARG;
  10182. ech = (WOLFSSL_ECH*)echX->data;
  10183. /* read the ech parameters before the payload */
  10184. ech->type = *readBuf_p;
  10185. readBuf_p++;
  10186. if (ech->type == ECH_TYPE_INNER) {
  10187. ech->state = ECH_PARSED_INTERNAL;
  10188. return 0;
  10189. }
  10190. /* technically the payload would only be 1 byte at this length */
  10191. if (size < 11 + ech->encLen)
  10192. return BAD_FUNC_ARG;
  10193. ato16(readBuf_p, &ech->cipherSuite.kdfId);
  10194. readBuf_p += 2;
  10195. ato16(readBuf_p, &ech->cipherSuite.aeadId);
  10196. readBuf_p += 2;
  10197. ech->configId = *readBuf_p;
  10198. readBuf_p++;
  10199. ato16(readBuf_p, &ech->encLen);
  10200. readBuf_p += 2;
  10201. if (ech->encLen > HPKE_Npk_MAX)
  10202. return BAD_FUNC_ARG;
  10203. XMEMCPY(ech->enc, readBuf_p, ech->encLen);
  10204. readBuf_p += ech->encLen;
  10205. ato16(readBuf_p, &ech->innerClientHelloLen);
  10206. ech->innerClientHelloLen -= AES_BLOCK_SIZE;
  10207. readBuf_p += 2;
  10208. ech->outerClientPayload = readBuf_p;
  10209. /* make a copy of the aad */
  10210. aadCopy = (byte*)XMALLOC(ech->aadLen, ssl->heap,
  10211. DYNAMIC_TYPE_TMP_BUFFER);
  10212. if (aadCopy == NULL)
  10213. return MEMORY_E;
  10214. XMEMCPY(aadCopy, ech->aad, ech->aadLen);
  10215. /* set the ech payload of the copy to zeros */
  10216. XMEMSET(aadCopy + (readBuf_p - ech->aad), 0,
  10217. ech->innerClientHelloLen + AES_BLOCK_SIZE);
  10218. /* allocate the inner payload buffer */
  10219. ech->innerClientHello =
  10220. (byte*)XMALLOC(ech->innerClientHelloLen + HANDSHAKE_HEADER_SZ,
  10221. ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  10222. if (ech->innerClientHello == NULL) {
  10223. XFREE(aadCopy, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  10224. return MEMORY_E;
  10225. }
  10226. /* first check if the config id matches */
  10227. echConfig = ssl->ctx->echConfigs;
  10228. while (echConfig != NULL) {
  10229. /* decrypt with this config */
  10230. if (echConfig->configId == ech->configId) {
  10231. ret = TLSX_ExtractEch(ech, echConfig, aadCopy, ech->aadLen,
  10232. ssl->heap);
  10233. break;
  10234. }
  10235. echConfig = echConfig->next;
  10236. }
  10237. /* try to decrypt with all configs */
  10238. if (echConfig == NULL || ret != 0) {
  10239. echConfig = ssl->ctx->echConfigs;
  10240. while (echConfig != NULL) {
  10241. ret = TLSX_ExtractEch(ech, echConfig, aadCopy, ech->aadLen,
  10242. ssl->heap);
  10243. if (ret== 0)
  10244. break;
  10245. echConfig = echConfig->next;
  10246. }
  10247. }
  10248. /* if we failed to extract */
  10249. if (ret != 0) {
  10250. XFREE(ech->innerClientHello, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  10251. ech->innerClientHello = NULL;
  10252. ech->state = ECH_WRITE_RETRY_CONFIGS;
  10253. }
  10254. else {
  10255. i = 0;
  10256. /* decrement until before the padding */
  10257. while (ech->innerClientHello[ech->innerClientHelloLen +
  10258. HANDSHAKE_HEADER_SZ - i - 1] != ECH_TYPE_INNER) {
  10259. i++;
  10260. }
  10261. /* subtract the length of the padding from the length */
  10262. ech->innerClientHelloLen -= i;
  10263. }
  10264. XFREE(aadCopy, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  10265. return 0;
  10266. }
  10267. return ret;
  10268. }
  10269. /* free the ech struct and the dynamic buffer it uses */
  10270. static void TLSX_ECH_Free(WOLFSSL_ECH* ech, void* heap)
  10271. {
  10272. if (ech->innerClientHello != NULL)
  10273. XFREE(ech->innerClientHello, heap, DYNAMIC_TYPE_TMP_BUFFER);
  10274. if (ech->ephemeralKey != NULL)
  10275. wc_HpkeFreeKey(ech->hpke, ech->hpke->kem, ech->ephemeralKey,
  10276. ech->hpke->heap);
  10277. if (ech->hpke != NULL)
  10278. XFREE(ech->hpke, heap, DYNAMIC_TYPE_TMP_BUFFER);
  10279. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  10280. (void)heap;
  10281. }
  10282. /* encrypt the client hello and store it in ech->outerClientPayload, return
  10283. * status */
  10284. int TLSX_FinalizeEch(WOLFSSL_ECH* ech, byte* aad, word32 aadLen)
  10285. {
  10286. int ret;
  10287. void* receiverPubkey = NULL;
  10288. byte* info;
  10289. int infoLen;
  10290. byte* aadCopy;
  10291. /* import the server public key */
  10292. ret = wc_HpkeDeserializePublicKey(ech->hpke, &receiverPubkey,
  10293. ech->echConfig->receiverPubkey, ech->encLen);
  10294. if (ret == 0) {
  10295. /* create info */
  10296. infoLen = TLS_INFO_CONST_STRING_SZ + 1 + ech->echConfig->rawLen;
  10297. info = (byte*)XMALLOC(infoLen, ech->hpke->heap,
  10298. DYNAMIC_TYPE_TMP_BUFFER);
  10299. if (info == NULL)
  10300. ret = MEMORY_E;
  10301. if (ret == 0) {
  10302. /* puts the null byte in for me */
  10303. XMEMCPY(info, (byte*)TLS_INFO_CONST_STRING, TLS_INFO_CONST_STRING_SZ
  10304. + 1);
  10305. XMEMCPY(info + TLS_INFO_CONST_STRING_SZ + 1, ech->echConfig->raw,
  10306. ech->echConfig->rawLen);
  10307. /* make a copy of the aad since we overwrite it */
  10308. aadCopy = (byte*)XMALLOC(aadLen, ech->hpke->heap,
  10309. DYNAMIC_TYPE_TMP_BUFFER);
  10310. if (aadCopy == NULL) {
  10311. XFREE(info, ech->hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  10312. ret = MEMORY_E;
  10313. }
  10314. }
  10315. if (ret == 0) {
  10316. XMEMCPY(aadCopy, aad, aadLen);
  10317. /* seal the payload */
  10318. ret = wc_HpkeSealBase(ech->hpke, ech->ephemeralKey, receiverPubkey,
  10319. info, infoLen, aadCopy, aadLen, ech->innerClientHello,
  10320. ech->innerClientHelloLen - ech->hpke->Nt,
  10321. ech->outerClientPayload);
  10322. XFREE(info, ech->hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  10323. XFREE(aadCopy, ech->hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  10324. }
  10325. }
  10326. if (receiverPubkey != NULL)
  10327. wc_HpkeFreeKey(ech->hpke, ech->hpke->kem, receiverPubkey,
  10328. ech->hpke->heap);
  10329. return ret;
  10330. }
  10331. #define GREASE_ECH_USE TLSX_GreaseECH_Use
  10332. #define ECH_USE TLSX_ECH_Use
  10333. #define SERVER_ECH_USE TLSX_ServerECH_Use
  10334. #define ECH_WRITE TLSX_ECH_Write
  10335. #define ECH_GET_SIZE TLSX_ECH_GetSize
  10336. #define ECH_PARSE TLSX_ECH_Parse
  10337. #define ECH_FREE TLSX_ECH_Free
  10338. #endif
  10339. /** Releases all extensions in the provided list. */
  10340. void TLSX_FreeAll(TLSX* list, void* heap)
  10341. {
  10342. TLSX* extension;
  10343. while ((extension = list)) {
  10344. list = extension->next;
  10345. switch (extension->type) {
  10346. #if defined(HAVE_RPK)
  10347. case TLSX_CLIENT_CERTIFICATE_TYPE:
  10348. WOLFSSL_MSG("Client Certificate Type extension free");
  10349. /* nothing to do */
  10350. break;
  10351. case TLSX_SERVER_CERTIFICATE_TYPE:
  10352. WOLFSSL_MSG("Server Certificate Type extension free");
  10353. /* nothing to do */
  10354. break;
  10355. #endif
  10356. #ifdef HAVE_SNI
  10357. case TLSX_SERVER_NAME:
  10358. WOLFSSL_MSG("SNI extension free");
  10359. SNI_FREE_ALL((SNI*)extension->data, heap);
  10360. break;
  10361. #endif
  10362. case TLSX_TRUSTED_CA_KEYS:
  10363. WOLFSSL_MSG("Trusted CA Indication extension free");
  10364. TCA_FREE_ALL((TCA*)extension->data, heap);
  10365. break;
  10366. case TLSX_MAX_FRAGMENT_LENGTH:
  10367. WOLFSSL_MSG("Max Fragment Length extension free");
  10368. MFL_FREE_ALL(extension->data, heap);
  10369. break;
  10370. case TLSX_EXTENDED_MASTER_SECRET:
  10371. WOLFSSL_MSG("Extended Master Secret free");
  10372. /* Nothing to do. */
  10373. break;
  10374. case TLSX_TRUNCATED_HMAC:
  10375. WOLFSSL_MSG("Truncated HMAC extension free");
  10376. /* Nothing to do. */
  10377. break;
  10378. case TLSX_SUPPORTED_GROUPS:
  10379. WOLFSSL_MSG("Supported Groups extension free");
  10380. EC_FREE_ALL((SupportedCurve*)extension->data, heap);
  10381. break;
  10382. case TLSX_EC_POINT_FORMATS:
  10383. WOLFSSL_MSG("Point Formats extension free");
  10384. PF_FREE_ALL((PointFormat*)extension->data, heap);
  10385. break;
  10386. case TLSX_STATUS_REQUEST:
  10387. WOLFSSL_MSG("Certificate Status Request extension free");
  10388. CSR_FREE_ALL((CertificateStatusRequest*)extension->data, heap);
  10389. break;
  10390. case TLSX_STATUS_REQUEST_V2:
  10391. WOLFSSL_MSG("Certificate Status Request v2 extension free");
  10392. CSR2_FREE_ALL((CertificateStatusRequestItemV2*)extension->data,
  10393. heap);
  10394. break;
  10395. case TLSX_RENEGOTIATION_INFO:
  10396. WOLFSSL_MSG("Secure Renegotiation extension free");
  10397. SCR_FREE_ALL(extension->data, heap);
  10398. break;
  10399. case TLSX_SESSION_TICKET:
  10400. WOLFSSL_MSG("Session Ticket extension free");
  10401. WOLF_STK_FREE(extension->data, heap);
  10402. break;
  10403. case TLSX_APPLICATION_LAYER_PROTOCOL:
  10404. WOLFSSL_MSG("ALPN extension free");
  10405. ALPN_FREE_ALL((ALPN*)extension->data, heap);
  10406. break;
  10407. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10408. case TLSX_SIGNATURE_ALGORITHMS:
  10409. WOLFSSL_MSG("Signature Algorithms extension to free");
  10410. SA_FREE_ALL((SignatureAlgorithms*)extension->data, heap);
  10411. break;
  10412. #endif
  10413. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  10414. case TLSX_ENCRYPT_THEN_MAC:
  10415. WOLFSSL_MSG("Encrypt-Then-Mac extension free");
  10416. break;
  10417. #endif
  10418. #ifdef WOLFSSL_TLS13
  10419. case TLSX_SUPPORTED_VERSIONS:
  10420. WOLFSSL_MSG("Supported Versions extension free");
  10421. break;
  10422. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10423. case TLSX_COOKIE:
  10424. WOLFSSL_MSG("Cookie extension free");
  10425. CKE_FREE_ALL((Cookie*)extension->data, heap);
  10426. break;
  10427. #endif
  10428. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10429. case TLSX_PRE_SHARED_KEY:
  10430. WOLFSSL_MSG("Pre-Shared Key extension free");
  10431. PSK_FREE_ALL((PreSharedKey*)extension->data, heap);
  10432. break;
  10433. case TLSX_PSK_KEY_EXCHANGE_MODES:
  10434. WOLFSSL_MSG("PSK Key Exchange Modes extension free");
  10435. break;
  10436. #endif
  10437. #ifdef WOLFSSL_EARLY_DATA
  10438. case TLSX_EARLY_DATA:
  10439. WOLFSSL_MSG("Early Data extension free");
  10440. break;
  10441. #endif
  10442. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  10443. case TLSX_POST_HANDSHAKE_AUTH:
  10444. WOLFSSL_MSG("Post-Handshake Authentication extension free");
  10445. break;
  10446. #endif
  10447. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10448. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  10449. WOLFSSL_MSG("Signature Algorithms extension free");
  10450. break;
  10451. #endif
  10452. case TLSX_KEY_SHARE:
  10453. WOLFSSL_MSG("Key Share extension free");
  10454. KS_FREE_ALL((KeyShareEntry*)extension->data, heap);
  10455. break;
  10456. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES)
  10457. case TLSX_CERTIFICATE_AUTHORITIES:
  10458. WOLFSSL_MSG("Certificate Authorities extension free");
  10459. break;
  10460. #endif
  10461. #endif
  10462. #ifdef WOLFSSL_SRTP
  10463. case TLSX_USE_SRTP:
  10464. WOLFSSL_MSG("SRTP extension free");
  10465. SRTP_FREE((TlsxSrtp*)extension->data, heap);
  10466. break;
  10467. #endif
  10468. #ifdef WOLFSSL_QUIC
  10469. case TLSX_KEY_QUIC_TP_PARAMS:
  10470. FALL_THROUGH;
  10471. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  10472. WOLFSSL_MSG("QUIC transport parameter free");
  10473. QTP_FREE((QuicTransportParam*)extension->data, heap);
  10474. break;
  10475. #endif
  10476. #ifdef WOLFSSL_DTLS_CID
  10477. case TLSX_CONNECTION_ID:
  10478. WOLFSSL_MSG("Connection ID extension free");
  10479. CID_FREE((byte*)extension->data, heap);
  10480. break;
  10481. #endif /* WOLFSSL_DTLS_CID */
  10482. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  10483. case TLSX_ECH:
  10484. WOLFSSL_MSG("ECH extension free");
  10485. ECH_FREE((WOLFSSL_ECH*)extension->data, heap);
  10486. break;
  10487. #endif
  10488. #ifdef WOLFSSL_DUAL_ALG_CERTS
  10489. case TLSX_CKS:
  10490. WOLFSSL_MSG("CKS extension free");
  10491. /* nothing to do */
  10492. break;
  10493. #endif
  10494. default:
  10495. break;
  10496. }
  10497. XFREE(extension, heap, DYNAMIC_TYPE_TLSX);
  10498. }
  10499. (void)heap;
  10500. }
  10501. /** Checks if the tls extensions are supported based on the protocol version. */
  10502. int TLSX_SupportExtensions(WOLFSSL* ssl) {
  10503. return ssl && (IsTLS(ssl) || ssl->version.major == DTLS_MAJOR);
  10504. }
  10505. /** Tells the buffered size of the extensions in a list. */
  10506. static int TLSX_GetSize(TLSX* list, byte* semaphore, byte msgType,
  10507. word16* pLength)
  10508. {
  10509. int ret = 0;
  10510. TLSX* extension;
  10511. word16 length = 0;
  10512. byte isRequest = (msgType == client_hello ||
  10513. msgType == certificate_request);
  10514. while ((extension = list)) {
  10515. list = extension->next;
  10516. /* only extensions marked as response are sent back to the client. */
  10517. if (!isRequest && !extension->resp)
  10518. continue; /* skip! */
  10519. /* ssl level extensions are expected to override ctx level ones. */
  10520. if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
  10521. continue; /* skip! */
  10522. /* extension type + extension data length. */
  10523. length += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
  10524. switch (extension->type) {
  10525. #ifdef WOLFSSL_DUAL_ALG_CERTS
  10526. case TLSX_CKS:
  10527. length += ((WOLFSSL*)extension->data)->sigSpecSz ;
  10528. break;
  10529. #endif
  10530. #ifdef HAVE_SNI
  10531. case TLSX_SERVER_NAME:
  10532. /* SNI only sends the name on the request. */
  10533. if (isRequest)
  10534. length += SNI_GET_SIZE((SNI*)extension->data);
  10535. break;
  10536. #endif
  10537. case TLSX_TRUSTED_CA_KEYS:
  10538. /* TCA only sends the list on the request. */
  10539. if (isRequest)
  10540. length += TCA_GET_SIZE((TCA*)extension->data);
  10541. break;
  10542. case TLSX_MAX_FRAGMENT_LENGTH:
  10543. length += MFL_GET_SIZE(extension->data);
  10544. break;
  10545. case TLSX_EXTENDED_MASTER_SECRET:
  10546. case TLSX_TRUNCATED_HMAC:
  10547. /* always empty. */
  10548. break;
  10549. case TLSX_SUPPORTED_GROUPS:
  10550. length += EC_GET_SIZE((SupportedCurve*)extension->data);
  10551. break;
  10552. case TLSX_EC_POINT_FORMATS:
  10553. length += PF_GET_SIZE((PointFormat*)extension->data);
  10554. break;
  10555. case TLSX_STATUS_REQUEST:
  10556. length += CSR_GET_SIZE(
  10557. (CertificateStatusRequest*)extension->data, isRequest);
  10558. break;
  10559. case TLSX_STATUS_REQUEST_V2:
  10560. length += CSR2_GET_SIZE(
  10561. (CertificateStatusRequestItemV2*)extension->data,
  10562. isRequest);
  10563. break;
  10564. case TLSX_RENEGOTIATION_INFO:
  10565. length += SCR_GET_SIZE((SecureRenegotiation*)extension->data,
  10566. isRequest);
  10567. break;
  10568. case TLSX_SESSION_TICKET:
  10569. length += WOLF_STK_GET_SIZE((SessionTicket*)extension->data,
  10570. isRequest);
  10571. break;
  10572. case TLSX_APPLICATION_LAYER_PROTOCOL:
  10573. length += ALPN_GET_SIZE((ALPN*)extension->data);
  10574. break;
  10575. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10576. case TLSX_SIGNATURE_ALGORITHMS:
  10577. length += SA_GET_SIZE(extension->data);
  10578. break;
  10579. #endif
  10580. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  10581. case TLSX_ENCRYPT_THEN_MAC:
  10582. ret = ETM_GET_SIZE(msgType, &length);
  10583. break;
  10584. #endif /* HAVE_ENCRYPT_THEN_MAC */
  10585. #ifdef WOLFSSL_TLS13
  10586. case TLSX_SUPPORTED_VERSIONS:
  10587. ret = SV_GET_SIZE(extension->data, msgType, &length);
  10588. break;
  10589. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10590. case TLSX_COOKIE:
  10591. ret = CKE_GET_SIZE((Cookie*)extension->data, msgType, &length);
  10592. break;
  10593. #endif
  10594. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10595. case TLSX_PRE_SHARED_KEY:
  10596. ret = PSK_GET_SIZE((PreSharedKey*)extension->data, msgType,
  10597. &length);
  10598. break;
  10599. case TLSX_PSK_KEY_EXCHANGE_MODES:
  10600. ret = PKM_GET_SIZE((byte)extension->val, msgType, &length);
  10601. break;
  10602. #endif
  10603. #ifdef WOLFSSL_EARLY_DATA
  10604. case TLSX_EARLY_DATA:
  10605. ret = EDI_GET_SIZE(msgType, &length);
  10606. break;
  10607. #endif
  10608. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  10609. case TLSX_POST_HANDSHAKE_AUTH:
  10610. ret = PHA_GET_SIZE(msgType, &length);
  10611. break;
  10612. #endif
  10613. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10614. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  10615. length += SAC_GET_SIZE(extension->data);
  10616. break;
  10617. #endif
  10618. case TLSX_KEY_SHARE:
  10619. length += KS_GET_SIZE((KeyShareEntry*)extension->data, msgType);
  10620. break;
  10621. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES)
  10622. case TLSX_CERTIFICATE_AUTHORITIES:
  10623. length += CAN_GET_SIZE(extension->data);
  10624. break;
  10625. #endif
  10626. #endif
  10627. #ifdef WOLFSSL_SRTP
  10628. case TLSX_USE_SRTP:
  10629. length += SRTP_GET_SIZE((TlsxSrtp*)extension->data);
  10630. break;
  10631. #endif
  10632. #ifdef HAVE_RPK
  10633. case TLSX_CLIENT_CERTIFICATE_TYPE:
  10634. length += CCT_GET_SIZE((WOLFSSL*)extension->data, msgType);
  10635. break;
  10636. case TLSX_SERVER_CERTIFICATE_TYPE:
  10637. length += SCT_GET_SIZE((WOLFSSL*)extension->data, msgType);
  10638. break;
  10639. #endif /* HAVE_RPK */
  10640. #ifdef WOLFSSL_QUIC
  10641. case TLSX_KEY_QUIC_TP_PARAMS:
  10642. FALL_THROUGH; /* followed by */
  10643. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  10644. length += QTP_GET_SIZE(extension);
  10645. break;
  10646. #endif
  10647. #ifdef WOLFSSL_DTLS_CID
  10648. case TLSX_CONNECTION_ID:
  10649. length += CID_GET_SIZE((byte*)extension->data);
  10650. break;
  10651. #endif /* WOLFSSL_DTLS_CID */
  10652. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  10653. case TLSX_ECH:
  10654. length += ECH_GET_SIZE((WOLFSSL_ECH*)extension->data);
  10655. break;
  10656. #endif
  10657. default:
  10658. break;
  10659. }
  10660. /* marks the extension as processed so ctx level */
  10661. /* extensions don't overlap with ssl level ones. */
  10662. TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
  10663. }
  10664. *pLength += length;
  10665. return ret;
  10666. }
  10667. /** Writes the extensions of a list in a buffer. */
  10668. static int TLSX_Write(TLSX* list, byte* output, byte* semaphore,
  10669. byte msgType, word16* pOffset)
  10670. {
  10671. int ret = 0;
  10672. TLSX* extension;
  10673. word16 offset = 0;
  10674. word16 length_offset = 0;
  10675. byte isRequest = (msgType == client_hello ||
  10676. msgType == certificate_request);
  10677. while ((extension = list)) {
  10678. list = extension->next;
  10679. /* only extensions marked as response are written in a response. */
  10680. if (!isRequest && !extension->resp)
  10681. continue; /* skip! */
  10682. /* ssl level extensions are expected to override ctx level ones. */
  10683. if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
  10684. continue; /* skip! */
  10685. /* writes extension type. */
  10686. c16toa(extension->type, output + offset);
  10687. offset += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
  10688. length_offset = offset;
  10689. /* extension data should be written internally. */
  10690. switch (extension->type) {
  10691. #ifdef WOLFSSL_DUAL_ALG_CERTS
  10692. case TLSX_CKS:
  10693. WOLFSSL_MSG("CKS extension to write");
  10694. offset += CKS_WRITE(((WOLFSSL*)extension->data),
  10695. output + offset);
  10696. break;
  10697. #endif
  10698. #ifdef HAVE_SNI
  10699. case TLSX_SERVER_NAME:
  10700. if (isRequest) {
  10701. WOLFSSL_MSG("SNI extension to write");
  10702. offset += SNI_WRITE((SNI*)extension->data, output + offset);
  10703. }
  10704. break;
  10705. #endif
  10706. case TLSX_TRUSTED_CA_KEYS:
  10707. WOLFSSL_MSG("Trusted CA Indication extension to write");
  10708. if (isRequest) {
  10709. offset += TCA_WRITE((TCA*)extension->data, output + offset);
  10710. }
  10711. break;
  10712. case TLSX_MAX_FRAGMENT_LENGTH:
  10713. WOLFSSL_MSG("Max Fragment Length extension to write");
  10714. offset += MFL_WRITE((byte*)extension->data, output + offset);
  10715. break;
  10716. case TLSX_EXTENDED_MASTER_SECRET:
  10717. WOLFSSL_MSG("Extended Master Secret");
  10718. /* always empty. */
  10719. break;
  10720. case TLSX_TRUNCATED_HMAC:
  10721. WOLFSSL_MSG("Truncated HMAC extension to write");
  10722. /* always empty. */
  10723. break;
  10724. case TLSX_SUPPORTED_GROUPS:
  10725. WOLFSSL_MSG("Supported Groups extension to write");
  10726. offset += EC_WRITE((SupportedCurve*)extension->data,
  10727. output + offset);
  10728. break;
  10729. case TLSX_EC_POINT_FORMATS:
  10730. WOLFSSL_MSG("Point Formats extension to write");
  10731. offset += PF_WRITE((PointFormat*)extension->data,
  10732. output + offset);
  10733. break;
  10734. case TLSX_STATUS_REQUEST:
  10735. WOLFSSL_MSG("Certificate Status Request extension to write");
  10736. ret = CSR_WRITE((CertificateStatusRequest*)extension->data,
  10737. output + offset, isRequest);
  10738. if (ret > 0) {
  10739. offset += (word16)ret;
  10740. ret = 0;
  10741. }
  10742. break;
  10743. case TLSX_STATUS_REQUEST_V2:
  10744. WOLFSSL_MSG("Certificate Status Request v2 extension to write");
  10745. ret = CSR2_WRITE(
  10746. (CertificateStatusRequestItemV2*)extension->data,
  10747. output + offset, isRequest);
  10748. if (ret > 0) {
  10749. offset += (word16)ret;
  10750. ret = 0;
  10751. }
  10752. break;
  10753. case TLSX_RENEGOTIATION_INFO:
  10754. WOLFSSL_MSG("Secure Renegotiation extension to write");
  10755. offset += SCR_WRITE((SecureRenegotiation*)extension->data,
  10756. output + offset, isRequest);
  10757. break;
  10758. case TLSX_SESSION_TICKET:
  10759. WOLFSSL_MSG("Session Ticket extension to write");
  10760. offset += WOLF_STK_WRITE((SessionTicket*)extension->data,
  10761. output + offset, isRequest);
  10762. break;
  10763. case TLSX_APPLICATION_LAYER_PROTOCOL:
  10764. WOLFSSL_MSG("ALPN extension to write");
  10765. offset += ALPN_WRITE((ALPN*)extension->data, output + offset);
  10766. break;
  10767. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10768. case TLSX_SIGNATURE_ALGORITHMS:
  10769. WOLFSSL_MSG("Signature Algorithms extension to write");
  10770. offset += SA_WRITE(extension->data, output + offset);
  10771. break;
  10772. #endif
  10773. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  10774. case TLSX_ENCRYPT_THEN_MAC:
  10775. WOLFSSL_MSG("Encrypt-Then-Mac extension to write");
  10776. ret = ETM_WRITE(extension->data, output, msgType, &offset);
  10777. break;
  10778. #endif /* HAVE_ENCRYPT_THEN_MAC */
  10779. #ifdef WOLFSSL_TLS13
  10780. case TLSX_SUPPORTED_VERSIONS:
  10781. WOLFSSL_MSG("Supported Versions extension to write");
  10782. ret = SV_WRITE(extension->data, output + offset, msgType, &offset);
  10783. break;
  10784. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10785. case TLSX_COOKIE:
  10786. WOLFSSL_MSG("Cookie extension to write");
  10787. ret = CKE_WRITE((Cookie*)extension->data, output + offset,
  10788. msgType, &offset);
  10789. break;
  10790. #endif
  10791. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10792. case TLSX_PRE_SHARED_KEY:
  10793. WOLFSSL_MSG("Pre-Shared Key extension to write");
  10794. ret = PSK_WRITE((PreSharedKey*)extension->data, output + offset,
  10795. msgType, &offset);
  10796. break;
  10797. case TLSX_PSK_KEY_EXCHANGE_MODES:
  10798. WOLFSSL_MSG("PSK Key Exchange Modes extension to write");
  10799. ret = PKM_WRITE((byte)extension->val, output + offset, msgType,
  10800. &offset);
  10801. break;
  10802. #endif
  10803. #ifdef WOLFSSL_EARLY_DATA
  10804. case TLSX_EARLY_DATA:
  10805. WOLFSSL_MSG("Early Data extension to write");
  10806. ret = EDI_WRITE(extension->val, output + offset, msgType,
  10807. &offset);
  10808. break;
  10809. #endif
  10810. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  10811. case TLSX_POST_HANDSHAKE_AUTH:
  10812. WOLFSSL_MSG("Post-Handshake Authentication extension to write");
  10813. ret = PHA_WRITE(output + offset, msgType, &offset);
  10814. break;
  10815. #endif
  10816. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10817. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  10818. WOLFSSL_MSG("Signature Algorithms extension to write");
  10819. offset += SAC_WRITE(extension->data, output + offset);
  10820. break;
  10821. #endif
  10822. case TLSX_KEY_SHARE:
  10823. WOLFSSL_MSG("Key Share extension to write");
  10824. offset += KS_WRITE((KeyShareEntry*)extension->data,
  10825. output + offset, msgType);
  10826. break;
  10827. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES)
  10828. case TLSX_CERTIFICATE_AUTHORITIES:
  10829. WOLFSSL_MSG("Certificate Authorities extension to write");
  10830. offset += CAN_WRITE(extension->data, output + offset);
  10831. break;
  10832. #endif
  10833. #endif
  10834. #ifdef WOLFSSL_SRTP
  10835. case TLSX_USE_SRTP:
  10836. WOLFSSL_MSG("SRTP extension to write");
  10837. offset += SRTP_WRITE((TlsxSrtp*)extension->data, output+offset);
  10838. break;
  10839. #endif
  10840. #ifdef HAVE_RPK
  10841. case TLSX_CLIENT_CERTIFICATE_TYPE:
  10842. WOLFSSL_MSG("Client Certificate Type extension to write");
  10843. offset += CCT_WRITE(extension->data, output + offset, msgType);
  10844. break;
  10845. case TLSX_SERVER_CERTIFICATE_TYPE:
  10846. WOLFSSL_MSG("Server Certificate Type extension to write");
  10847. offset += SCT_WRITE(extension->data, output + offset, msgType);
  10848. break;
  10849. #endif /* HAVE_RPK */
  10850. #ifdef WOLFSSL_QUIC
  10851. case TLSX_KEY_QUIC_TP_PARAMS:
  10852. FALL_THROUGH;
  10853. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  10854. WOLFSSL_MSG("QUIC transport parameter to write");
  10855. offset += QTP_WRITE((QuicTransportParam*)extension->data,
  10856. output + offset);
  10857. break;
  10858. #endif
  10859. #ifdef WOLFSSL_DTLS_CID
  10860. case TLSX_CONNECTION_ID:
  10861. WOLFSSL_MSG("Connection ID extension to write");
  10862. offset += CID_WRITE((byte*)extension->data, output+offset);
  10863. break;
  10864. #endif /* WOLFSSL_DTLS_CID */
  10865. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  10866. case TLSX_ECH:
  10867. WOLFSSL_MSG("ECH extension to write");
  10868. ret = ECH_WRITE((WOLFSSL_ECH*)extension->data,
  10869. output + offset, &offset);
  10870. break;
  10871. #endif
  10872. default:
  10873. break;
  10874. }
  10875. /* writes extension data length. */
  10876. c16toa(offset - length_offset, output + length_offset - OPAQUE16_LEN);
  10877. /* marks the extension as processed so ctx level */
  10878. /* extensions don't overlap with ssl level ones. */
  10879. TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
  10880. /* if we encountered an error propagate it */
  10881. if (ret != 0)
  10882. break;
  10883. }
  10884. *pOffset += offset;
  10885. return ret;
  10886. }
  10887. #ifdef HAVE_SUPPORTED_CURVES
  10888. /* Populates the default supported groups / curves */
  10889. static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions)
  10890. {
  10891. int ret = WOLFSSL_SUCCESS;
  10892. #ifdef WOLFSSL_TLS13
  10893. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10894. if (ssl->options.resuming && ssl->session->namedGroup != 0) {
  10895. return TLSX_UseSupportedCurve(extensions, ssl->session->namedGroup,
  10896. ssl->heap);
  10897. }
  10898. #endif
  10899. if (ssl->numGroups != 0) {
  10900. int i;
  10901. for (i = 0; i < ssl->numGroups; i++) {
  10902. ret = TLSX_UseSupportedCurve(extensions, ssl->group[i], ssl->heap);
  10903. if (ret != WOLFSSL_SUCCESS)
  10904. return ret;
  10905. }
  10906. return WOLFSSL_SUCCESS;
  10907. }
  10908. #endif /* WOLFSSL_TLS13 */
  10909. #if defined(HAVE_ECC)
  10910. /* list in order by strength, since not all servers choose by strength */
  10911. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  10912. #ifndef NO_ECC_SECP
  10913. ret = TLSX_UseSupportedCurve(extensions,
  10914. WOLFSSL_ECC_SECP521R1, ssl->heap);
  10915. if (ret != WOLFSSL_SUCCESS) return ret;
  10916. #endif
  10917. #endif
  10918. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  10919. #ifdef HAVE_ECC_BRAINPOOL
  10920. ret = TLSX_UseSupportedCurve(extensions,
  10921. WOLFSSL_ECC_BRAINPOOLP512R1, ssl->heap);
  10922. if (ret != WOLFSSL_SUCCESS) return ret;
  10923. #endif
  10924. #endif
  10925. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  10926. #ifndef NO_ECC_SECP
  10927. ret = TLSX_UseSupportedCurve(extensions,
  10928. WOLFSSL_ECC_SECP384R1, ssl->heap);
  10929. if (ret != WOLFSSL_SUCCESS) return ret;
  10930. #endif
  10931. #ifdef HAVE_ECC_BRAINPOOL
  10932. ret = TLSX_UseSupportedCurve(extensions,
  10933. WOLFSSL_ECC_BRAINPOOLP384R1, ssl->heap);
  10934. if (ret != WOLFSSL_SUCCESS) return ret;
  10935. #endif
  10936. #endif
  10937. #endif /* HAVE_ECC */
  10938. #ifndef HAVE_FIPS
  10939. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  10940. ret = TLSX_UseSupportedCurve(extensions,
  10941. WOLFSSL_ECC_X448, ssl->heap);
  10942. if (ret != WOLFSSL_SUCCESS) return ret;
  10943. #endif
  10944. #endif /* HAVE_FIPS */
  10945. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  10946. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  10947. #ifndef NO_ECC_SECP
  10948. ret = TLSX_UseSupportedCurve(extensions,
  10949. WOLFSSL_ECC_SECP256R1, ssl->heap);
  10950. if (ret != WOLFSSL_SUCCESS) return ret;
  10951. #endif
  10952. #ifdef HAVE_ECC_KOBLITZ
  10953. ret = TLSX_UseSupportedCurve(extensions,
  10954. WOLFSSL_ECC_SECP256K1, ssl->heap);
  10955. if (ret != WOLFSSL_SUCCESS) return ret;
  10956. #endif
  10957. #ifdef HAVE_ECC_BRAINPOOL
  10958. ret = TLSX_UseSupportedCurve(extensions,
  10959. WOLFSSL_ECC_BRAINPOOLP256R1, ssl->heap);
  10960. if (ret != WOLFSSL_SUCCESS) return ret;
  10961. #endif
  10962. #ifdef WOLFSSL_SM2
  10963. ret = TLSX_UseSupportedCurve(extensions,
  10964. WOLFSSL_ECC_SM2P256V1, ssl->heap);
  10965. if (ret != WOLFSSL_SUCCESS) return ret;
  10966. #endif
  10967. #endif
  10968. #endif /* HAVE_ECC */
  10969. #ifndef HAVE_FIPS
  10970. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  10971. ret = TLSX_UseSupportedCurve(extensions,
  10972. WOLFSSL_ECC_X25519, ssl->heap);
  10973. if (ret != WOLFSSL_SUCCESS) return ret;
  10974. #endif
  10975. #endif /* HAVE_FIPS */
  10976. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  10977. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  10978. #ifndef NO_ECC_SECP
  10979. ret = TLSX_UseSupportedCurve(extensions,
  10980. WOLFSSL_ECC_SECP224R1, ssl->heap);
  10981. if (ret != WOLFSSL_SUCCESS) return ret;
  10982. #endif
  10983. #ifdef HAVE_ECC_KOBLITZ
  10984. ret = TLSX_UseSupportedCurve(extensions,
  10985. WOLFSSL_ECC_SECP224K1, ssl->heap);
  10986. if (ret != WOLFSSL_SUCCESS) return ret;
  10987. #endif
  10988. #endif
  10989. #ifndef HAVE_FIPS
  10990. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  10991. #ifndef NO_ECC_SECP
  10992. ret = TLSX_UseSupportedCurve(extensions,
  10993. WOLFSSL_ECC_SECP192R1, ssl->heap);
  10994. if (ret != WOLFSSL_SUCCESS) return ret;
  10995. #endif
  10996. #ifdef HAVE_ECC_KOBLITZ
  10997. ret = TLSX_UseSupportedCurve(extensions,
  10998. WOLFSSL_ECC_SECP192K1, ssl->heap);
  10999. if (ret != WOLFSSL_SUCCESS) return ret;
  11000. #endif
  11001. #endif
  11002. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  11003. #ifndef NO_ECC_SECP
  11004. ret = TLSX_UseSupportedCurve(extensions,
  11005. WOLFSSL_ECC_SECP160R1, ssl->heap);
  11006. if (ret != WOLFSSL_SUCCESS) return ret;
  11007. #endif
  11008. #ifdef HAVE_ECC_SECPR2
  11009. ret = TLSX_UseSupportedCurve(extensions,
  11010. WOLFSSL_ECC_SECP160R2, ssl->heap);
  11011. if (ret != WOLFSSL_SUCCESS) return ret;
  11012. #endif
  11013. #ifdef HAVE_ECC_KOBLITZ
  11014. ret = TLSX_UseSupportedCurve(extensions,
  11015. WOLFSSL_ECC_SECP160K1, ssl->heap);
  11016. if (ret != WOLFSSL_SUCCESS) return ret;
  11017. #endif
  11018. #endif
  11019. #endif /* HAVE_FIPS */
  11020. #endif /* HAVE_ECC */
  11021. #ifndef NO_DH
  11022. /* Add FFDHE supported groups. */
  11023. #ifdef HAVE_FFDHE_8192
  11024. if (8192/8 >= ssl->options.minDhKeySz &&
  11025. 8192/8 <= ssl->options.maxDhKeySz) {
  11026. ret = TLSX_UseSupportedCurve(extensions,
  11027. WOLFSSL_FFDHE_8192, ssl->heap);
  11028. if (ret != WOLFSSL_SUCCESS)
  11029. return ret;
  11030. }
  11031. #endif
  11032. #ifdef HAVE_FFDHE_6144
  11033. if (6144/8 >= ssl->options.minDhKeySz &&
  11034. 6144/8 <= ssl->options.maxDhKeySz) {
  11035. ret = TLSX_UseSupportedCurve(extensions,
  11036. WOLFSSL_FFDHE_6144, ssl->heap);
  11037. if (ret != WOLFSSL_SUCCESS)
  11038. return ret;
  11039. }
  11040. #endif
  11041. #ifdef HAVE_FFDHE_4096
  11042. if (4096/8 >= ssl->options.minDhKeySz &&
  11043. 4096/8 <= ssl->options.maxDhKeySz) {
  11044. ret = TLSX_UseSupportedCurve(extensions,
  11045. WOLFSSL_FFDHE_4096, ssl->heap);
  11046. if (ret != WOLFSSL_SUCCESS)
  11047. return ret;
  11048. }
  11049. #endif
  11050. #ifdef HAVE_FFDHE_3072
  11051. if (3072/8 >= ssl->options.minDhKeySz &&
  11052. 3072/8 <= ssl->options.maxDhKeySz) {
  11053. ret = TLSX_UseSupportedCurve(extensions,
  11054. WOLFSSL_FFDHE_3072, ssl->heap);
  11055. if (ret != WOLFSSL_SUCCESS)
  11056. return ret;
  11057. }
  11058. #endif
  11059. #ifdef HAVE_FFDHE_2048
  11060. if (2048/8 >= ssl->options.minDhKeySz &&
  11061. 2048/8 <= ssl->options.maxDhKeySz) {
  11062. ret = TLSX_UseSupportedCurve(extensions,
  11063. WOLFSSL_FFDHE_2048, ssl->heap);
  11064. if (ret != WOLFSSL_SUCCESS)
  11065. return ret;
  11066. }
  11067. #endif
  11068. #endif
  11069. #ifdef HAVE_PQC
  11070. #ifdef WOLFSSL_WC_KYBER
  11071. #ifdef WOLFSSL_KYBER512
  11072. if (ret == WOLFSSL_SUCCESS)
  11073. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1,
  11074. ssl->heap);
  11075. #endif
  11076. #ifdef WOLFSSL_KYBER768
  11077. if (ret == WOLFSSL_SUCCESS)
  11078. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL3,
  11079. ssl->heap);
  11080. #endif
  11081. #ifdef WOLFSSL_KYBER768
  11082. if (ret == WOLFSSL_SUCCESS)
  11083. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL5,
  11084. ssl->heap);
  11085. #endif
  11086. #elif defined(HAVE_LIBOQS)
  11087. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1, ssl->heap);
  11088. if (ret == WOLFSSL_SUCCESS)
  11089. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL3,
  11090. ssl->heap);
  11091. if (ret == WOLFSSL_SUCCESS)
  11092. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL5,
  11093. ssl->heap);
  11094. if (ret == WOLFSSL_SUCCESS)
  11095. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P256_KYBER_LEVEL1,
  11096. ssl->heap);
  11097. if (ret == WOLFSSL_SUCCESS)
  11098. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_KYBER_LEVEL3,
  11099. ssl->heap);
  11100. if (ret == WOLFSSL_SUCCESS)
  11101. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P521_KYBER_LEVEL5,
  11102. ssl->heap);
  11103. #elif defined(HAVE_PQM4)
  11104. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1, ssl->heap);
  11105. #endif /* HAVE_LIBOQS */
  11106. #endif /* HAVE_PQC */
  11107. (void)ssl;
  11108. (void)extensions;
  11109. return ret;
  11110. }
  11111. #endif /* HAVE_SUPPORTED_CURVES */
  11112. int TLSX_PopulateExtensions(WOLFSSL* ssl, byte isServer)
  11113. {
  11114. int ret = 0;
  11115. byte* public_key = NULL;
  11116. word16 public_key_len = 0;
  11117. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  11118. int usingPSK = 0;
  11119. #endif
  11120. #if defined(HAVE_SUPPORTED_CURVES) && defined(WOLFSSL_TLS13)
  11121. TLSX* extension = NULL;
  11122. word16 namedGroup = WOLFSSL_NAMED_GROUP_INVALID;
  11123. #endif
  11124. /* server will add extension depending on what is parsed from client */
  11125. if (!isServer) {
  11126. #if defined(HAVE_RPK)
  11127. ret = TLSX_ClientCertificateType_Use(ssl, isServer);
  11128. if (ret != 0)
  11129. return ret;
  11130. ret = TLSX_ServerCertificateType_Use(ssl, isServer);
  11131. if (ret != 0)
  11132. return ret;
  11133. #endif /* HAVE_RPK */
  11134. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  11135. if (!ssl->options.disallowEncThenMac) {
  11136. ret = TLSX_EncryptThenMac_Use(ssl);
  11137. if (ret != 0)
  11138. return ret;
  11139. }
  11140. #endif
  11141. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  11142. defined(HAVE_CURVE448)) && defined(HAVE_SUPPORTED_CURVES)
  11143. if (!ssl->options.userCurves && !ssl->ctx->userCurves) {
  11144. if (TLSX_Find(ssl->ctx->extensions,
  11145. TLSX_SUPPORTED_GROUPS) == NULL) {
  11146. ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
  11147. if (ret != WOLFSSL_SUCCESS)
  11148. return ret;
  11149. }
  11150. }
  11151. if ((!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade) &&
  11152. TLSX_Find(ssl->ctx->extensions, TLSX_EC_POINT_FORMATS) == NULL &&
  11153. TLSX_Find(ssl->extensions, TLSX_EC_POINT_FORMATS) == NULL) {
  11154. ret = TLSX_UsePointFormat(&ssl->extensions,
  11155. WOLFSSL_EC_PF_UNCOMPRESSED, ssl->heap);
  11156. if (ret != WOLFSSL_SUCCESS)
  11157. return ret;
  11158. }
  11159. #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
  11160. #ifdef WOLFSSL_SRTP
  11161. if (ssl->options.dtls && ssl->dtlsSrtpProfiles != 0) {
  11162. WOLFSSL_MSG("Adding DTLS SRTP extension");
  11163. if ((ret = TLSX_UseSRTP(&ssl->extensions, ssl->dtlsSrtpProfiles,
  11164. ssl->heap)) != 0) {
  11165. return ret;
  11166. }
  11167. }
  11168. #endif
  11169. #ifdef WOLFSSL_DUAL_ALG_CERTS
  11170. if ((IsAtLeastTLSv1_3(ssl->version)) && (ssl->sigSpec != NULL)) {
  11171. WOLFSSL_MSG("Adding CKS extension");
  11172. if ((ret = TLSX_UseCKS(&ssl->extensions, ssl, ssl->heap)) != 0) {
  11173. return ret;
  11174. }
  11175. }
  11176. #endif
  11177. } /* is not server */
  11178. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  11179. WOLFSSL_MSG("Adding signature algorithms extension");
  11180. if ((ret = TLSX_SetSignatureAlgorithms(&ssl->extensions, ssl, ssl->heap))
  11181. != 0) {
  11182. return ret;
  11183. }
  11184. #else
  11185. ret = 0;
  11186. #endif
  11187. #ifdef WOLFSSL_TLS13
  11188. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES)
  11189. if (isServer && IsAtLeastTLSv1_3(ssl->version)) {
  11190. if (SSL_CA_NAMES(ssl) != NULL) {
  11191. WOLFSSL_MSG("Adding certificate authorities extension");
  11192. if ((ret = TLSX_Push(&ssl->extensions,
  11193. TLSX_CERTIFICATE_AUTHORITIES, ssl, ssl->heap)) != 0) {
  11194. return ret;
  11195. }
  11196. }
  11197. }
  11198. #endif
  11199. if (!isServer && IsAtLeastTLSv1_3(ssl->version)) {
  11200. /* Add mandatory TLS v1.3 extension: supported version */
  11201. WOLFSSL_MSG("Adding supported versions extension");
  11202. if ((ret = TLSX_SetSupportedVersions(&ssl->extensions, ssl,
  11203. ssl->heap)) != 0) {
  11204. return ret;
  11205. }
  11206. #if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && \
  11207. !defined(HAVE_CURVE448) && defined(HAVE_SUPPORTED_CURVES)
  11208. if (TLSX_Find(ssl->ctx->extensions, TLSX_SUPPORTED_GROUPS) == NULL) {
  11209. /* Put in DH groups for TLS 1.3 only. */
  11210. ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
  11211. if (ret != WOLFSSL_SUCCESS)
  11212. return ret;
  11213. /* ret value will be overwritten in !NO_PSK case */
  11214. #ifdef NO_PSK
  11215. ret = 0;
  11216. #endif
  11217. }
  11218. #endif /* !(HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
  11219. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  11220. if (ssl->certHashSigAlgoSz > 0) {
  11221. WOLFSSL_MSG("Adding signature algorithms cert extension");
  11222. if ((ret = TLSX_SetSignatureAlgorithmsCert(&ssl->extensions,
  11223. ssl, ssl->heap)) != 0) {
  11224. return ret;
  11225. }
  11226. }
  11227. #endif
  11228. #if defined(HAVE_SUPPORTED_CURVES)
  11229. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  11230. if (extension == NULL) {
  11231. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11232. if (ssl->options.resuming && ssl->session->namedGroup != 0)
  11233. namedGroup = ssl->session->namedGroup;
  11234. else
  11235. #endif
  11236. if (ssl->numGroups > 0) {
  11237. int set = 0;
  11238. int i, j;
  11239. /* try to find the highest element in ssl->group[]
  11240. * that is contained in preferredGroup[].
  11241. */
  11242. namedGroup = preferredGroup[0];
  11243. for (i = 0; i < ssl->numGroups && !set; i++) {
  11244. for (j = 0; preferredGroup[j] != WOLFSSL_NAMED_GROUP_INVALID; j++) {
  11245. if (preferredGroup[j] == ssl->group[i]
  11246. #ifdef HAVE_LIBOQS
  11247. && TLSX_KeyShare_IsSupported(preferredGroup[j])
  11248. #endif
  11249. ) {
  11250. namedGroup = ssl->group[i];
  11251. set = 1;
  11252. break;
  11253. }
  11254. }
  11255. }
  11256. if (!set)
  11257. namedGroup = WOLFSSL_NAMED_GROUP_INVALID;
  11258. }
  11259. else {
  11260. /* Choose the most preferred group. */
  11261. namedGroup = preferredGroup[0];
  11262. #ifdef HAVE_LIBOQS
  11263. if (!TLSX_KeyShare_IsSupported(namedGroup)) {
  11264. int i = 1;
  11265. for (;preferredGroup[i] != WOLFSSL_NAMED_GROUP_INVALID;
  11266. i++) {
  11267. if (TLSX_KeyShare_IsSupported(preferredGroup[i]))
  11268. break;
  11269. }
  11270. namedGroup = preferredGroup[i];
  11271. }
  11272. #endif
  11273. }
  11274. }
  11275. else {
  11276. KeyShareEntry* kse = (KeyShareEntry*)extension->data;
  11277. if (kse)
  11278. namedGroup = kse->group;
  11279. }
  11280. if (namedGroup != WOLFSSL_NAMED_GROUP_INVALID) {
  11281. ret = TLSX_KeyShare_Use(ssl, namedGroup, 0, NULL, NULL,
  11282. &ssl->extensions);
  11283. if (ret != 0)
  11284. return ret;
  11285. }
  11286. #endif /* HAVE_SUPPORTED_CURVES */
  11287. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11288. TLSX_Remove(&ssl->extensions, TLSX_PRE_SHARED_KEY, ssl->heap);
  11289. #endif
  11290. #if defined(HAVE_SESSION_TICKET)
  11291. if (ssl->options.resuming && ssl->session->ticketLen > 0) {
  11292. WOLFSSL_SESSION* sess = ssl->session;
  11293. #ifdef WOLFSSL_32BIT_MILLI_TIME
  11294. word32 now, milli;
  11295. #else
  11296. word64 now, milli;
  11297. #endif
  11298. if (sess->ticketLen > MAX_PSK_ID_LEN) {
  11299. WOLFSSL_MSG("Session ticket length for PSK ext is too large");
  11300. return BUFFER_ERROR;
  11301. }
  11302. /* Determine the MAC algorithm for the cipher suite used. */
  11303. ssl->options.cipherSuite0 = sess->cipherSuite0;
  11304. ssl->options.cipherSuite = sess->cipherSuite;
  11305. ret = SetCipherSpecs(ssl);
  11306. if (ret != 0)
  11307. return ret;
  11308. now = TimeNowInMilliseconds();
  11309. if (now == 0)
  11310. return GETTIME_ERROR;
  11311. #ifdef WOLFSSL_32BIT_MILLI_TIME
  11312. if (now < sess->ticketSeen)
  11313. milli = (0xFFFFFFFFU - sess->ticketSeen) + 1 + now;
  11314. else
  11315. milli = now - sess->ticketSeen;
  11316. milli += sess->ticketAdd;
  11317. /* Pre-shared key is mandatory extension for resumption. */
  11318. ret = TLSX_PreSharedKey_Use(&ssl->extensions, sess->ticket,
  11319. sess->ticketLen, milli, ssl->specs.mac_algorithm,
  11320. ssl->options.cipherSuite0, ssl->options.cipherSuite, 1,
  11321. NULL, ssl->heap);
  11322. #else
  11323. milli = now - sess->ticketSeen + sess->ticketAdd;
  11324. /* Pre-shared key is mandatory extension for resumption. */
  11325. ret = TLSX_PreSharedKey_Use(&ssl->extensions, sess->ticket,
  11326. sess->ticketLen, (word32)milli, ssl->specs.mac_algorithm,
  11327. ssl->options.cipherSuite0, ssl->options.cipherSuite, 1,
  11328. NULL, ssl->heap);
  11329. #endif
  11330. if (ret != 0)
  11331. return ret;
  11332. usingPSK = 1;
  11333. }
  11334. #endif
  11335. #ifndef NO_PSK
  11336. #ifndef WOLFSSL_PSK_ONE_ID
  11337. if (ssl->options.client_psk_cs_cb != NULL) {
  11338. int i;
  11339. const Suites* suites = WOLFSSL_SUITES(ssl);
  11340. for (i = 0; i < suites->suiteSz; i += 2) {
  11341. byte cipherSuite0 = suites->suites[i + 0];
  11342. byte cipherSuite = suites->suites[i + 1];
  11343. unsigned int keySz;
  11344. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  11345. int cnt = 0;
  11346. #endif
  11347. #ifdef HAVE_NULL_CIPHER
  11348. if (cipherSuite0 == ECC_BYTE ||
  11349. cipherSuite0 == ECDHE_PSK_BYTE) {
  11350. if (cipherSuite != TLS_SHA256_SHA256 &&
  11351. cipherSuite != TLS_SHA384_SHA384) {
  11352. continue;
  11353. }
  11354. }
  11355. else
  11356. #endif
  11357. #if (defined(WOLFSSL_SM4_GCM) || defined(WOLFSSL_SM4_CCM)) && \
  11358. defined(WOLFSSL_SM3)
  11359. if (cipherSuite0 == CIPHER_BYTE) {
  11360. if ((cipherSuite != TLS_SM4_GCM_SM3) &&
  11361. (cipherSuite != TLS_SM4_CCM_SM3)) {
  11362. continue;
  11363. }
  11364. }
  11365. else
  11366. #endif
  11367. if (cipherSuite0 != TLS13_BYTE)
  11368. continue;
  11369. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  11370. do {
  11371. ssl->arrays->client_identity[0] = cnt;
  11372. #endif
  11373. ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0';
  11374. keySz = ssl->options.client_psk_cs_cb(
  11375. ssl, ssl->arrays->server_hint,
  11376. ssl->arrays->client_identity, MAX_PSK_ID_LEN,
  11377. ssl->arrays->psk_key, MAX_PSK_KEY_LEN,
  11378. GetCipherNameInternal(cipherSuite0, cipherSuite));
  11379. if (keySz > 0) {
  11380. ssl->arrays->psk_keySz = keySz;
  11381. ret = TLSX_PreSharedKey_Use(&ssl->extensions,
  11382. (byte*)ssl->arrays->client_identity,
  11383. (word16)XSTRLEN(ssl->arrays->client_identity),
  11384. 0, SuiteMac(WOLFSSL_SUITES(ssl)->suites + i),
  11385. cipherSuite0, cipherSuite, 0, NULL, ssl->heap);
  11386. if (ret != 0)
  11387. return ret;
  11388. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  11389. cnt++;
  11390. #endif
  11391. }
  11392. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  11393. }
  11394. while (keySz > 0);
  11395. #endif
  11396. }
  11397. usingPSK = 1;
  11398. }
  11399. else
  11400. #endif
  11401. if (ssl->options.client_psk_cb != NULL ||
  11402. ssl->options.client_psk_tls13_cb != NULL) {
  11403. /* Default cipher suite. */
  11404. byte cipherSuite0 = TLS13_BYTE;
  11405. byte cipherSuite = WOLFSSL_DEF_PSK_CIPHER;
  11406. int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE;
  11407. const char* cipherName = NULL;
  11408. if (ssl->options.client_psk_tls13_cb != NULL) {
  11409. ssl->arrays->psk_keySz = ssl->options.client_psk_tls13_cb(
  11410. ssl, ssl->arrays->server_hint,
  11411. ssl->arrays->client_identity, MAX_PSK_ID_LEN,
  11412. ssl->arrays->psk_key, MAX_PSK_KEY_LEN, &cipherName);
  11413. if (GetCipherSuiteFromName(cipherName, &cipherSuite0,
  11414. &cipherSuite, &cipherSuiteFlags) != 0) {
  11415. return PSK_KEY_ERROR;
  11416. }
  11417. }
  11418. else {
  11419. ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl,
  11420. ssl->arrays->server_hint, ssl->arrays->client_identity,
  11421. MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN);
  11422. }
  11423. if (
  11424. #ifdef OPENSSL_EXTRA
  11425. /* OpenSSL treats a PSK key length of 0
  11426. * to indicate no PSK available.
  11427. */
  11428. ssl->arrays->psk_keySz == 0 ||
  11429. #endif
  11430. (ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN &&
  11431. (int)ssl->arrays->psk_keySz != USE_HW_PSK)) {
  11432. #ifndef OPENSSL_EXTRA
  11433. ret = PSK_KEY_ERROR;
  11434. #endif
  11435. }
  11436. else {
  11437. ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0';
  11438. ssl->options.cipherSuite0 = cipherSuite0;
  11439. ssl->options.cipherSuite = cipherSuite;
  11440. (void)cipherSuiteFlags;
  11441. ret = SetCipherSpecs(ssl);
  11442. if (ret == 0) {
  11443. ret = TLSX_PreSharedKey_Use(
  11444. &ssl->extensions,
  11445. (byte*)ssl->arrays->client_identity,
  11446. (word16)XSTRLEN(ssl->arrays->client_identity),
  11447. 0, ssl->specs.mac_algorithm,
  11448. cipherSuite0, cipherSuite, 0,
  11449. NULL, ssl->heap);
  11450. }
  11451. if (ret == 0)
  11452. usingPSK = 1;
  11453. }
  11454. if (ret != 0)
  11455. return ret;
  11456. }
  11457. #endif /* !NO_PSK */
  11458. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11459. /* Some servers do not generate session tickets unless
  11460. * the extension is seen in a non-resume client hello.
  11461. * We used to send it only if we were otherwise using PSK.
  11462. * Now always send it. Define NO_TLSX_PSKKEM_PLAIN_ANNOUNCE
  11463. * to revert to the old behaviour. */
  11464. #ifdef NO_TLSX_PSKKEM_PLAIN_ANNOUNCE
  11465. if (usingPSK)
  11466. #endif
  11467. {
  11468. byte modes = 0;
  11469. (void)usingPSK;
  11470. /* Pre-shared key modes: mandatory extension for resumption. */
  11471. #ifdef HAVE_SUPPORTED_CURVES
  11472. if (!ssl->options.onlyPskDheKe)
  11473. #endif
  11474. {
  11475. modes = 1 << PSK_KE;
  11476. }
  11477. #if !defined(NO_DH) || defined(HAVE_ECC) || \
  11478. defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  11479. if (!ssl->options.noPskDheKe) {
  11480. modes |= 1 << PSK_DHE_KE;
  11481. }
  11482. #endif
  11483. ret = TLSX_PskKeyModes_Use(ssl, modes);
  11484. if (ret != 0)
  11485. return ret;
  11486. }
  11487. #endif
  11488. #if defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  11489. if (!isServer && ssl->options.postHandshakeAuth) {
  11490. ret = TLSX_PostHandAuth_Use(ssl);
  11491. if (ret != 0)
  11492. return ret;
  11493. }
  11494. #endif
  11495. #if defined(HAVE_ECH)
  11496. /* GREASE ECH */
  11497. if (ssl->echConfigs == NULL) {
  11498. ret = GREASE_ECH_USE(&(ssl->extensions), ssl->heap, ssl->rng);
  11499. }
  11500. else if (ssl->echConfigs != NULL) {
  11501. ret = ECH_USE(ssl->echConfigs, &(ssl->extensions), ssl->heap,
  11502. ssl->rng);
  11503. }
  11504. #endif
  11505. }
  11506. #if defined(HAVE_ECH)
  11507. else if (IsAtLeastTLSv1_3(ssl->version)) {
  11508. if (ssl->ctx->echConfigs != NULL) {
  11509. ret = SERVER_ECH_USE(&(ssl->extensions), ssl->heap,
  11510. ssl->ctx->echConfigs);
  11511. if (ret == 0)
  11512. TLSX_SetResponse(ssl, TLSX_ECH);
  11513. }
  11514. }
  11515. #endif
  11516. #endif
  11517. (void)isServer;
  11518. (void)public_key;
  11519. (void)public_key_len;
  11520. (void)ssl;
  11521. return ret;
  11522. }
  11523. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  11524. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  11525. /* because the size of ech depends on the size of other extensions we need to
  11526. * get the size with ech special and process ech last, return status */
  11527. static int TLSX_GetSizeWithEch(WOLFSSL* ssl, byte* semaphore, byte msgType,
  11528. word16* pLength)
  11529. {
  11530. int ret = 0;
  11531. TLSX* echX = NULL;
  11532. TLSX* serverNameX = NULL;
  11533. TLSX** extensions = NULL;
  11534. #ifdef WOLFSSL_SMALL_STACK
  11535. char* tmpServerName = NULL;
  11536. #else
  11537. char tmpServerName[MAX_PUBLIC_NAME_SZ];
  11538. #endif
  11539. /* calculate the rest of the extensions length with inner ech */
  11540. if (ssl->extensions)
  11541. echX = TLSX_Find(ssl->extensions, TLSX_ECH);
  11542. if (echX == NULL && ssl->ctx && ssl->ctx->extensions)
  11543. echX = TLSX_Find(ssl->ctx->extensions, TLSX_ECH);
  11544. /* if type is outer change sni to public name */
  11545. if (echX != NULL && ((WOLFSSL_ECH*)echX->data)->type == ECH_TYPE_OUTER) {
  11546. if (ssl->extensions) {
  11547. serverNameX = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  11548. if (serverNameX != NULL)
  11549. extensions = &ssl->extensions;
  11550. }
  11551. if (serverNameX == NULL && ssl->ctx && ssl->ctx->extensions) {
  11552. serverNameX = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  11553. extensions = &ssl->ctx->extensions;
  11554. }
  11555. /* store the inner server name */
  11556. if (serverNameX != NULL) {
  11557. char* hostName = ((SNI*)serverNameX->data)->data.host_name;
  11558. word32 hostNameSz = (word32)XSTRLEN(hostName) + 1;
  11559. #ifdef WOLFSSL_SMALL_STACK
  11560. tmpServerName = (char*)XMALLOC(hostNameSz, ssl->heap,
  11561. DYNAMIC_TYPE_TMP_BUFFER);
  11562. if (tmpServerName == NULL)
  11563. return MEMORY_E;
  11564. #else
  11565. /* truncate if too long */
  11566. if (hostNameSz > MAX_PUBLIC_NAME_SZ)
  11567. hostNameSz = MAX_PUBLIC_NAME_SZ;
  11568. #endif
  11569. XMEMCPY(tmpServerName, hostName, hostNameSz);
  11570. }
  11571. /* remove the inner server name */
  11572. TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
  11573. ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME,
  11574. ((WOLFSSL_ECH*)echX->data)->echConfig->publicName,
  11575. XSTRLEN(((WOLFSSL_ECH*)echX->data)->echConfig->publicName),
  11576. ssl->heap);
  11577. /* set the public name as the server name */
  11578. if (ret == WOLFSSL_SUCCESS)
  11579. ret = 0;
  11580. }
  11581. if (ret == 0 && ssl->extensions)
  11582. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, pLength);
  11583. if (ret == 0 && ssl->ctx && ssl->ctx->extensions)
  11584. ret = TLSX_GetSize(ssl->ctx->extensions, semaphore, msgType, pLength);
  11585. if (serverNameX != NULL) {
  11586. /* remove the public name SNI */
  11587. TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
  11588. ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME,
  11589. tmpServerName, XSTRLEN(tmpServerName), ssl->heap);
  11590. /* restore the inner server name */
  11591. if (ret == WOLFSSL_SUCCESS)
  11592. ret = 0;
  11593. }
  11594. #ifdef WOLFSSL_SMALL_STACK
  11595. XFREE(tmpServerName, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  11596. #endif
  11597. return ret;
  11598. }
  11599. #endif
  11600. /** Tells the buffered size of extensions to be sent into the client hello. */
  11601. int TLSX_GetRequestSize(WOLFSSL* ssl, byte msgType, word16* pLength)
  11602. {
  11603. int ret = 0;
  11604. word16 length = 0;
  11605. byte semaphore[SEMAPHORE_SIZE] = {0};
  11606. if (!TLSX_SupportExtensions(ssl))
  11607. return 0;
  11608. if (msgType == client_hello) {
  11609. EC_VALIDATE_REQUEST(ssl, semaphore);
  11610. PF_VALIDATE_REQUEST(ssl, semaphore);
  11611. WOLF_STK_VALIDATE_REQUEST(ssl);
  11612. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  11613. if (WOLFSSL_SUITES(ssl)->hashSigAlgoSz == 0)
  11614. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  11615. #endif
  11616. #if defined(WOLFSSL_TLS13)
  11617. if (!IsAtLeastTLSv1_2(ssl)) {
  11618. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  11619. }
  11620. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  11621. if (!IsAtLeastTLSv1_3(ssl->version)) {
  11622. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  11623. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11624. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  11625. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
  11626. #endif
  11627. #ifdef WOLFSSL_EARLY_DATA
  11628. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  11629. #endif
  11630. #ifdef WOLFSSL_SEND_HRR_COOKIE
  11631. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  11632. #endif
  11633. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  11634. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
  11635. #endif
  11636. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES)
  11637. TURN_ON(semaphore,
  11638. TLSX_ToSemaphore(TLSX_CERTIFICATE_AUTHORITIES));
  11639. #endif
  11640. }
  11641. #endif
  11642. #endif /* WOLFSSL_TLS13 */
  11643. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  11644. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  11645. if (!SSL_CM(ssl)->ocspStaplingEnabled) {
  11646. /* mark already sent, so it won't send it */
  11647. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  11648. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  11649. }
  11650. #endif
  11651. }
  11652. #ifdef WOLFSSL_TLS13
  11653. #ifndef NO_CERTS
  11654. else if (msgType == certificate_request) {
  11655. /* Don't send out any extension except those that are turned off. */
  11656. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11657. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  11658. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  11659. #endif
  11660. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES)
  11661. if (SSL_CA_NAMES(ssl) != NULL)
  11662. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_CERTIFICATE_AUTHORITIES));
  11663. #endif
  11664. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP, OID_FILTERS
  11665. * TLSX_STATUS_REQUEST
  11666. */
  11667. }
  11668. #endif
  11669. #if defined(HAVE_ECH)
  11670. if (ssl->options.useEch == 1 && msgType == client_hello) {
  11671. ret = TLSX_GetSizeWithEch(ssl, semaphore, msgType, &length);
  11672. if (ret != 0)
  11673. return ret;
  11674. }
  11675. else
  11676. #endif /* HAVE_ECH */
  11677. #endif /* WOLFSSL_TLS13 */
  11678. {
  11679. if (ssl->extensions) {
  11680. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
  11681. if (ret != 0)
  11682. return ret;
  11683. }
  11684. if (ssl->ctx && ssl->ctx->extensions) {
  11685. ret = TLSX_GetSize(ssl->ctx->extensions, semaphore, msgType,
  11686. &length);
  11687. if (ret != 0)
  11688. return ret;
  11689. }
  11690. }
  11691. #ifdef HAVE_EXTENDED_MASTER
  11692. if (msgType == client_hello && ssl->options.haveEMS &&
  11693. (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
  11694. length += HELLO_EXT_SZ;
  11695. }
  11696. #endif
  11697. if (length)
  11698. length += OPAQUE16_LEN; /* for total length storage. */
  11699. *pLength += length;
  11700. return ret;
  11701. }
  11702. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  11703. /* return status after writing the extensions with ech written last */
  11704. static int TLSX_WriteWithEch(WOLFSSL* ssl, byte* output, byte* semaphore,
  11705. byte msgType, word16* pOffset)
  11706. {
  11707. int ret = 0;
  11708. TLSX* echX = NULL;
  11709. TLSX* serverNameX = NULL;
  11710. TLSX** extensions = NULL;
  11711. #ifdef WOLFSSL_SMALL_STACK
  11712. char* tmpServerName = NULL;
  11713. #else
  11714. char tmpServerName[MAX_PUBLIC_NAME_SZ];
  11715. #endif
  11716. /* get the echX from either extensions or ctx */
  11717. if (ssl->extensions)
  11718. echX = TLSX_Find(ssl->extensions, TLSX_ECH);
  11719. if (echX == NULL && ssl->ctx && ssl->ctx->extensions) {
  11720. /* if not NULL the semaphore will stop it from being counted */
  11721. if (echX == NULL)
  11722. echX = TLSX_Find(ssl->ctx->extensions, TLSX_ECH);
  11723. }
  11724. /* if type is outer change sni to public name */
  11725. if (echX != NULL && ((WOLFSSL_ECH*)echX->data)->type == ECH_TYPE_OUTER) {
  11726. if (ssl->extensions) {
  11727. serverNameX = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  11728. if (serverNameX != NULL)
  11729. extensions = &ssl->extensions;
  11730. }
  11731. if (serverNameX == NULL && ssl->ctx && ssl->ctx->extensions) {
  11732. serverNameX = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  11733. extensions = &ssl->ctx->extensions;
  11734. }
  11735. /* store the inner server name */
  11736. if (serverNameX != NULL) {
  11737. char* hostName = ((SNI*)serverNameX->data)->data.host_name;
  11738. word32 hostNameSz = (word32)XSTRLEN(hostName) + 1;
  11739. #ifdef WOLFSSL_SMALL_STACK
  11740. tmpServerName = (char*)XMALLOC(hostNameSz, ssl->heap,
  11741. DYNAMIC_TYPE_TMP_BUFFER);
  11742. if (tmpServerName == NULL)
  11743. return MEMORY_E;
  11744. #else
  11745. /* truncate if too long */
  11746. if (hostNameSz > MAX_PUBLIC_NAME_SZ)
  11747. hostNameSz = MAX_PUBLIC_NAME_SZ;
  11748. #endif
  11749. XMEMCPY(tmpServerName, hostName, hostNameSz);
  11750. }
  11751. /* remove the inner server name */
  11752. TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
  11753. ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME,
  11754. ((WOLFSSL_ECH*)echX->data)->echConfig->publicName,
  11755. XSTRLEN(((WOLFSSL_ECH*)echX->data)->echConfig->publicName),
  11756. ssl->heap);
  11757. /* set the public name as the server name */
  11758. if (ret == WOLFSSL_SUCCESS)
  11759. ret = 0;
  11760. }
  11761. if (echX != NULL) {
  11762. /* turn ech on so it doesn't write, then write it last */
  11763. TURN_ON(semaphore, TLSX_ToSemaphore(echX->type));
  11764. }
  11765. if (ret == 0 && ssl->extensions) {
  11766. ret = TLSX_Write(ssl->extensions, output + *pOffset, semaphore,
  11767. msgType, pOffset);
  11768. }
  11769. if (ret == 0 && ssl->ctx && ssl->ctx->extensions) {
  11770. ret = TLSX_Write(ssl->ctx->extensions, output + *pOffset, semaphore,
  11771. msgType, pOffset);
  11772. }
  11773. if (echX != NULL) {
  11774. /* turn off and write it last */
  11775. TURN_OFF(semaphore, TLSX_ToSemaphore(echX->type));
  11776. }
  11777. if (ret == 0 && ssl->extensions) {
  11778. ret = TLSX_Write(ssl->extensions, output + *pOffset, semaphore,
  11779. msgType, pOffset);
  11780. }
  11781. if (ret == 0 && ssl->ctx && ssl->ctx->extensions) {
  11782. ret = TLSX_Write(ssl->ctx->extensions, output + *pOffset, semaphore,
  11783. msgType, pOffset);
  11784. }
  11785. if (serverNameX != NULL) {
  11786. /* remove the public name SNI */
  11787. TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
  11788. ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME, tmpServerName,
  11789. XSTRLEN(tmpServerName), ssl->heap);
  11790. /* restore the inner server name */
  11791. if (ret == WOLFSSL_SUCCESS)
  11792. ret = 0;
  11793. }
  11794. #ifdef WOLFSSL_SMALL_STACK
  11795. XFREE(tmpServerName, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  11796. #endif
  11797. return ret;
  11798. }
  11799. #endif
  11800. /** Writes the extensions to be sent into the client hello. */
  11801. int TLSX_WriteRequest(WOLFSSL* ssl, byte* output, byte msgType, word16* pOffset)
  11802. {
  11803. int ret = 0;
  11804. word16 offset = 0;
  11805. byte semaphore[SEMAPHORE_SIZE] = {0};
  11806. if (!TLSX_SupportExtensions(ssl) || output == NULL)
  11807. return 0;
  11808. offset += OPAQUE16_LEN; /* extensions length */
  11809. if (msgType == client_hello) {
  11810. EC_VALIDATE_REQUEST(ssl, semaphore);
  11811. PF_VALIDATE_REQUEST(ssl, semaphore);
  11812. WOLF_STK_VALIDATE_REQUEST(ssl);
  11813. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  11814. if (WOLFSSL_SUITES(ssl)->hashSigAlgoSz == 0)
  11815. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  11816. #endif
  11817. #ifdef WOLFSSL_TLS13
  11818. if (!IsAtLeastTLSv1_2(ssl)) {
  11819. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  11820. }
  11821. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  11822. if (!IsAtLeastTLSv1_3(ssl->version)) {
  11823. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  11824. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11825. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
  11826. #endif
  11827. #ifdef WOLFSSL_EARLY_DATA
  11828. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  11829. #endif
  11830. #ifdef WOLFSSL_SEND_HRR_COOKIE
  11831. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  11832. #endif
  11833. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  11834. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
  11835. #endif
  11836. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES)
  11837. TURN_ON(semaphore,
  11838. TLSX_ToSemaphore(TLSX_CERTIFICATE_AUTHORITIES));
  11839. #endif
  11840. #ifdef WOLFSSL_DUAL_ALG_CERTS
  11841. TURN_ON(semaphore,
  11842. TLSX_ToSemaphore(TLSX_CKS));
  11843. #endif
  11844. }
  11845. #endif
  11846. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11847. /* Must write Pre-shared Key extension at the end in TLS v1.3.
  11848. * Must not write out Pre-shared Key extension in earlier versions of
  11849. * protocol.
  11850. */
  11851. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  11852. #endif
  11853. #endif /* WOLFSSL_TLS13 */
  11854. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  11855. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  11856. /* mark already sent, so it won't send it */
  11857. if (!SSL_CM(ssl)->ocspStaplingEnabled) {
  11858. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  11859. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  11860. }
  11861. #endif
  11862. }
  11863. #ifdef WOLFSSL_TLS13
  11864. #ifndef NO_CERTS
  11865. else if (msgType == certificate_request) {
  11866. /* Don't send out any extension except those that are turned off. */
  11867. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11868. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  11869. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  11870. #endif
  11871. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES)
  11872. if (SSL_CA_NAMES(ssl) != NULL) {
  11873. TURN_OFF(semaphore,
  11874. TLSX_ToSemaphore(TLSX_CERTIFICATE_AUTHORITIES));
  11875. }
  11876. #endif
  11877. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP, TLSX_OID_FILTERS
  11878. * TLSX_STATUS_REQUEST
  11879. */
  11880. }
  11881. #endif
  11882. #endif
  11883. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  11884. if (ssl->options.useEch == 1 && msgType == client_hello) {
  11885. ret = TLSX_WriteWithEch(ssl, output, semaphore,
  11886. msgType, &offset);
  11887. if (ret != 0)
  11888. return ret;
  11889. }
  11890. else
  11891. #endif
  11892. {
  11893. if (ssl->extensions) {
  11894. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  11895. msgType, &offset);
  11896. if (ret != 0)
  11897. return ret;
  11898. }
  11899. if (ssl->ctx && ssl->ctx->extensions) {
  11900. ret = TLSX_Write(ssl->ctx->extensions, output + offset, semaphore,
  11901. msgType, &offset);
  11902. if (ret != 0)
  11903. return ret;
  11904. }
  11905. }
  11906. #ifdef HAVE_EXTENDED_MASTER
  11907. if (msgType == client_hello && ssl->options.haveEMS &&
  11908. (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
  11909. WOLFSSL_MSG("EMS extension to write");
  11910. c16toa(HELLO_EXT_EXTMS, output + offset);
  11911. offset += HELLO_EXT_TYPE_SZ;
  11912. c16toa(0, output + offset);
  11913. offset += HELLO_EXT_SZ_SZ;
  11914. }
  11915. #endif
  11916. #ifdef WOLFSSL_TLS13
  11917. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11918. if (msgType == client_hello && IsAtLeastTLSv1_3(ssl->version)) {
  11919. /* Write out what we can of Pre-shared key extension. */
  11920. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  11921. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  11922. client_hello, &offset);
  11923. if (ret != 0)
  11924. return ret;
  11925. }
  11926. #endif
  11927. #endif
  11928. if (offset > OPAQUE16_LEN || msgType != client_hello)
  11929. c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
  11930. *pOffset += offset;
  11931. return ret;
  11932. }
  11933. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  11934. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_SERVER)
  11935. /** Tells the buffered size of extensions to be sent into the server hello. */
  11936. int TLSX_GetResponseSize(WOLFSSL* ssl, byte msgType, word16* pLength)
  11937. {
  11938. int ret = 0;
  11939. word16 length = 0;
  11940. byte semaphore[SEMAPHORE_SIZE] = {0};
  11941. switch (msgType) {
  11942. #ifndef NO_WOLFSSL_SERVER
  11943. case server_hello:
  11944. PF_VALIDATE_RESPONSE(ssl, semaphore);
  11945. #ifdef WOLFSSL_TLS13
  11946. if (IsAtLeastTLSv1_3(ssl->version)) {
  11947. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11948. TURN_OFF(semaphore,
  11949. TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  11950. #if defined(HAVE_SUPPORTED_CURVES)
  11951. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11952. if (!ssl->options.noPskDheKe)
  11953. #endif
  11954. {
  11955. /* Expect KeyShare extension in ServerHello. */
  11956. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  11957. }
  11958. #endif
  11959. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11960. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  11961. #endif
  11962. #ifdef WOLFSSL_DTLS_CID
  11963. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  11964. #endif
  11965. }
  11966. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  11967. else {
  11968. #ifdef HAVE_SUPPORTED_CURVES
  11969. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  11970. #endif
  11971. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11972. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  11973. #endif
  11974. }
  11975. #endif
  11976. #endif /* WOLFSSL_TLS13 */
  11977. break;
  11978. #ifdef WOLFSSL_TLS13
  11979. case hello_retry_request:
  11980. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11981. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  11982. #ifdef HAVE_SUPPORTED_CURVES
  11983. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11984. if (!ssl->options.noPskDheKe)
  11985. #endif
  11986. {
  11987. /* Expect KeyShare extension in HelloRetryRequest. */
  11988. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  11989. }
  11990. #endif
  11991. #ifdef WOLFSSL_SEND_HRR_COOKIE
  11992. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  11993. #endif
  11994. break;
  11995. #endif
  11996. #ifdef WOLFSSL_TLS13
  11997. case encrypted_extensions:
  11998. /* Send out all extension except those that are turned on. */
  11999. #ifdef HAVE_ECC
  12000. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  12001. #endif
  12002. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  12003. #ifdef HAVE_SESSION_TICKET
  12004. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
  12005. #endif
  12006. #ifdef HAVE_SUPPORTED_CURVES
  12007. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  12008. #endif
  12009. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  12010. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  12011. #endif
  12012. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  12013. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  12014. #endif
  12015. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  12016. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  12017. #endif
  12018. #if defined(HAVE_SERVER_RENEGOTIATION_INFO)
  12019. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
  12020. #endif
  12021. #ifdef WOLFSSL_DTLS_CID
  12022. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  12023. #endif /* WOLFSSL_DTLS_CID */
  12024. break;
  12025. #ifdef WOLFSSL_EARLY_DATA
  12026. case session_ticket:
  12027. if (ssl->options.tls1_3) {
  12028. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  12029. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  12030. }
  12031. break;
  12032. #endif
  12033. #endif
  12034. #endif
  12035. #ifdef WOLFSSL_TLS13
  12036. #ifndef NO_CERTS
  12037. case certificate:
  12038. /* Don't send out any extension except those that are turned off. */
  12039. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  12040. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  12041. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  12042. * TLSX_SERVER_CERTIFICATE_TYPE
  12043. */
  12044. break;
  12045. #endif
  12046. #endif
  12047. }
  12048. #ifdef HAVE_EXTENDED_MASTER
  12049. if (ssl->options.haveEMS && msgType == server_hello &&
  12050. !IsAtLeastTLSv1_3(ssl->version)) {
  12051. length += HELLO_EXT_SZ;
  12052. }
  12053. #endif
  12054. if (TLSX_SupportExtensions(ssl)) {
  12055. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
  12056. if (ret != 0)
  12057. return ret;
  12058. }
  12059. /* All the response data is set at the ssl object only, so no ctx here. */
  12060. if (length || msgType != server_hello)
  12061. length += OPAQUE16_LEN; /* for total length storage. */
  12062. *pLength += length;
  12063. return ret;
  12064. }
  12065. /** Writes the server hello extensions into a buffer. */
  12066. int TLSX_WriteResponse(WOLFSSL *ssl, byte* output, byte msgType, word16* pOffset)
  12067. {
  12068. int ret = 0;
  12069. word16 offset = 0;
  12070. if (TLSX_SupportExtensions(ssl) && output) {
  12071. byte semaphore[SEMAPHORE_SIZE] = {0};
  12072. switch (msgType) {
  12073. #ifndef NO_WOLFSSL_SERVER
  12074. case server_hello:
  12075. PF_VALIDATE_RESPONSE(ssl, semaphore);
  12076. #ifdef WOLFSSL_TLS13
  12077. if (IsAtLeastTLSv1_3(ssl->version)) {
  12078. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  12079. TURN_OFF(semaphore,
  12080. TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  12081. #ifdef HAVE_SUPPORTED_CURVES
  12082. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  12083. if (!ssl->options.noPskDheKe)
  12084. #endif
  12085. {
  12086. /* Write out KeyShare in ServerHello. */
  12087. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  12088. }
  12089. #endif
  12090. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  12091. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  12092. #endif
  12093. #ifdef WOLFSSL_DTLS_CID
  12094. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  12095. #endif /* WOLFSSL_DTLS_CID */
  12096. }
  12097. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  12098. else {
  12099. #ifdef HAVE_SUPPORTED_CURVES
  12100. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  12101. #endif
  12102. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  12103. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  12104. #endif
  12105. }
  12106. #endif
  12107. #endif
  12108. break;
  12109. #ifdef WOLFSSL_TLS13
  12110. case hello_retry_request:
  12111. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  12112. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  12113. #ifdef HAVE_SUPPORTED_CURVES
  12114. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  12115. if (!ssl->options.noPskDheKe)
  12116. #endif
  12117. {
  12118. /* Write out KeyShare in HelloRetryRequest. */
  12119. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  12120. }
  12121. #endif
  12122. /* Cookie is written below as last extension. */
  12123. break;
  12124. #endif
  12125. #ifdef WOLFSSL_TLS13
  12126. case encrypted_extensions:
  12127. /* Send out all extension except those that are turned on. */
  12128. #ifdef HAVE_ECC
  12129. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  12130. #endif
  12131. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  12132. #ifdef HAVE_SESSION_TICKET
  12133. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
  12134. #endif
  12135. #ifdef HAVE_SUPPORTED_CURVES
  12136. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  12137. #endif
  12138. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  12139. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  12140. #endif
  12141. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  12142. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  12143. #endif
  12144. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  12145. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  12146. #endif
  12147. #if defined(HAVE_SERVER_RENEGOTIATION_INFO)
  12148. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
  12149. #endif
  12150. #ifdef WOLFSSL_DTLS_CID
  12151. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  12152. #endif /* WOLFSSL_DTLS_CID */
  12153. break;
  12154. #ifdef WOLFSSL_EARLY_DATA
  12155. case session_ticket:
  12156. if (ssl->options.tls1_3) {
  12157. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  12158. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  12159. }
  12160. break;
  12161. #endif
  12162. #endif
  12163. #endif
  12164. #ifdef WOLFSSL_TLS13
  12165. #ifndef NO_CERTS
  12166. case certificate:
  12167. /* Don't send out any extension except those that are turned
  12168. * off. */
  12169. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  12170. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  12171. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  12172. * TLSX_SERVER_CERTIFICATE_TYPE
  12173. */
  12174. break;
  12175. #endif
  12176. #endif
  12177. default:
  12178. break;
  12179. }
  12180. offset += OPAQUE16_LEN; /* extensions length */
  12181. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  12182. msgType, &offset);
  12183. if (ret != 0)
  12184. return ret;
  12185. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  12186. if (msgType == hello_retry_request) {
  12187. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  12188. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  12189. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  12190. msgType, &offset);
  12191. if (ret != 0)
  12192. return ret;
  12193. }
  12194. #endif
  12195. #ifdef HAVE_EXTENDED_MASTER
  12196. if (ssl->options.haveEMS && msgType == server_hello &&
  12197. !IsAtLeastTLSv1_3(ssl->version)) {
  12198. WOLFSSL_MSG("EMS extension to write");
  12199. c16toa(HELLO_EXT_EXTMS, output + offset);
  12200. offset += HELLO_EXT_TYPE_SZ;
  12201. c16toa(0, output + offset);
  12202. offset += HELLO_EXT_SZ_SZ;
  12203. }
  12204. #endif
  12205. if (offset > OPAQUE16_LEN || msgType != server_hello)
  12206. c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
  12207. }
  12208. if (pOffset)
  12209. *pOffset += offset;
  12210. return ret;
  12211. }
  12212. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_SERVER */
  12213. #ifdef WOLFSSL_TLS13
  12214. int TLSX_ParseVersion(WOLFSSL* ssl, const byte* input, word16 length,
  12215. byte msgType, int* found)
  12216. {
  12217. int ret = 0;
  12218. int offset = 0;
  12219. *found = 0;
  12220. while (offset < (int)length) {
  12221. word16 type;
  12222. word16 size;
  12223. if (offset + (2 * OPAQUE16_LEN) > length) {
  12224. ret = BUFFER_ERROR;
  12225. break;
  12226. }
  12227. ato16(input + offset, &type);
  12228. offset += HELLO_EXT_TYPE_SZ;
  12229. ato16(input + offset, &size);
  12230. offset += OPAQUE16_LEN;
  12231. if (offset + size > length) {
  12232. ret = BUFFER_ERROR;
  12233. break;
  12234. }
  12235. if (type == TLSX_SUPPORTED_VERSIONS) {
  12236. *found = 1;
  12237. WOLFSSL_MSG("Supported Versions extension received");
  12238. ret = SV_PARSE(ssl, input + offset, size, msgType, &ssl->version,
  12239. &ssl->options, &ssl->extensions);
  12240. break;
  12241. }
  12242. offset += size;
  12243. }
  12244. return ret;
  12245. }
  12246. #endif
  12247. /** Parses a buffer of TLS extensions. */
  12248. int TLSX_Parse(WOLFSSL* ssl, const byte* input, word16 length, byte msgType,
  12249. Suites *suites)
  12250. {
  12251. int ret = 0;
  12252. word16 offset = 0;
  12253. byte isRequest = (msgType == client_hello ||
  12254. msgType == certificate_request);
  12255. #ifdef HAVE_EXTENDED_MASTER
  12256. byte pendingEMS = 0;
  12257. #endif
  12258. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  12259. int pskDone = 0;
  12260. #endif
  12261. byte seenType[SEMAPHORE_SIZE]; /* Seen known extensions. */
  12262. if (!ssl || !input || (isRequest && !suites))
  12263. return BAD_FUNC_ARG;
  12264. /* No known extensions seen yet. */
  12265. XMEMSET(seenType, 0, sizeof(seenType));
  12266. while (ret == 0 && offset < length) {
  12267. word16 type;
  12268. word16 size;
  12269. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  12270. if (msgType == client_hello && pskDone) {
  12271. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  12272. return PSK_KEY_ERROR;
  12273. }
  12274. #endif
  12275. if (length - offset < HELLO_EXT_TYPE_SZ + OPAQUE16_LEN)
  12276. return BUFFER_ERROR;
  12277. ato16(input + offset, &type);
  12278. offset += HELLO_EXT_TYPE_SZ;
  12279. ato16(input + offset, &size);
  12280. offset += OPAQUE16_LEN;
  12281. /* Check we have a bit for extension type. */
  12282. if ((type <= 62) || (type == TLSX_RENEGOTIATION_INFO)
  12283. #ifdef WOLFSSL_QUIC
  12284. || (type == TLSX_KEY_QUIC_TP_PARAMS_DRAFT)
  12285. #endif
  12286. )
  12287. {
  12288. /* Detect duplicate recognized extensions. */
  12289. if (IS_OFF(seenType, TLSX_ToSemaphore(type))) {
  12290. TURN_ON(seenType, TLSX_ToSemaphore(type));
  12291. }
  12292. else {
  12293. return DUPLICATE_TLS_EXT_E;
  12294. }
  12295. }
  12296. if (length - offset < size)
  12297. return BUFFER_ERROR;
  12298. switch (type) {
  12299. #ifdef HAVE_SNI
  12300. case TLSX_SERVER_NAME:
  12301. WOLFSSL_MSG("SNI extension received");
  12302. #ifdef WOLFSSL_DEBUG_TLS
  12303. WOLFSSL_BUFFER(input + offset, size);
  12304. #endif
  12305. #ifdef WOLFSSL_TLS13
  12306. if (IsAtLeastTLSv1_3(ssl->version)) {
  12307. if (msgType != client_hello &&
  12308. msgType != encrypted_extensions)
  12309. return EXT_NOT_ALLOWED;
  12310. }
  12311. else
  12312. #endif
  12313. {
  12314. if (msgType != client_hello &&
  12315. msgType != server_hello)
  12316. return EXT_NOT_ALLOWED;
  12317. }
  12318. ret = SNI_PARSE(ssl, input + offset, size, isRequest);
  12319. break;
  12320. #endif
  12321. case TLSX_TRUSTED_CA_KEYS:
  12322. WOLFSSL_MSG("Trusted CA extension received");
  12323. #ifdef WOLFSSL_DEBUG_TLS
  12324. WOLFSSL_BUFFER(input + offset, size);
  12325. #endif
  12326. #ifdef WOLFSSL_TLS13
  12327. /* RFC 8446 4.2.4 states trusted_ca_keys is not used
  12328. in TLS 1.3. */
  12329. if (IsAtLeastTLSv1_3(ssl->version)) {
  12330. return EXT_NOT_ALLOWED;
  12331. }
  12332. else
  12333. #endif
  12334. {
  12335. if (msgType != client_hello &&
  12336. msgType != server_hello)
  12337. return EXT_NOT_ALLOWED;
  12338. }
  12339. ret = TCA_PARSE(ssl, input + offset, size, isRequest);
  12340. break;
  12341. case TLSX_MAX_FRAGMENT_LENGTH:
  12342. WOLFSSL_MSG("Max Fragment Length extension received");
  12343. #ifdef WOLFSSL_DEBUG_TLS
  12344. WOLFSSL_BUFFER(input + offset, size);
  12345. #endif
  12346. #ifdef WOLFSSL_TLS13
  12347. if (IsAtLeastTLSv1_3(ssl->version)) {
  12348. if (msgType != client_hello &&
  12349. msgType != encrypted_extensions) {
  12350. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  12351. return EXT_NOT_ALLOWED;
  12352. }
  12353. }
  12354. else
  12355. #endif
  12356. {
  12357. if (msgType != client_hello &&
  12358. msgType != server_hello) {
  12359. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  12360. return EXT_NOT_ALLOWED;
  12361. }
  12362. }
  12363. ret = MFL_PARSE(ssl, input + offset, size, isRequest);
  12364. break;
  12365. case TLSX_TRUNCATED_HMAC:
  12366. WOLFSSL_MSG("Truncated HMAC extension received");
  12367. #ifdef WOLFSSL_DEBUG_TLS
  12368. WOLFSSL_BUFFER(input + offset, size);
  12369. #endif
  12370. #ifdef WOLFSSL_TLS13
  12371. if (IsAtLeastTLSv1_3(ssl->version))
  12372. break;
  12373. #endif
  12374. if (msgType != client_hello)
  12375. return EXT_NOT_ALLOWED;
  12376. ret = THM_PARSE(ssl, input + offset, size, isRequest);
  12377. break;
  12378. case TLSX_SUPPORTED_GROUPS:
  12379. WOLFSSL_MSG("Supported Groups extension received");
  12380. #ifdef WOLFSSL_DEBUG_TLS
  12381. WOLFSSL_BUFFER(input + offset, size);
  12382. #endif
  12383. #ifdef WOLFSSL_TLS13
  12384. if (IsAtLeastTLSv1_3(ssl->version)) {
  12385. if (msgType != client_hello &&
  12386. msgType != encrypted_extensions) {
  12387. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  12388. return EXT_NOT_ALLOWED;
  12389. }
  12390. }
  12391. else
  12392. #endif
  12393. {
  12394. if (msgType != client_hello) {
  12395. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  12396. return EXT_NOT_ALLOWED;
  12397. }
  12398. }
  12399. ret = EC_PARSE(ssl, input + offset, size, isRequest,
  12400. &ssl->extensions);
  12401. break;
  12402. #ifdef WOLFSSL_DUAL_ALG_CERTS
  12403. case TLSX_CKS:
  12404. WOLFSSL_MSG("CKS extension received");
  12405. if (!IsAtLeastTLSv1_3(ssl->version) ||
  12406. (msgType != client_hello &&
  12407. msgType != encrypted_extensions)) {
  12408. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  12409. return EXT_NOT_ALLOWED;
  12410. }
  12411. ret = TLSX_CKS_Parse(ssl, (byte *)(input + offset), size,
  12412. &ssl->extensions);
  12413. break;
  12414. #endif /* WOLFSSL_DUAL_ALG_CERTS */
  12415. case TLSX_EC_POINT_FORMATS:
  12416. WOLFSSL_MSG("Point Formats extension received");
  12417. #ifdef WOLFSSL_DEBUG_TLS
  12418. WOLFSSL_BUFFER(input + offset, size);
  12419. #endif
  12420. #ifdef WOLFSSL_TLS13
  12421. if (IsAtLeastTLSv1_3(ssl->version))
  12422. break;
  12423. #endif
  12424. if (msgType != client_hello &&
  12425. msgType != server_hello) {
  12426. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  12427. return EXT_NOT_ALLOWED;
  12428. }
  12429. ret = PF_PARSE(ssl, input + offset, size, isRequest);
  12430. break;
  12431. case TLSX_STATUS_REQUEST:
  12432. WOLFSSL_MSG("Certificate Status Request extension received");
  12433. #ifdef WOLFSSL_DEBUG_TLS
  12434. WOLFSSL_BUFFER(input + offset, size);
  12435. #endif
  12436. #ifdef WOLFSSL_TLS13
  12437. if (IsAtLeastTLSv1_3(ssl->version)) {
  12438. if (msgType != client_hello &&
  12439. msgType != certificate_request &&
  12440. msgType != certificate)
  12441. return EXT_NOT_ALLOWED;
  12442. }
  12443. else
  12444. #endif
  12445. {
  12446. if (msgType != client_hello &&
  12447. msgType != server_hello)
  12448. return EXT_NOT_ALLOWED;
  12449. }
  12450. ret = CSR_PARSE(ssl, input + offset, size, isRequest);
  12451. break;
  12452. case TLSX_STATUS_REQUEST_V2:
  12453. WOLFSSL_MSG("Certificate Status Request v2 extension received");
  12454. #ifdef WOLFSSL_DEBUG_TLS
  12455. WOLFSSL_BUFFER(input + offset, size);
  12456. #endif
  12457. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  12458. if (IsAtLeastTLSv1_3(ssl->version)) {
  12459. if (msgType != client_hello &&
  12460. msgType != certificate_request &&
  12461. msgType != certificate)
  12462. return EXT_NOT_ALLOWED;
  12463. }
  12464. else
  12465. #endif
  12466. {
  12467. if (msgType != client_hello &&
  12468. msgType != server_hello)
  12469. return EXT_NOT_ALLOWED;
  12470. }
  12471. ret = CSR2_PARSE(ssl, input + offset, size, isRequest);
  12472. break;
  12473. #ifdef HAVE_EXTENDED_MASTER
  12474. case HELLO_EXT_EXTMS:
  12475. WOLFSSL_MSG("Extended Master Secret extension received");
  12476. #ifdef WOLFSSL_DEBUG_TLS
  12477. WOLFSSL_BUFFER(input + offset, size);
  12478. #endif
  12479. #if defined(WOLFSSL_TLS13)
  12480. if (IsAtLeastTLSv1_3(ssl->version))
  12481. break;
  12482. #endif
  12483. if (msgType != client_hello &&
  12484. msgType != server_hello)
  12485. return EXT_NOT_ALLOWED;
  12486. if (size != 0)
  12487. return BUFFER_ERROR;
  12488. #ifndef NO_WOLFSSL_SERVER
  12489. if (isRequest)
  12490. ssl->options.haveEMS = 1;
  12491. #endif
  12492. pendingEMS = 1;
  12493. break;
  12494. #endif
  12495. case TLSX_RENEGOTIATION_INFO:
  12496. WOLFSSL_MSG("Secure Renegotiation extension received");
  12497. #ifdef WOLFSSL_DEBUG_TLS
  12498. WOLFSSL_BUFFER(input + offset, size);
  12499. #endif
  12500. #ifdef WOLFSSL_TLS13
  12501. if (IsAtLeastTLSv1_3(ssl->version))
  12502. break;
  12503. #endif
  12504. if (msgType != client_hello &&
  12505. msgType != server_hello)
  12506. return EXT_NOT_ALLOWED;
  12507. ret = SCR_PARSE(ssl, input + offset, size, isRequest);
  12508. break;
  12509. case TLSX_SESSION_TICKET:
  12510. WOLFSSL_MSG("Session Ticket extension received");
  12511. #ifdef WOLFSSL_DEBUG_TLS
  12512. WOLFSSL_BUFFER(input + offset, size);
  12513. #endif
  12514. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  12515. if (IsAtLeastTLSv1_3(ssl->version)) {
  12516. if (msgType != client_hello)
  12517. return EXT_NOT_ALLOWED;
  12518. }
  12519. else
  12520. #endif
  12521. {
  12522. if (msgType != client_hello &&
  12523. msgType != server_hello)
  12524. return EXT_NOT_ALLOWED;
  12525. }
  12526. ret = WOLF_STK_PARSE(ssl, input + offset, size, isRequest);
  12527. break;
  12528. case TLSX_APPLICATION_LAYER_PROTOCOL:
  12529. WOLFSSL_MSG("ALPN extension received");
  12530. #ifdef WOLFSSL_DEBUG_TLS
  12531. WOLFSSL_BUFFER(input + offset, size);
  12532. #endif
  12533. #if defined(WOLFSSL_TLS13) && defined(HAVE_ALPN)
  12534. if (IsAtLeastTLSv1_3(ssl->version)) {
  12535. if (msgType != client_hello &&
  12536. msgType != encrypted_extensions)
  12537. return EXT_NOT_ALLOWED;
  12538. }
  12539. else
  12540. #endif
  12541. {
  12542. if (msgType != client_hello &&
  12543. msgType != server_hello)
  12544. return EXT_NOT_ALLOWED;
  12545. }
  12546. ret = ALPN_PARSE(ssl, input + offset, size, isRequest);
  12547. break;
  12548. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  12549. case TLSX_SIGNATURE_ALGORITHMS:
  12550. WOLFSSL_MSG("Signature Algorithms extension received");
  12551. #ifdef WOLFSSL_DEBUG_TLS
  12552. WOLFSSL_BUFFER(input + offset, size);
  12553. #endif
  12554. if (!IsAtLeastTLSv1_2(ssl))
  12555. break;
  12556. #ifdef WOLFSSL_TLS13
  12557. if (IsAtLeastTLSv1_3(ssl->version)) {
  12558. if (msgType != client_hello &&
  12559. msgType != certificate_request)
  12560. return EXT_NOT_ALLOWED;
  12561. }
  12562. else
  12563. #endif
  12564. {
  12565. if (msgType != client_hello)
  12566. return EXT_NOT_ALLOWED;
  12567. }
  12568. ret = SA_PARSE(ssl, input + offset, size, isRequest, suites);
  12569. break;
  12570. #endif
  12571. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  12572. case TLSX_ENCRYPT_THEN_MAC:
  12573. WOLFSSL_MSG("Encrypt-Then-Mac extension received");
  12574. /* Ignore for TLS 1.3+ */
  12575. if (IsAtLeastTLSv1_3(ssl->version))
  12576. break;
  12577. if (msgType != client_hello &&
  12578. msgType != server_hello)
  12579. return EXT_NOT_ALLOWED;
  12580. ret = ETM_PARSE(ssl, input + offset, size, msgType);
  12581. break;
  12582. #endif /* HAVE_ENCRYPT_THEN_MAC */
  12583. #ifdef WOLFSSL_TLS13
  12584. case TLSX_SUPPORTED_VERSIONS:
  12585. WOLFSSL_MSG("Skipping Supported Versions - already processed");
  12586. #ifdef WOLFSSL_DEBUG_TLS
  12587. WOLFSSL_BUFFER(input + offset, size);
  12588. #endif
  12589. if (msgType != client_hello &&
  12590. msgType != server_hello &&
  12591. msgType != hello_retry_request)
  12592. return EXT_NOT_ALLOWED;
  12593. break;
  12594. #ifdef WOLFSSL_SEND_HRR_COOKIE
  12595. case TLSX_COOKIE:
  12596. WOLFSSL_MSG("Cookie extension received");
  12597. #ifdef WOLFSSL_DEBUG_TLS
  12598. WOLFSSL_BUFFER(input + offset, size);
  12599. #endif
  12600. if (!IsAtLeastTLSv1_3(ssl->version))
  12601. break;
  12602. if (msgType != client_hello &&
  12603. msgType != hello_retry_request) {
  12604. return EXT_NOT_ALLOWED;
  12605. }
  12606. ret = CKE_PARSE(ssl, input + offset, size, msgType);
  12607. break;
  12608. #endif
  12609. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  12610. case TLSX_PRE_SHARED_KEY:
  12611. WOLFSSL_MSG("Pre-Shared Key extension received");
  12612. #ifdef WOLFSSL_DEBUG_TLS
  12613. WOLFSSL_BUFFER(input + offset, size);
  12614. #endif
  12615. if (!IsAtLeastTLSv1_3(ssl->version))
  12616. break;
  12617. if (msgType != client_hello &&
  12618. msgType != server_hello) {
  12619. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  12620. return EXT_NOT_ALLOWED;
  12621. }
  12622. ret = PSK_PARSE(ssl, input + offset, size, msgType);
  12623. pskDone = 1;
  12624. break;
  12625. case TLSX_PSK_KEY_EXCHANGE_MODES:
  12626. WOLFSSL_MSG("PSK Key Exchange Modes extension received");
  12627. #ifdef WOLFSSL_DEBUG_TLS
  12628. WOLFSSL_BUFFER(input + offset, size);
  12629. #endif
  12630. if (!IsAtLeastTLSv1_3(ssl->version))
  12631. break;
  12632. if (msgType != client_hello) {
  12633. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  12634. return EXT_NOT_ALLOWED;
  12635. }
  12636. ret = PKM_PARSE(ssl, input + offset, size, msgType);
  12637. break;
  12638. #endif
  12639. #ifdef WOLFSSL_EARLY_DATA
  12640. case TLSX_EARLY_DATA:
  12641. WOLFSSL_MSG("Early Data extension received");
  12642. #ifdef WOLFSSL_DEBUG_TLS
  12643. WOLFSSL_BUFFER(input + offset, size);
  12644. #endif
  12645. if (!IsAtLeastTLSv1_3(ssl->version))
  12646. break;
  12647. if (msgType != client_hello && msgType != session_ticket &&
  12648. msgType != encrypted_extensions) {
  12649. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  12650. return EXT_NOT_ALLOWED;
  12651. }
  12652. ret = EDI_PARSE(ssl, input + offset, size, msgType);
  12653. break;
  12654. #endif
  12655. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  12656. case TLSX_POST_HANDSHAKE_AUTH:
  12657. WOLFSSL_MSG("Post Handshake Authentication extension received");
  12658. #ifdef WOLFSSL_DEBUG_TLS
  12659. WOLFSSL_BUFFER(input + offset, size);
  12660. #endif
  12661. if (!IsAtLeastTLSv1_3(ssl->version))
  12662. break;
  12663. if (msgType != client_hello) {
  12664. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  12665. return EXT_NOT_ALLOWED;
  12666. }
  12667. ret = PHA_PARSE(ssl, input + offset, size, msgType);
  12668. break;
  12669. #endif
  12670. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  12671. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  12672. WOLFSSL_MSG("Signature Algorithms extension received");
  12673. #ifdef WOLFSSL_DEBUG_TLS
  12674. WOLFSSL_BUFFER(input + offset, size);
  12675. #endif
  12676. if (!IsAtLeastTLSv1_3(ssl->version))
  12677. break;
  12678. if (msgType != client_hello &&
  12679. msgType != certificate_request) {
  12680. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  12681. return EXT_NOT_ALLOWED;
  12682. }
  12683. ret = SAC_PARSE(ssl, input + offset, size, isRequest);
  12684. break;
  12685. #endif
  12686. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES)
  12687. case TLSX_CERTIFICATE_AUTHORITIES:
  12688. WOLFSSL_MSG("Certificate Authorities extension received");
  12689. #ifdef WOLFSSL_DEBUG_TLS
  12690. WOLFSSL_BUFFER(input + offset, size);
  12691. #endif
  12692. if (!IsAtLeastTLSv1_3(ssl->version))
  12693. break;
  12694. if (msgType != client_hello &&
  12695. msgType != certificate_request) {
  12696. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  12697. return EXT_NOT_ALLOWED;
  12698. }
  12699. ret = CAN_PARSE(ssl, input + offset, size, isRequest);
  12700. break;
  12701. #endif
  12702. case TLSX_KEY_SHARE:
  12703. WOLFSSL_MSG("Key Share extension received");
  12704. #ifdef WOLFSSL_DEBUG_TLS
  12705. WOLFSSL_BUFFER(input + offset, size);
  12706. #endif
  12707. #ifdef HAVE_SUPPORTED_CURVES
  12708. if (!IsAtLeastTLSv1_3(ssl->version))
  12709. break;
  12710. if (msgType != client_hello && msgType != server_hello &&
  12711. msgType != hello_retry_request) {
  12712. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  12713. return EXT_NOT_ALLOWED;
  12714. }
  12715. #endif
  12716. ret = KS_PARSE(ssl, input + offset, size, msgType);
  12717. break;
  12718. #endif
  12719. #ifdef WOLFSSL_SRTP
  12720. case TLSX_USE_SRTP:
  12721. WOLFSSL_MSG("Use SRTP extension received");
  12722. ret = SRTP_PARSE(ssl, input + offset, size, isRequest);
  12723. break;
  12724. #endif
  12725. #ifdef WOLFSSL_QUIC
  12726. case TLSX_KEY_QUIC_TP_PARAMS:
  12727. FALL_THROUGH;
  12728. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  12729. WOLFSSL_MSG("QUIC transport parameter received");
  12730. #ifdef WOLFSSL_DEBUG_TLS
  12731. WOLFSSL_BUFFER(input + offset, size);
  12732. #endif
  12733. if (IsAtLeastTLSv1_3(ssl->version) &&
  12734. msgType != client_hello &&
  12735. msgType != server_hello &&
  12736. msgType != encrypted_extensions) {
  12737. return EXT_NOT_ALLOWED;
  12738. }
  12739. else if (!IsAtLeastTLSv1_3(ssl->version) &&
  12740. msgType == encrypted_extensions) {
  12741. return EXT_NOT_ALLOWED;
  12742. }
  12743. else if (WOLFSSL_IS_QUIC(ssl)) {
  12744. ret = QTP_PARSE(ssl, input + offset, size, type, msgType);
  12745. }
  12746. else {
  12747. WOLFSSL_MSG("QUIC transport param TLS extension type, but no QUIC");
  12748. return EXT_NOT_ALLOWED; /* be safe, this should not happen */
  12749. }
  12750. break;
  12751. #endif /* WOLFSSL_QUIC */
  12752. #if defined(WOLFSSL_DTLS_CID)
  12753. case TLSX_CONNECTION_ID:
  12754. /* connection ID not supported in DTLSv1.2 */
  12755. if (!IsAtLeastTLSv1_3(ssl->version))
  12756. break;
  12757. if (msgType != client_hello && msgType != server_hello)
  12758. return EXT_NOT_ALLOWED;
  12759. WOLFSSL_MSG("ConnectionID extension received");
  12760. ret = CID_PARSE(ssl, input + offset, size, isRequest);
  12761. break;
  12762. #endif /* defined(WOLFSSL_DTLS_CID) */
  12763. #if defined(HAVE_RPK)
  12764. case TLSX_CLIENT_CERTIFICATE_TYPE:
  12765. WOLFSSL_MSG("Client Certificate Type extension received");
  12766. ret = CCT_PARSE(ssl, input + offset, size, msgType);
  12767. break;
  12768. case TLSX_SERVER_CERTIFICATE_TYPE:
  12769. WOLFSSL_MSG("Server Certificate Type extension received");
  12770. ret = SCT_PARSE(ssl, input + offset, size, msgType);
  12771. break;
  12772. #endif /* HAVE_RPK */
  12773. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  12774. case TLSX_ECH:
  12775. WOLFSSL_MSG("ECH extension received");
  12776. ret = ECH_PARSE(ssl, input + offset, size, msgType);
  12777. break;
  12778. #endif
  12779. default:
  12780. WOLFSSL_MSG("Unknown TLS extension type");
  12781. }
  12782. /* offset should be updated here! */
  12783. offset += size;
  12784. }
  12785. #ifdef HAVE_EXTENDED_MASTER
  12786. if (IsAtLeastTLSv1_3(ssl->version) && msgType == hello_retry_request) {
  12787. /* Don't change EMS status until server_hello received.
  12788. * Second ClientHello must have same extensions.
  12789. */
  12790. }
  12791. else if (!isRequest && ssl->options.haveEMS && !pendingEMS)
  12792. ssl->options.haveEMS = 0;
  12793. #endif
  12794. if (ret == 0)
  12795. ret = SNI_VERIFY_PARSE(ssl, isRequest);
  12796. if (ret == 0)
  12797. ret = TCA_VERIFY_PARSE(ssl, isRequest);
  12798. return ret;
  12799. }
  12800. /* undefining semaphore macros */
  12801. #undef IS_OFF
  12802. #undef TURN_ON
  12803. #undef SEMAPHORE_SIZE
  12804. #endif /* HAVE_TLS_EXTENSIONS */
  12805. #ifndef NO_WOLFSSL_CLIENT
  12806. WOLFSSL_METHOD* wolfTLS_client_method(void)
  12807. {
  12808. return wolfTLS_client_method_ex(NULL);
  12809. }
  12810. WOLFSSL_METHOD* wolfTLS_client_method_ex(void* heap)
  12811. {
  12812. WOLFSSL_METHOD* method =
  12813. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12814. heap, DYNAMIC_TYPE_METHOD);
  12815. (void)heap;
  12816. WOLFSSL_ENTER("TLS_client_method_ex");
  12817. if (method) {
  12818. #if defined(WOLFSSL_TLS13)
  12819. InitSSL_Method(method, MakeTLSv1_3());
  12820. #elif !defined(WOLFSSL_NO_TLS12)
  12821. InitSSL_Method(method, MakeTLSv1_2());
  12822. #elif !defined(NO_OLD_TLS)
  12823. InitSSL_Method(method, MakeTLSv1_1());
  12824. #elif defined(WOLFSSL_ALLOW_TLSV10)
  12825. InitSSL_Method(method, MakeTLSv1());
  12826. #else
  12827. #error No TLS version enabled!
  12828. #endif
  12829. method->downgrade = 1;
  12830. method->side = WOLFSSL_CLIENT_END;
  12831. }
  12832. return method;
  12833. }
  12834. #ifndef NO_OLD_TLS
  12835. #ifdef WOLFSSL_ALLOW_TLSV10
  12836. WOLFSSL_METHOD* wolfTLSv1_client_method(void)
  12837. {
  12838. return wolfTLSv1_client_method_ex(NULL);
  12839. }
  12840. WOLFSSL_METHOD* wolfTLSv1_client_method_ex(void* heap)
  12841. {
  12842. WOLFSSL_METHOD* method =
  12843. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12844. heap, DYNAMIC_TYPE_METHOD);
  12845. (void)heap;
  12846. WOLFSSL_ENTER("TLSv1_client_method_ex");
  12847. if (method)
  12848. InitSSL_Method(method, MakeTLSv1());
  12849. return method;
  12850. }
  12851. #endif /* WOLFSSL_ALLOW_TLSV10 */
  12852. WOLFSSL_METHOD* wolfTLSv1_1_client_method(void)
  12853. {
  12854. return wolfTLSv1_1_client_method_ex(NULL);
  12855. }
  12856. WOLFSSL_METHOD* wolfTLSv1_1_client_method_ex(void* heap)
  12857. {
  12858. WOLFSSL_METHOD* method =
  12859. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12860. heap, DYNAMIC_TYPE_METHOD);
  12861. (void)heap;
  12862. WOLFSSL_ENTER("TLSv1_1_client_method_ex");
  12863. if (method)
  12864. InitSSL_Method(method, MakeTLSv1_1());
  12865. return method;
  12866. }
  12867. #endif /* !NO_OLD_TLS */
  12868. #ifndef WOLFSSL_NO_TLS12
  12869. WOLFSSL_ABI
  12870. WOLFSSL_METHOD* wolfTLSv1_2_client_method(void)
  12871. {
  12872. return wolfTLSv1_2_client_method_ex(NULL);
  12873. }
  12874. WOLFSSL_METHOD* wolfTLSv1_2_client_method_ex(void* heap)
  12875. {
  12876. WOLFSSL_METHOD* method =
  12877. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12878. heap, DYNAMIC_TYPE_METHOD);
  12879. (void)heap;
  12880. WOLFSSL_ENTER("TLSv1_2_client_method_ex");
  12881. if (method)
  12882. InitSSL_Method(method, MakeTLSv1_2());
  12883. return method;
  12884. }
  12885. #endif /* WOLFSSL_NO_TLS12 */
  12886. #ifdef WOLFSSL_TLS13
  12887. /* The TLS v1.3 client method data.
  12888. *
  12889. * returns the method data for a TLS v1.3 client.
  12890. */
  12891. WOLFSSL_ABI
  12892. WOLFSSL_METHOD* wolfTLSv1_3_client_method(void)
  12893. {
  12894. return wolfTLSv1_3_client_method_ex(NULL);
  12895. }
  12896. /* The TLS v1.3 client method data.
  12897. *
  12898. * heap The heap used for allocation.
  12899. * returns the method data for a TLS v1.3 client.
  12900. */
  12901. WOLFSSL_METHOD* wolfTLSv1_3_client_method_ex(void* heap)
  12902. {
  12903. WOLFSSL_METHOD* method = (WOLFSSL_METHOD*)
  12904. XMALLOC(sizeof(WOLFSSL_METHOD), heap,
  12905. DYNAMIC_TYPE_METHOD);
  12906. (void)heap;
  12907. WOLFSSL_ENTER("TLSv1_3_client_method_ex");
  12908. if (method)
  12909. InitSSL_Method(method, MakeTLSv1_3());
  12910. return method;
  12911. }
  12912. #endif /* WOLFSSL_TLS13 */
  12913. #ifdef WOLFSSL_DTLS
  12914. WOLFSSL_METHOD* wolfDTLS_client_method(void)
  12915. {
  12916. return wolfDTLS_client_method_ex(NULL);
  12917. }
  12918. WOLFSSL_METHOD* wolfDTLS_client_method_ex(void* heap)
  12919. {
  12920. WOLFSSL_METHOD* method =
  12921. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12922. heap, DYNAMIC_TYPE_METHOD);
  12923. (void)heap;
  12924. WOLFSSL_ENTER("DTLS_client_method_ex");
  12925. if (method) {
  12926. #if defined(WOLFSSL_DTLS13)
  12927. InitSSL_Method(method, MakeDTLSv1_3());
  12928. #elif !defined(WOLFSSL_NO_TLS12)
  12929. InitSSL_Method(method, MakeDTLSv1_2());
  12930. #elif !defined(NO_OLD_TLS)
  12931. InitSSL_Method(method, MakeDTLSv1());
  12932. #else
  12933. #error No DTLS version enabled!
  12934. #endif
  12935. method->downgrade = 1;
  12936. method->side = WOLFSSL_CLIENT_END;
  12937. }
  12938. return method;
  12939. }
  12940. #ifndef NO_OLD_TLS
  12941. WOLFSSL_METHOD* wolfDTLSv1_client_method(void)
  12942. {
  12943. return wolfDTLSv1_client_method_ex(NULL);
  12944. }
  12945. WOLFSSL_METHOD* wolfDTLSv1_client_method_ex(void* heap)
  12946. {
  12947. WOLFSSL_METHOD* method =
  12948. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12949. heap, DYNAMIC_TYPE_METHOD);
  12950. (void)heap;
  12951. WOLFSSL_ENTER("DTLSv1_client_method_ex");
  12952. if (method)
  12953. InitSSL_Method(method, MakeDTLSv1());
  12954. return method;
  12955. }
  12956. #endif /* NO_OLD_TLS */
  12957. #ifndef WOLFSSL_NO_TLS12
  12958. WOLFSSL_METHOD* wolfDTLSv1_2_client_method(void)
  12959. {
  12960. return wolfDTLSv1_2_client_method_ex(NULL);
  12961. }
  12962. WOLFSSL_METHOD* wolfDTLSv1_2_client_method_ex(void* heap)
  12963. {
  12964. WOLFSSL_METHOD* method =
  12965. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12966. heap, DYNAMIC_TYPE_METHOD);
  12967. (void)heap;
  12968. WOLFSSL_ENTER("DTLSv1_2_client_method_ex");
  12969. if (method)
  12970. InitSSL_Method(method, MakeDTLSv1_2());
  12971. (void)heap;
  12972. return method;
  12973. }
  12974. #endif /* !WOLFSSL_NO_TLS12 */
  12975. #endif /* WOLFSSL_DTLS */
  12976. #endif /* NO_WOLFSSL_CLIENT */
  12977. /* EITHER SIDE METHODS */
  12978. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  12979. #ifndef NO_OLD_TLS
  12980. #ifdef WOLFSSL_ALLOW_TLSV10
  12981. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  12982. *
  12983. * Returns a pointer to a WOLFSSL_METHOD struct
  12984. */
  12985. WOLFSSL_METHOD* wolfTLSv1_method(void)
  12986. {
  12987. return wolfTLSv1_method_ex(NULL);
  12988. }
  12989. WOLFSSL_METHOD* wolfTLSv1_method_ex(void* heap)
  12990. {
  12991. WOLFSSL_METHOD* m;
  12992. WOLFSSL_ENTER("TLSv1_method");
  12993. #ifndef NO_WOLFSSL_CLIENT
  12994. m = wolfTLSv1_client_method_ex(heap);
  12995. #else
  12996. m = wolfTLSv1_server_method_ex(heap);
  12997. #endif
  12998. if (m != NULL) {
  12999. m->side = WOLFSSL_NEITHER_END;
  13000. }
  13001. return m;
  13002. }
  13003. #endif /* WOLFSSL_ALLOW_TLSV10 */
  13004. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  13005. *
  13006. * Returns a pointer to a WOLFSSL_METHOD struct
  13007. */
  13008. WOLFSSL_METHOD* wolfTLSv1_1_method(void)
  13009. {
  13010. return wolfTLSv1_1_method_ex(NULL);
  13011. }
  13012. WOLFSSL_METHOD* wolfTLSv1_1_method_ex(void* heap)
  13013. {
  13014. WOLFSSL_METHOD* m;
  13015. WOLFSSL_ENTER("TLSv1_1_method");
  13016. #ifndef NO_WOLFSSL_CLIENT
  13017. m = wolfTLSv1_1_client_method_ex(heap);
  13018. #else
  13019. m = wolfTLSv1_1_server_method_ex(heap);
  13020. #endif
  13021. if (m != NULL) {
  13022. m->side = WOLFSSL_NEITHER_END;
  13023. }
  13024. return m;
  13025. }
  13026. #endif /* !NO_OLD_TLS */
  13027. #ifndef WOLFSSL_NO_TLS12
  13028. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  13029. *
  13030. * Returns a pointer to a WOLFSSL_METHOD struct
  13031. */
  13032. WOLFSSL_METHOD* wolfTLSv1_2_method(void)
  13033. {
  13034. return wolfTLSv1_2_method_ex(NULL);
  13035. }
  13036. WOLFSSL_METHOD* wolfTLSv1_2_method_ex(void* heap)
  13037. {
  13038. WOLFSSL_METHOD* m;
  13039. WOLFSSL_ENTER("TLSv1_2_method");
  13040. #ifndef NO_WOLFSSL_CLIENT
  13041. m = wolfTLSv1_2_client_method_ex(heap);
  13042. #else
  13043. m = wolfTLSv1_2_server_method_ex(heap);
  13044. #endif
  13045. if (m != NULL) {
  13046. m->side = WOLFSSL_NEITHER_END;
  13047. }
  13048. return m;
  13049. }
  13050. #endif /* !WOLFSSL_NO_TLS12 */
  13051. #ifdef WOLFSSL_TLS13
  13052. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  13053. *
  13054. * Returns a pointer to a WOLFSSL_METHOD struct
  13055. */
  13056. WOLFSSL_METHOD* wolfTLSv1_3_method(void)
  13057. {
  13058. return wolfTLSv1_3_method_ex(NULL);
  13059. }
  13060. WOLFSSL_METHOD* wolfTLSv1_3_method_ex(void* heap)
  13061. {
  13062. WOLFSSL_METHOD* m;
  13063. WOLFSSL_ENTER("TLSv1_3_method");
  13064. #ifndef NO_WOLFSSL_CLIENT
  13065. m = wolfTLSv1_3_client_method_ex(heap);
  13066. #else
  13067. m = wolfTLSv1_3_server_method_ex(heap);
  13068. #endif
  13069. if (m != NULL) {
  13070. m->side = WOLFSSL_NEITHER_END;
  13071. }
  13072. return m;
  13073. }
  13074. #endif /* WOLFSSL_TLS13 */
  13075. #ifdef WOLFSSL_DTLS
  13076. WOLFSSL_METHOD* wolfDTLS_method(void)
  13077. {
  13078. return wolfDTLS_method_ex(NULL);
  13079. }
  13080. WOLFSSL_METHOD* wolfDTLS_method_ex(void* heap)
  13081. {
  13082. WOLFSSL_METHOD* m;
  13083. WOLFSSL_ENTER("DTLS_method_ex");
  13084. #ifndef NO_WOLFSSL_CLIENT
  13085. m = wolfDTLS_client_method_ex(heap);
  13086. #else
  13087. m = wolfDTLS_server_method_ex(heap);
  13088. #endif
  13089. if (m != NULL) {
  13090. m->side = WOLFSSL_NEITHER_END;
  13091. }
  13092. return m;
  13093. }
  13094. #ifndef NO_OLD_TLS
  13095. WOLFSSL_METHOD* wolfDTLSv1_method(void)
  13096. {
  13097. return wolfDTLSv1_method_ex(NULL);
  13098. }
  13099. WOLFSSL_METHOD* wolfDTLSv1_method_ex(void* heap)
  13100. {
  13101. WOLFSSL_METHOD* m;
  13102. WOLFSSL_ENTER("DTLSv1_method_ex");
  13103. #ifndef NO_WOLFSSL_CLIENT
  13104. m = wolfDTLSv1_client_method_ex(heap);
  13105. #else
  13106. m = wolfDTLSv1_server_method_ex(heap);
  13107. #endif
  13108. if (m != NULL) {
  13109. m->side = WOLFSSL_NEITHER_END;
  13110. }
  13111. return m;
  13112. }
  13113. #endif /* !NO_OLD_TLS */
  13114. #ifndef WOLFSSL_NO_TLS12
  13115. WOLFSSL_METHOD* wolfDTLSv1_2_method(void)
  13116. {
  13117. return wolfDTLSv1_2_method_ex(NULL);
  13118. }
  13119. WOLFSSL_METHOD* wolfDTLSv1_2_method_ex(void* heap)
  13120. {
  13121. WOLFSSL_METHOD* m;
  13122. WOLFSSL_ENTER("DTLSv1_2_method");
  13123. #ifndef NO_WOLFSSL_CLIENT
  13124. m = wolfDTLSv1_2_client_method_ex(heap);
  13125. #else
  13126. m = wolfDTLSv1_2_server_method_ex(heap);
  13127. #endif
  13128. if (m != NULL) {
  13129. m->side = WOLFSSL_NEITHER_END;
  13130. }
  13131. return m;
  13132. }
  13133. #endif /* !WOLFSSL_NO_TLS12 */
  13134. #endif /* WOLFSSL_DTLS */
  13135. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  13136. #ifndef NO_WOLFSSL_SERVER
  13137. WOLFSSL_METHOD* wolfTLS_server_method(void)
  13138. {
  13139. return wolfTLS_server_method_ex(NULL);
  13140. }
  13141. WOLFSSL_METHOD* wolfTLS_server_method_ex(void* heap)
  13142. {
  13143. WOLFSSL_METHOD* method =
  13144. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  13145. heap, DYNAMIC_TYPE_METHOD);
  13146. (void)heap;
  13147. WOLFSSL_ENTER("TLS_server_method_ex");
  13148. if (method) {
  13149. #if defined(WOLFSSL_TLS13)
  13150. InitSSL_Method(method, MakeTLSv1_3());
  13151. #elif !defined(WOLFSSL_NO_TLS12)
  13152. InitSSL_Method(method, MakeTLSv1_2());
  13153. #elif !defined(NO_OLD_TLS)
  13154. InitSSL_Method(method, MakeTLSv1_1());
  13155. #elif defined(WOLFSSL_ALLOW_TLSV10)
  13156. InitSSL_Method(method, MakeTLSv1());
  13157. #else
  13158. #error No TLS version enabled!
  13159. #endif
  13160. method->downgrade = 1;
  13161. method->side = WOLFSSL_SERVER_END;
  13162. }
  13163. return method;
  13164. }
  13165. #ifndef NO_OLD_TLS
  13166. #ifdef WOLFSSL_ALLOW_TLSV10
  13167. WOLFSSL_METHOD* wolfTLSv1_server_method(void)
  13168. {
  13169. return wolfTLSv1_server_method_ex(NULL);
  13170. }
  13171. WOLFSSL_METHOD* wolfTLSv1_server_method_ex(void* heap)
  13172. {
  13173. WOLFSSL_METHOD* method =
  13174. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  13175. heap, DYNAMIC_TYPE_METHOD);
  13176. (void)heap;
  13177. WOLFSSL_ENTER("TLSv1_server_method_ex");
  13178. if (method) {
  13179. InitSSL_Method(method, MakeTLSv1());
  13180. method->side = WOLFSSL_SERVER_END;
  13181. }
  13182. return method;
  13183. }
  13184. #endif /* WOLFSSL_ALLOW_TLSV10 */
  13185. WOLFSSL_METHOD* wolfTLSv1_1_server_method(void)
  13186. {
  13187. return wolfTLSv1_1_server_method_ex(NULL);
  13188. }
  13189. WOLFSSL_METHOD* wolfTLSv1_1_server_method_ex(void* heap)
  13190. {
  13191. WOLFSSL_METHOD* method =
  13192. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  13193. heap, DYNAMIC_TYPE_METHOD);
  13194. (void)heap;
  13195. WOLFSSL_ENTER("TLSv1_1_server_method_ex");
  13196. if (method) {
  13197. InitSSL_Method(method, MakeTLSv1_1());
  13198. method->side = WOLFSSL_SERVER_END;
  13199. }
  13200. return method;
  13201. }
  13202. #endif /* !NO_OLD_TLS */
  13203. #ifndef WOLFSSL_NO_TLS12
  13204. WOLFSSL_ABI
  13205. WOLFSSL_METHOD* wolfTLSv1_2_server_method(void)
  13206. {
  13207. return wolfTLSv1_2_server_method_ex(NULL);
  13208. }
  13209. WOLFSSL_METHOD* wolfTLSv1_2_server_method_ex(void* heap)
  13210. {
  13211. WOLFSSL_METHOD* method =
  13212. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  13213. heap, DYNAMIC_TYPE_METHOD);
  13214. (void)heap;
  13215. WOLFSSL_ENTER("TLSv1_2_server_method_ex");
  13216. if (method) {
  13217. InitSSL_Method(method, MakeTLSv1_2());
  13218. method->side = WOLFSSL_SERVER_END;
  13219. }
  13220. return method;
  13221. }
  13222. #endif /* !WOLFSSL_NO_TLS12 */
  13223. #ifdef WOLFSSL_TLS13
  13224. /* The TLS v1.3 server method data.
  13225. *
  13226. * returns the method data for a TLS v1.3 server.
  13227. */
  13228. WOLFSSL_ABI
  13229. WOLFSSL_METHOD* wolfTLSv1_3_server_method(void)
  13230. {
  13231. return wolfTLSv1_3_server_method_ex(NULL);
  13232. }
  13233. /* The TLS v1.3 server method data.
  13234. *
  13235. * heap The heap used for allocation.
  13236. * returns the method data for a TLS v1.3 server.
  13237. */
  13238. WOLFSSL_METHOD* wolfTLSv1_3_server_method_ex(void* heap)
  13239. {
  13240. WOLFSSL_METHOD* method =
  13241. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  13242. heap, DYNAMIC_TYPE_METHOD);
  13243. (void)heap;
  13244. WOLFSSL_ENTER("TLSv1_3_server_method_ex");
  13245. if (method) {
  13246. InitSSL_Method(method, MakeTLSv1_3());
  13247. method->side = WOLFSSL_SERVER_END;
  13248. }
  13249. return method;
  13250. }
  13251. #endif /* WOLFSSL_TLS13 */
  13252. #ifdef WOLFSSL_DTLS
  13253. WOLFSSL_METHOD* wolfDTLS_server_method(void)
  13254. {
  13255. return wolfDTLS_server_method_ex(NULL);
  13256. }
  13257. WOLFSSL_METHOD* wolfDTLS_server_method_ex(void* heap)
  13258. {
  13259. WOLFSSL_METHOD* method =
  13260. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  13261. heap, DYNAMIC_TYPE_METHOD);
  13262. (void)heap;
  13263. WOLFSSL_ENTER("DTLS_server_method_ex");
  13264. if (method) {
  13265. #if defined(WOLFSSL_DTLS13)
  13266. InitSSL_Method(method, MakeDTLSv1_3());
  13267. #elif !defined(WOLFSSL_NO_TLS12)
  13268. InitSSL_Method(method, MakeDTLSv1_2());
  13269. #elif !defined(NO_OLD_TLS)
  13270. InitSSL_Method(method, MakeDTLSv1());
  13271. #else
  13272. #error No DTLS version enabled!
  13273. #endif
  13274. method->downgrade = 1;
  13275. method->side = WOLFSSL_SERVER_END;
  13276. }
  13277. return method;
  13278. }
  13279. #ifndef NO_OLD_TLS
  13280. WOLFSSL_METHOD* wolfDTLSv1_server_method(void)
  13281. {
  13282. return wolfDTLSv1_server_method_ex(NULL);
  13283. }
  13284. WOLFSSL_METHOD* wolfDTLSv1_server_method_ex(void* heap)
  13285. {
  13286. WOLFSSL_METHOD* method =
  13287. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  13288. heap, DYNAMIC_TYPE_METHOD);
  13289. (void)heap;
  13290. WOLFSSL_ENTER("DTLSv1_server_method_ex");
  13291. if (method) {
  13292. InitSSL_Method(method, MakeDTLSv1());
  13293. method->side = WOLFSSL_SERVER_END;
  13294. }
  13295. return method;
  13296. }
  13297. #endif /* !NO_OLD_TLS */
  13298. #ifndef WOLFSSL_NO_TLS12
  13299. WOLFSSL_METHOD* wolfDTLSv1_2_server_method(void)
  13300. {
  13301. return wolfDTLSv1_2_server_method_ex(NULL);
  13302. }
  13303. WOLFSSL_METHOD* wolfDTLSv1_2_server_method_ex(void* heap)
  13304. {
  13305. WOLFSSL_METHOD* method =
  13306. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  13307. heap, DYNAMIC_TYPE_METHOD);
  13308. WOLFSSL_ENTER("DTLSv1_2_server_method_ex");
  13309. (void)heap;
  13310. if (method) {
  13311. InitSSL_Method(method, MakeDTLSv1_2());
  13312. method->side = WOLFSSL_SERVER_END;
  13313. }
  13314. (void)heap;
  13315. return method;
  13316. }
  13317. #endif /* !WOLFSSL_NO_TLS12 */
  13318. #endif /* WOLFSSL_DTLS */
  13319. #endif /* NO_WOLFSSL_SERVER */
  13320. #endif /* NO_TLS */
  13321. #endif /* WOLFCRYPT_ONLY */