tls.c 431 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957
  1. /* tls.c
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifndef WOLFCRYPT_ONLY
  26. #include <wolfssl/ssl.h>
  27. #include <wolfssl/internal.h>
  28. #include <wolfssl/error-ssl.h>
  29. #include <wolfssl/wolfcrypt/hash.h>
  30. #include <wolfssl/wolfcrypt/hmac.h>
  31. #include <wolfssl/wolfcrypt/kdf.h>
  32. #ifdef NO_INLINE
  33. #include <wolfssl/wolfcrypt/misc.h>
  34. #else
  35. #define WOLFSSL_MISC_INCLUDED
  36. #include <wolfcrypt/src/misc.c>
  37. #endif
  38. #ifdef HAVE_CURVE25519
  39. #include <wolfssl/wolfcrypt/curve25519.h>
  40. #endif
  41. #ifdef HAVE_CURVE448
  42. #include <wolfssl/wolfcrypt/curve448.h>
  43. #endif
  44. #ifdef HAVE_PQC
  45. #ifdef WOLFSSL_HAVE_KYBER
  46. #include <wolfssl/wolfcrypt/kyber.h>
  47. #ifdef WOLFSSL_WC_KYBER
  48. #include <wolfssl/wolfcrypt/wc_kyber.h>
  49. #elif defined(HAVE_LIBOQS)
  50. #include <oqs/kem.h>
  51. #include <wolfssl/wolfcrypt/ext_kyber.h>
  52. #elif defined(HAVE_PQM4)
  53. #include "api_kyber.h"
  54. #define PQM4_PUBLIC_KEY_LENGTH CRYPTO_PUBLICKEYBYTES
  55. #define PQM4_PRIVATE_KEY_LENGTH CRYPTO_SECRETKEYBYTES
  56. #define PQM4_SHARED_SECRET_LENGTH CRYPTO_BYTES
  57. #define PQM4_CIPHERTEXT_LENGTH CRYPTO_CIPHERTEXTBYTES
  58. #include <wolfssl/wolfcrypt/ext_kyber.h>
  59. #endif
  60. #endif
  61. #endif
  62. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  63. #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
  64. #endif
  65. #ifndef NO_TLS
  66. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  67. static int TLSX_KeyShare_IsSupported(int namedGroup);
  68. static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap);
  69. #endif
  70. #ifdef HAVE_SUPPORTED_CURVES
  71. static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions);
  72. #endif
  73. /* Digest enable checks */
  74. #ifdef NO_OLD_TLS /* TLS 1.2 only */
  75. #if defined(NO_SHA256) && !defined(WOLFSSL_SHA384) && \
  76. !defined(WOLFSSL_SHA512)
  77. #error Must have SHA256, SHA384 or SHA512 enabled for TLS 1.2
  78. #endif
  79. #else /* TLS 1.1 or older */
  80. #if defined(NO_MD5) && defined(NO_SHA)
  81. #error Must have SHA1 and MD5 enabled for old TLS
  82. #endif
  83. #endif
  84. #ifdef WOLFSSL_TLS13
  85. #if !defined(NO_DH) && \
  86. !defined(HAVE_FFDHE_2048) && !defined(HAVE_FFDHE_3072) && \
  87. !defined(HAVE_FFDHE_4096) && !defined(HAVE_FFDHE_6144) && \
  88. !defined(HAVE_FFDHE_8192)
  89. #error Please configure your TLS 1.3 DH key size using either: HAVE_FFDHE_2048, HAVE_FFDHE_3072, HAVE_FFDHE_4096, HAVE_FFDHE_6144 or HAVE_FFDHE_8192
  90. #endif
  91. #if !defined(NO_RSA) && !defined(WC_RSA_PSS)
  92. #error The build option WC_RSA_PSS is required for TLS 1.3 with RSA
  93. #endif
  94. #ifndef HAVE_TLS_EXTENSIONS
  95. #ifndef _MSC_VER
  96. #error "The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3"
  97. #else
  98. #pragma message("Error: The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3")
  99. #endif
  100. #endif
  101. #endif
  102. /* Warn if secrets logging is enabled */
  103. #if defined(SHOW_SECRETS) || defined(WOLFSSL_SSLKEYLOGFILE)
  104. #ifndef _MSC_VER
  105. #warning The SHOW_SECRETS and WOLFSSL_SSLKEYLOGFILE options should only be used for debugging and never in a production environment
  106. #else
  107. #pragma message("Warning: The SHOW_SECRETS and WOLFSSL_SSLKEYLOGFILE options should only be used for debugging and never in a production environment")
  108. #endif
  109. #endif
  110. /* Optional Pre-Master-Secret logging for Wireshark */
  111. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
  112. #ifndef WOLFSSL_SSLKEYLOGFILE_OUTPUT
  113. #define WOLFSSL_SSLKEYLOGFILE_OUTPUT "sslkeylog.log"
  114. #endif
  115. #endif
  116. #ifndef WOLFSSL_NO_TLS12
  117. #ifdef WOLFSSL_SHA384
  118. #define HSHASH_SZ WC_SHA384_DIGEST_SIZE
  119. #else
  120. #define HSHASH_SZ FINISHED_SZ
  121. #endif
  122. int BuildTlsHandshakeHash(WOLFSSL* ssl, byte* hash, word32* hashLen)
  123. {
  124. int ret = 0;
  125. word32 hashSz = FINISHED_SZ;
  126. if (ssl == NULL || hash == NULL || hashLen == NULL || *hashLen < HSHASH_SZ)
  127. return BAD_FUNC_ARG;
  128. /* for constant timing perform these even if error */
  129. #ifndef NO_OLD_TLS
  130. ret |= wc_Md5GetHash(&ssl->hsHashes->hashMd5, hash);
  131. ret |= wc_ShaGetHash(&ssl->hsHashes->hashSha, &hash[WC_MD5_DIGEST_SIZE]);
  132. #endif
  133. if (IsAtLeastTLSv1_2(ssl)) {
  134. #ifndef NO_SHA256
  135. if (ssl->specs.mac_algorithm <= sha256_mac ||
  136. ssl->specs.mac_algorithm == blake2b_mac) {
  137. ret |= wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash);
  138. hashSz = WC_SHA256_DIGEST_SIZE;
  139. }
  140. #endif
  141. #ifdef WOLFSSL_SHA384
  142. if (ssl->specs.mac_algorithm == sha384_mac) {
  143. ret |= wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash);
  144. hashSz = WC_SHA384_DIGEST_SIZE;
  145. }
  146. #endif
  147. }
  148. *hashLen = hashSz;
  149. #ifdef WOLFSSL_CHECK_MEM_ZERO
  150. wc_MemZero_Add("TLS handshake hash", hash, hashSz);
  151. #endif
  152. if (ret != 0) {
  153. ret = BUILD_MSG_ERROR;
  154. WOLFSSL_ERROR_VERBOSE(ret);
  155. }
  156. return ret;
  157. }
  158. int BuildTlsFinished(WOLFSSL* ssl, Hashes* hashes, const byte* sender)
  159. {
  160. int ret;
  161. const byte* side = NULL;
  162. word32 hashSz = HSHASH_SZ;
  163. #if !defined(WOLFSSL_ASYNC_CRYPT) || defined(WC_ASYNC_NO_HASH)
  164. byte handshake_hash[HSHASH_SZ];
  165. #else
  166. WC_DECLARE_VAR(handshake_hash, byte, HSHASH_SZ, ssl->heap);
  167. if (handshake_hash == NULL)
  168. return MEMORY_E;
  169. #endif
  170. ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
  171. if (ret == 0) {
  172. if (XSTRNCMP((const char*)sender, (const char*)kTlsClientStr,
  173. SIZEOF_SENDER) == 0) {
  174. side = kTlsClientFinStr;
  175. }
  176. else if (XSTRNCMP((const char*)sender, (const char*)kTlsServerStr,
  177. SIZEOF_SENDER) == 0) {
  178. side = kTlsServerFinStr;
  179. }
  180. else {
  181. ret = BAD_FUNC_ARG;
  182. WOLFSSL_MSG("Unexpected sender value");
  183. }
  184. }
  185. if (ret == 0) {
  186. #ifdef WOLFSSL_HAVE_PRF
  187. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  188. if (ssl->ctx->TlsFinishedCb) {
  189. void* ctx = wolfSSL_GetTlsFinishedCtx(ssl);
  190. ret = ssl->ctx->TlsFinishedCb(ssl, side, handshake_hash,
  191. (byte*)hashes, ctx);
  192. }
  193. if (!ssl->ctx->TlsFinishedCb || ret == PROTOCOLCB_UNAVAILABLE)
  194. #endif
  195. {
  196. PRIVATE_KEY_UNLOCK();
  197. ret = wc_PRF_TLS((byte*)hashes, TLS_FINISHED_SZ,
  198. ssl->arrays->masterSecret,
  199. SECRET_LEN, side, FINISHED_LABEL_SZ, handshake_hash, hashSz,
  200. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  201. ssl->heap, ssl->devId);
  202. PRIVATE_KEY_LOCK();
  203. }
  204. ForceZero(handshake_hash, hashSz);
  205. #else
  206. /* Pseudo random function must be enabled in the configuration. */
  207. ret = PRF_MISSING;
  208. WOLFSSL_ERROR_VERBOSE(ret);
  209. WOLFSSL_MSG("Pseudo-random function is not enabled");
  210. (void)side;
  211. (void)hashes;
  212. #endif
  213. }
  214. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  215. WC_FREE_VAR(handshake_hash, ssl->heap);
  216. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  217. wc_MemZero_Check(handshake_hash, HSHASH_SZ);
  218. #endif
  219. return ret;
  220. }
  221. #endif /* !WOLFSSL_NO_TLS12 */
  222. #ifndef NO_OLD_TLS
  223. #ifdef WOLFSSL_ALLOW_TLSV10
  224. ProtocolVersion MakeTLSv1(void)
  225. {
  226. ProtocolVersion pv;
  227. pv.major = SSLv3_MAJOR;
  228. pv.minor = TLSv1_MINOR;
  229. return pv;
  230. }
  231. #endif /* WOLFSSL_ALLOW_TLSV10 */
  232. ProtocolVersion MakeTLSv1_1(void)
  233. {
  234. ProtocolVersion pv;
  235. pv.major = SSLv3_MAJOR;
  236. pv.minor = TLSv1_1_MINOR;
  237. return pv;
  238. }
  239. #endif /* !NO_OLD_TLS */
  240. #ifndef WOLFSSL_NO_TLS12
  241. ProtocolVersion MakeTLSv1_2(void)
  242. {
  243. ProtocolVersion pv;
  244. pv.major = SSLv3_MAJOR;
  245. pv.minor = TLSv1_2_MINOR;
  246. return pv;
  247. }
  248. #endif /* !WOLFSSL_NO_TLS12 */
  249. #ifdef WOLFSSL_TLS13
  250. /* The TLS v1.3 protocol version.
  251. *
  252. * returns the protocol version data for TLS v1.3.
  253. */
  254. ProtocolVersion MakeTLSv1_3(void)
  255. {
  256. ProtocolVersion pv;
  257. pv.major = SSLv3_MAJOR;
  258. pv.minor = TLSv1_3_MINOR;
  259. return pv;
  260. }
  261. #endif
  262. #ifndef WOLFSSL_NO_TLS12
  263. #ifdef HAVE_EXTENDED_MASTER
  264. static const byte ext_master_label[EXT_MASTER_LABEL_SZ + 1] =
  265. "extended master secret";
  266. #endif
  267. static const byte master_label[MASTER_LABEL_SZ + 1] = "master secret";
  268. static const byte key_label [KEY_LABEL_SZ + 1] = "key expansion";
  269. static int _DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
  270. const byte* ms, word32 msLen,
  271. const byte* sr, const byte* cr,
  272. int tls1_2, int hash_type,
  273. void* heap, int devId)
  274. {
  275. int ret;
  276. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  277. WC_DECLARE_VAR(seed, byte, SEED_LEN, heap);
  278. if (seed == NULL)
  279. return MEMORY_E;
  280. #else
  281. byte seed[SEED_LEN];
  282. #endif
  283. XMEMCPY(seed, sr, RAN_LEN);
  284. XMEMCPY(seed + RAN_LEN, cr, RAN_LEN);
  285. #ifdef WOLFSSL_HAVE_PRF
  286. PRIVATE_KEY_UNLOCK();
  287. ret = wc_PRF_TLS(key_dig, key_dig_len, ms, msLen, key_label, KEY_LABEL_SZ,
  288. seed, SEED_LEN, tls1_2, hash_type, heap, devId);
  289. PRIVATE_KEY_LOCK();
  290. #else
  291. /* Pseudo random function must be enabled in the configuration. */
  292. ret = PRF_MISSING;
  293. WOLFSSL_ERROR_VERBOSE(ret);
  294. WOLFSSL_MSG("Pseudo-random function is not enabled");
  295. (void)key_dig;
  296. (void)key_dig_len;
  297. (void)ms;
  298. (void)msLen;
  299. (void)tls1_2;
  300. (void)hash_type;
  301. (void)heap;
  302. (void)devId;
  303. (void)key_label;
  304. (void)master_label;
  305. #ifdef HAVE_EXTENDED_MASTER
  306. (void)ext_master_label;
  307. #endif
  308. #endif
  309. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  310. WC_FREE_VAR(seed, heap);
  311. #endif
  312. return ret;
  313. }
  314. /* External facing wrapper so user can call as well, 0 on success */
  315. int wolfSSL_DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
  316. const byte* ms, word32 msLen,
  317. const byte* sr, const byte* cr,
  318. int tls1_2, int hash_type)
  319. {
  320. return _DeriveTlsKeys(key_dig, key_dig_len, ms, msLen, sr, cr, tls1_2,
  321. hash_type, NULL, INVALID_DEVID);
  322. }
  323. int DeriveTlsKeys(WOLFSSL* ssl)
  324. {
  325. int ret;
  326. int key_dig_len = 2 * ssl->specs.hash_size +
  327. 2 * ssl->specs.key_size +
  328. 2 * ssl->specs.iv_size;
  329. #ifdef WOLFSSL_SMALL_STACK
  330. byte* key_dig;
  331. #else
  332. byte key_dig[MAX_PRF_DIG];
  333. #endif
  334. #ifdef WOLFSSL_SMALL_STACK
  335. key_dig = (byte*)XMALLOC(MAX_PRF_DIG, ssl->heap, DYNAMIC_TYPE_DIGEST);
  336. if (key_dig == NULL) {
  337. return MEMORY_E;
  338. }
  339. #endif
  340. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  341. ret = PROTOCOLCB_UNAVAILABLE;
  342. if (ssl->ctx->GenSessionKeyCb) {
  343. void* ctx = wolfSSL_GetGenSessionKeyCtx(ssl);
  344. ret = ssl->ctx->GenSessionKeyCb(ssl, ctx);
  345. }
  346. if (!ssl->ctx->GenSessionKeyCb || ret == PROTOCOLCB_UNAVAILABLE)
  347. #endif
  348. ret = _DeriveTlsKeys(key_dig, key_dig_len,
  349. ssl->arrays->masterSecret, SECRET_LEN,
  350. ssl->arrays->serverRandom, ssl->arrays->clientRandom,
  351. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  352. ssl->heap, ssl->devId);
  353. if (ret == 0)
  354. ret = StoreKeys(ssl, key_dig, PROVISION_CLIENT_SERVER);
  355. #ifdef WOLFSSL_SMALL_STACK
  356. XFREE(key_dig, ssl->heap, DYNAMIC_TYPE_DIGEST);
  357. #endif
  358. return ret;
  359. }
  360. static int _MakeTlsMasterSecret(byte* ms, word32 msLen,
  361. const byte* pms, word32 pmsLen,
  362. const byte* cr, const byte* sr,
  363. int tls1_2, int hash_type,
  364. void* heap, int devId)
  365. {
  366. int ret;
  367. #if !defined(WOLFSSL_ASYNC_CRYPT) || defined(WC_ASYNC_NO_HASH)
  368. byte seed[SEED_LEN];
  369. #else
  370. WC_DECLARE_VAR(seed, byte, SEED_LEN, heap);
  371. if (seed == NULL)
  372. return MEMORY_E;
  373. #endif
  374. XMEMCPY(seed, cr, RAN_LEN);
  375. XMEMCPY(seed + RAN_LEN, sr, RAN_LEN);
  376. #ifdef WOLFSSL_HAVE_PRF
  377. PRIVATE_KEY_UNLOCK();
  378. ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, master_label, MASTER_LABEL_SZ,
  379. seed, SEED_LEN, tls1_2, hash_type, heap, devId);
  380. PRIVATE_KEY_LOCK();
  381. #else
  382. /* Pseudo random function must be enabled in the configuration. */
  383. ret = PRF_MISSING;
  384. WOLFSSL_MSG("Pseudo-random function is not enabled");
  385. (void)ms;
  386. (void)msLen;
  387. (void)pms;
  388. (void)pmsLen;
  389. (void)tls1_2;
  390. (void)hash_type;
  391. (void)heap;
  392. (void)devId;
  393. #endif
  394. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  395. WC_FREE_VAR(seed, heap);
  396. #endif
  397. return ret;
  398. }
  399. /* External facing wrapper so user can call as well, 0 on success */
  400. int wolfSSL_MakeTlsMasterSecret(byte* ms, word32 msLen,
  401. const byte* pms, word32 pmsLen,
  402. const byte* cr, const byte* sr,
  403. int tls1_2, int hash_type)
  404. {
  405. return _MakeTlsMasterSecret(ms, msLen, pms, pmsLen, cr, sr, tls1_2,
  406. hash_type, NULL, INVALID_DEVID);
  407. }
  408. #ifdef HAVE_EXTENDED_MASTER
  409. static int _MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
  410. const byte* pms, word32 pmsLen,
  411. const byte* sHash, word32 sHashLen,
  412. int tls1_2, int hash_type,
  413. void* heap, int devId)
  414. {
  415. int ret;
  416. #ifdef WOLFSSL_HAVE_PRF
  417. PRIVATE_KEY_UNLOCK();
  418. ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, ext_master_label, EXT_MASTER_LABEL_SZ,
  419. sHash, sHashLen, tls1_2, hash_type, heap, devId);
  420. PRIVATE_KEY_LOCK();
  421. #else
  422. /* Pseudo random function must be enabled in the configuration. */
  423. ret = PRF_MISSING;
  424. WOLFSSL_MSG("Pseudo-random function is not enabled");
  425. (void)ms;
  426. (void)msLen;
  427. (void)pms;
  428. (void)pmsLen;
  429. (void)sHash;
  430. (void)sHashLen;
  431. (void)tls1_2;
  432. (void)hash_type;
  433. (void)heap;
  434. (void)devId;
  435. #endif
  436. return ret;
  437. }
  438. /* External facing wrapper so user can call as well, 0 on success */
  439. int wolfSSL_MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
  440. const byte* pms, word32 pmsLen,
  441. const byte* sHash, word32 sHashLen,
  442. int tls1_2, int hash_type)
  443. {
  444. return _MakeTlsExtendedMasterSecret(ms, msLen, pms, pmsLen, sHash, sHashLen,
  445. tls1_2, hash_type, NULL, INVALID_DEVID);
  446. }
  447. #endif /* HAVE_EXTENDED_MASTER */
  448. int MakeTlsMasterSecret(WOLFSSL* ssl)
  449. {
  450. int ret;
  451. #ifdef HAVE_EXTENDED_MASTER
  452. if (ssl->options.haveEMS) {
  453. word32 hashSz = HSHASH_SZ;
  454. #ifdef WOLFSSL_SMALL_STACK
  455. byte* handshake_hash = (byte*)XMALLOC(HSHASH_SZ, ssl->heap,
  456. DYNAMIC_TYPE_DIGEST);
  457. if (handshake_hash == NULL)
  458. return MEMORY_E;
  459. #else
  460. byte handshake_hash[HSHASH_SZ];
  461. #endif
  462. ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
  463. if (ret == 0) {
  464. ret = _MakeTlsExtendedMasterSecret(
  465. ssl->arrays->masterSecret, SECRET_LEN,
  466. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
  467. handshake_hash, hashSz,
  468. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  469. ssl->heap, ssl->devId);
  470. ForceZero(handshake_hash, hashSz);
  471. }
  472. #ifdef WOLFSSL_SMALL_STACK
  473. XFREE(handshake_hash, ssl->heap, DYNAMIC_TYPE_DIGEST);
  474. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  475. wc_MemZero_Check(handshake_hash, HSHASH_SZ);
  476. #endif
  477. }
  478. else
  479. #endif /* HAVE_EXTENDED_MASTER */
  480. {
  481. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  482. ret = PROTOCOLCB_UNAVAILABLE;
  483. if (ssl->ctx->GenMasterCb) {
  484. void* ctx = wolfSSL_GetGenMasterSecretCtx(ssl);
  485. ret = ssl->ctx->GenMasterCb(ssl, ctx);
  486. }
  487. if (!ssl->ctx->GenMasterCb || ret == PROTOCOLCB_UNAVAILABLE)
  488. #endif
  489. ret = _MakeTlsMasterSecret(ssl->arrays->masterSecret, SECRET_LEN,
  490. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
  491. ssl->arrays->clientRandom, ssl->arrays->serverRandom,
  492. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  493. ssl->heap, ssl->devId);
  494. }
  495. if (ret == 0) {
  496. #ifdef SHOW_SECRETS
  497. /* Wireshark Pre-Master-Secret Format:
  498. * CLIENT_RANDOM <clientrandom> <mastersecret>
  499. */
  500. const char* CLIENT_RANDOM_LABEL = "CLIENT_RANDOM";
  501. int i, pmsPos = 0;
  502. char pmsBuf[13 + 1 + 64 + 1 + 96 + 1 + 1];
  503. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%s ",
  504. CLIENT_RANDOM_LABEL);
  505. pmsPos += XSTRLEN(CLIENT_RANDOM_LABEL) + 1;
  506. for (i = 0; i < RAN_LEN; i++) {
  507. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  508. ssl->arrays->clientRandom[i]);
  509. pmsPos += 2;
  510. }
  511. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, " ");
  512. pmsPos += 1;
  513. for (i = 0; i < SECRET_LEN; i++) {
  514. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  515. ssl->arrays->masterSecret[i]);
  516. pmsPos += 2;
  517. }
  518. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "\n");
  519. pmsPos += 1;
  520. /* print master secret */
  521. puts(pmsBuf);
  522. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
  523. {
  524. FILE* f = XFOPEN(WOLFSSL_SSLKEYLOGFILE_OUTPUT, "a");
  525. if (f != XBADFILE) {
  526. XFWRITE(pmsBuf, 1, pmsPos, f);
  527. XFCLOSE(f);
  528. }
  529. }
  530. #endif
  531. #endif /* SHOW_SECRETS */
  532. ret = DeriveTlsKeys(ssl);
  533. }
  534. return ret;
  535. }
  536. /* Used by EAP-TLS and EAP-TTLS to derive keying material from
  537. * the master_secret. */
  538. int wolfSSL_make_eap_keys(WOLFSSL* ssl, void* msk, unsigned int len,
  539. const char* label)
  540. {
  541. int ret;
  542. #ifdef WOLFSSL_SMALL_STACK
  543. byte* seed;
  544. #else
  545. byte seed[SEED_LEN];
  546. #endif
  547. #ifdef WOLFSSL_SMALL_STACK
  548. seed = (byte*)XMALLOC(SEED_LEN, ssl->heap, DYNAMIC_TYPE_SEED);
  549. if (seed == NULL)
  550. return MEMORY_E;
  551. #endif
  552. /*
  553. * As per RFC-5281, the order of the client and server randoms is reversed
  554. * from that used by the TLS protocol to derive keys.
  555. */
  556. XMEMCPY(seed, ssl->arrays->clientRandom, RAN_LEN);
  557. XMEMCPY(seed + RAN_LEN, ssl->arrays->serverRandom, RAN_LEN);
  558. #ifdef WOLFSSL_HAVE_PRF
  559. PRIVATE_KEY_UNLOCK();
  560. ret = wc_PRF_TLS((byte*)msk, len, ssl->arrays->masterSecret, SECRET_LEN,
  561. (const byte *)label, (word32)XSTRLEN(label), seed, SEED_LEN,
  562. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  563. ssl->heap, ssl->devId);
  564. PRIVATE_KEY_LOCK();
  565. #else
  566. /* Pseudo random function must be enabled in the configuration. */
  567. ret = PRF_MISSING;
  568. WOLFSSL_MSG("Pseudo-random function is not enabled");
  569. (void)msk;
  570. (void)len;
  571. (void)label;
  572. #endif
  573. #ifdef WOLFSSL_SMALL_STACK
  574. XFREE(seed, ssl->heap, DYNAMIC_TYPE_SEED);
  575. #endif
  576. return ret;
  577. }
  578. /* return HMAC digest type in wolfSSL format */
  579. int wolfSSL_GetHmacType(WOLFSSL* ssl)
  580. {
  581. if (ssl == NULL)
  582. return BAD_FUNC_ARG;
  583. switch (ssl->specs.mac_algorithm) {
  584. #ifndef NO_MD5
  585. case md5_mac:
  586. {
  587. return WC_MD5;
  588. }
  589. #endif
  590. #ifndef NO_SHA256
  591. case sha256_mac:
  592. {
  593. return WC_SHA256;
  594. }
  595. #endif
  596. #ifdef WOLFSSL_SHA384
  597. case sha384_mac:
  598. {
  599. return WC_SHA384;
  600. }
  601. #endif
  602. #ifndef NO_SHA
  603. case sha_mac:
  604. {
  605. return WC_SHA;
  606. }
  607. #endif
  608. #ifdef HAVE_BLAKE2
  609. case blake2b_mac:
  610. {
  611. return BLAKE2B_ID;
  612. }
  613. #endif
  614. default:
  615. {
  616. return WOLFSSL_FATAL_ERROR;
  617. }
  618. }
  619. }
  620. int wolfSSL_SetTlsHmacInner(WOLFSSL* ssl, byte* inner, word32 sz, int content,
  621. int verify)
  622. {
  623. if (ssl == NULL || inner == NULL)
  624. return BAD_FUNC_ARG;
  625. XMEMSET(inner, 0, WOLFSSL_TLS_HMAC_INNER_SZ);
  626. WriteSEQ(ssl, verify, inner);
  627. inner[SEQ_SZ] = (byte)content;
  628. inner[SEQ_SZ + ENUM_LEN] = ssl->version.major;
  629. inner[SEQ_SZ + ENUM_LEN + ENUM_LEN] = ssl->version.minor;
  630. c16toa((word16)sz, inner + SEQ_SZ + ENUM_LEN + VERSION_SZ);
  631. return 0;
  632. }
  633. #ifndef WOLFSSL_AEAD_ONLY
  634. #if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
  635. !defined(HAVE_SELFTEST)
  636. /* Update the hash in the HMAC.
  637. *
  638. * hmac HMAC object.
  639. * data Data to be hashed.
  640. * sz Size of data to hash.
  641. * returns 0 on success, otherwise failure.
  642. */
  643. static int Hmac_HashUpdate(Hmac* hmac, const byte* data, word32 sz)
  644. {
  645. int ret = BAD_FUNC_ARG;
  646. switch (hmac->macType) {
  647. #ifndef NO_SHA
  648. case WC_SHA:
  649. ret = wc_ShaUpdate(&hmac->hash.sha, data, sz);
  650. break;
  651. #endif /* !NO_SHA */
  652. #ifndef NO_SHA256
  653. case WC_SHA256:
  654. ret = wc_Sha256Update(&hmac->hash.sha256, data, sz);
  655. break;
  656. #endif /* !NO_SHA256 */
  657. #ifdef WOLFSSL_SHA384
  658. case WC_SHA384:
  659. ret = wc_Sha384Update(&hmac->hash.sha384, data, sz);
  660. break;
  661. #endif /* WOLFSSL_SHA384 */
  662. #ifdef WOLFSSL_SHA512
  663. case WC_SHA512:
  664. ret = wc_Sha512Update(&hmac->hash.sha512, data, sz);
  665. break;
  666. #endif /* WOLFSSL_SHA512 */
  667. default:
  668. break;
  669. }
  670. return ret;
  671. }
  672. /* Finalize the hash but don't put the EOC, padding or length in.
  673. *
  674. * hmac HMAC object.
  675. * hash Hash result.
  676. * returns 0 on success, otherwise failure.
  677. */
  678. static int Hmac_HashFinalRaw(Hmac* hmac, unsigned char* hash)
  679. {
  680. int ret = BAD_FUNC_ARG;
  681. switch (hmac->macType) {
  682. #ifndef NO_SHA
  683. case WC_SHA:
  684. ret = wc_ShaFinalRaw(&hmac->hash.sha, hash);
  685. break;
  686. #endif /* !NO_SHA */
  687. #ifndef NO_SHA256
  688. case WC_SHA256:
  689. ret = wc_Sha256FinalRaw(&hmac->hash.sha256, hash);
  690. break;
  691. #endif /* !NO_SHA256 */
  692. #ifdef WOLFSSL_SHA384
  693. case WC_SHA384:
  694. ret = wc_Sha384FinalRaw(&hmac->hash.sha384, hash);
  695. break;
  696. #endif /* WOLFSSL_SHA384 */
  697. #ifdef WOLFSSL_SHA512
  698. case WC_SHA512:
  699. ret = wc_Sha512FinalRaw(&hmac->hash.sha512, hash);
  700. break;
  701. #endif /* WOLFSSL_SHA512 */
  702. default:
  703. break;
  704. }
  705. return ret;
  706. }
  707. /* Finalize the HMAC by performing outer hash.
  708. *
  709. * hmac HMAC object.
  710. * mac MAC result.
  711. * returns 0 on success, otherwise failure.
  712. */
  713. static int Hmac_OuterHash(Hmac* hmac, unsigned char* mac)
  714. {
  715. int ret = BAD_FUNC_ARG;
  716. wc_HashAlg hash;
  717. enum wc_HashType hashType = (enum wc_HashType)hmac->macType;
  718. int digestSz = wc_HashGetDigestSize(hashType);
  719. int blockSz = wc_HashGetBlockSize(hashType);
  720. if ((digestSz >= 0) && (blockSz >= 0)) {
  721. ret = wc_HashInit(&hash, hashType);
  722. }
  723. if (ret == 0) {
  724. ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->opad,
  725. blockSz);
  726. if (ret == 0)
  727. ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->innerHash,
  728. digestSz);
  729. if (ret == 0)
  730. ret = wc_HashFinal(&hash, hashType, mac);
  731. wc_HashFree(&hash, hashType);
  732. }
  733. return ret;
  734. }
  735. /* Calculate the HMAC of the header + message data.
  736. * Constant time implementation using wc_Sha*FinalRaw().
  737. *
  738. * hmac HMAC object.
  739. * digest MAC result.
  740. * in Message data.
  741. * sz Size of the message data.
  742. * header Constructed record header with length of handshake data.
  743. * returns 0 on success, otherwise failure.
  744. */
  745. static int Hmac_UpdateFinal_CT(Hmac* hmac, byte* digest, const byte* in,
  746. word32 sz, int macLen, byte* header)
  747. {
  748. byte lenBytes[8];
  749. int i, j;
  750. unsigned int k;
  751. int blockBits, blockMask;
  752. int lastBlockLen, extraLen, eocIndex;
  753. int blocks, safeBlocks, lenBlock, eocBlock;
  754. unsigned int maxLen;
  755. int blockSz, padSz;
  756. int ret;
  757. word32 realLen;
  758. byte extraBlock;
  759. switch (hmac->macType) {
  760. #ifndef NO_SHA
  761. case WC_SHA:
  762. blockSz = WC_SHA_BLOCK_SIZE;
  763. blockBits = 6;
  764. padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
  765. break;
  766. #endif /* !NO_SHA */
  767. #ifndef NO_SHA256
  768. case WC_SHA256:
  769. blockSz = WC_SHA256_BLOCK_SIZE;
  770. blockBits = 6;
  771. padSz = WC_SHA256_BLOCK_SIZE - WC_SHA256_PAD_SIZE + 1;
  772. break;
  773. #endif /* !NO_SHA256 */
  774. #ifdef WOLFSSL_SHA384
  775. case WC_SHA384:
  776. blockSz = WC_SHA384_BLOCK_SIZE;
  777. blockBits = 7;
  778. padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
  779. break;
  780. #endif /* WOLFSSL_SHA384 */
  781. #ifdef WOLFSSL_SHA512
  782. case WC_SHA512:
  783. blockSz = WC_SHA512_BLOCK_SIZE;
  784. blockBits = 7;
  785. padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
  786. break;
  787. #endif /* WOLFSSL_SHA512 */
  788. default:
  789. return BAD_FUNC_ARG;
  790. }
  791. blockMask = blockSz - 1;
  792. /* Size of data to HMAC if padding length byte is zero. */
  793. maxLen = WOLFSSL_TLS_HMAC_INNER_SZ + sz - 1 - macLen;
  794. /* Complete data (including padding) has block for EOC and/or length. */
  795. extraBlock = ctSetLTE((maxLen + padSz) & blockMask, padSz);
  796. /* Total number of blocks for data including padding. */
  797. blocks = ((maxLen + blockSz - 1) >> blockBits) + extraBlock;
  798. /* Up to last 6 blocks can be hashed safely. */
  799. safeBlocks = blocks - 6;
  800. /* Length of message data. */
  801. realLen = maxLen - in[sz - 1];
  802. /* Number of message bytes in last block. */
  803. lastBlockLen = realLen & blockMask;
  804. /* Number of padding bytes in last block. */
  805. extraLen = ((blockSz * 2 - padSz - lastBlockLen) & blockMask) + 1;
  806. /* Number of blocks to create for hash. */
  807. lenBlock = (realLen + extraLen) >> blockBits;
  808. /* Block containing EOC byte. */
  809. eocBlock = realLen >> blockBits;
  810. /* Index of EOC byte in block. */
  811. eocIndex = realLen & blockMask;
  812. /* Add length of hmac's ipad to total length. */
  813. realLen += blockSz;
  814. /* Length as bits - 8 bytes bigendian. */
  815. c32toa(realLen >> ((sizeof(word32) * 8) - 3), lenBytes);
  816. c32toa(realLen << 3, lenBytes + sizeof(word32));
  817. ret = Hmac_HashUpdate(hmac, (unsigned char*)hmac->ipad, blockSz);
  818. if (ret != 0)
  819. return ret;
  820. XMEMSET(hmac->innerHash, 0, macLen);
  821. if (safeBlocks > 0) {
  822. ret = Hmac_HashUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
  823. if (ret != 0)
  824. return ret;
  825. ret = Hmac_HashUpdate(hmac, in, safeBlocks * blockSz -
  826. WOLFSSL_TLS_HMAC_INNER_SZ);
  827. if (ret != 0)
  828. return ret;
  829. }
  830. else
  831. safeBlocks = 0;
  832. XMEMSET(digest, 0, macLen);
  833. k = safeBlocks * blockSz;
  834. for (i = safeBlocks; i < blocks; i++) {
  835. unsigned char hashBlock[WC_MAX_BLOCK_SIZE];
  836. unsigned char isEocBlock = ctMaskEq(i, eocBlock);
  837. unsigned char isOutBlock = ctMaskEq(i, lenBlock);
  838. for (j = 0; j < blockSz; j++) {
  839. unsigned char atEoc = ctMaskEq(j, eocIndex) & isEocBlock;
  840. unsigned char pastEoc = ctMaskGT(j, eocIndex) & isEocBlock;
  841. unsigned char b = 0;
  842. if (k < WOLFSSL_TLS_HMAC_INNER_SZ)
  843. b = header[k];
  844. else if (k < maxLen)
  845. b = in[k - WOLFSSL_TLS_HMAC_INNER_SZ];
  846. k++;
  847. b = ctMaskSel(atEoc, 0x80, b);
  848. b &= (unsigned char)~(word32)pastEoc;
  849. b &= ((unsigned char)~(word32)isOutBlock) | isEocBlock;
  850. if (j >= blockSz - 8) {
  851. b = ctMaskSel(isOutBlock, lenBytes[j - (blockSz - 8)], b);
  852. }
  853. hashBlock[j] = b;
  854. }
  855. ret = Hmac_HashUpdate(hmac, hashBlock, blockSz);
  856. if (ret != 0)
  857. return ret;
  858. ret = Hmac_HashFinalRaw(hmac, hashBlock);
  859. if (ret != 0)
  860. return ret;
  861. for (j = 0; j < macLen; j++)
  862. ((unsigned char*)hmac->innerHash)[j] |= hashBlock[j] & isOutBlock;
  863. }
  864. ret = Hmac_OuterHash(hmac, digest);
  865. return ret;
  866. }
  867. #endif
  868. #if defined(WOLFSSL_NO_HASH_RAW) || defined(HAVE_FIPS) || \
  869. defined(HAVE_SELFTEST) || defined(HAVE_BLAKE2)
  870. /* Calculate the HMAC of the header + message data.
  871. * Constant time implementation using normal hashing operations.
  872. * Update-Final need to be constant time.
  873. *
  874. * hmac HMAC object.
  875. * digest MAC result.
  876. * in Message data.
  877. * sz Size of the message data.
  878. * header Constructed record header with length of handshake data.
  879. * returns 0 on success, otherwise failure.
  880. */
  881. static int Hmac_UpdateFinal(Hmac* hmac, byte* digest, const byte* in,
  882. word32 sz, byte* header)
  883. {
  884. byte dummy[WC_MAX_BLOCK_SIZE] = {0};
  885. int ret;
  886. word32 msgSz, blockSz, macSz, padSz, maxSz, realSz;
  887. word32 currSz, offset = 0;
  888. int msgBlocks, blocks, blockBits;
  889. int i;
  890. switch (hmac->macType) {
  891. #ifndef NO_SHA
  892. case WC_SHA:
  893. blockSz = WC_SHA_BLOCK_SIZE;
  894. blockBits = 6;
  895. macSz = WC_SHA_DIGEST_SIZE;
  896. padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
  897. break;
  898. #endif /* !NO_SHA */
  899. #ifndef NO_SHA256
  900. case WC_SHA256:
  901. blockSz = WC_SHA256_BLOCK_SIZE;
  902. blockBits = 6;
  903. macSz = WC_SHA256_DIGEST_SIZE;
  904. padSz = WC_SHA256_BLOCK_SIZE - WC_SHA256_PAD_SIZE + 1;
  905. break;
  906. #endif /* !NO_SHA256 */
  907. #ifdef WOLFSSL_SHA384
  908. case WC_SHA384:
  909. blockSz = WC_SHA384_BLOCK_SIZE;
  910. blockBits = 7;
  911. macSz = WC_SHA384_DIGEST_SIZE;
  912. padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
  913. break;
  914. #endif /* WOLFSSL_SHA384 */
  915. #ifdef WOLFSSL_SHA512
  916. case WC_SHA512:
  917. blockSz = WC_SHA512_BLOCK_SIZE;
  918. blockBits = 7;
  919. macSz = WC_SHA512_DIGEST_SIZE;
  920. padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
  921. break;
  922. #endif /* WOLFSSL_SHA512 */
  923. #ifdef HAVE_BLAKE2
  924. case WC_HASH_TYPE_BLAKE2B:
  925. blockSz = BLAKE2B_BLOCKBYTES;
  926. blockBits = 7;
  927. macSz = BLAKE2B_256;
  928. padSz = 0;
  929. break;
  930. #endif /* HAVE_BLAKE2 */
  931. default:
  932. return BAD_FUNC_ARG;
  933. }
  934. msgSz = sz - (1 + in[sz - 1] + macSz);
  935. /* Make negative result 0 */
  936. msgSz &= ~(0 - (msgSz >> 31));
  937. realSz = WOLFSSL_TLS_HMAC_INNER_SZ + msgSz;
  938. maxSz = WOLFSSL_TLS_HMAC_INNER_SZ + (sz - 1) - macSz;
  939. /* Calculate #blocks processed in HMAC for max and real data. */
  940. blocks = maxSz >> blockBits;
  941. blocks += ((maxSz + padSz) % blockSz) < padSz;
  942. msgBlocks = realSz >> blockBits;
  943. /* #Extra blocks to process. */
  944. blocks -= msgBlocks + ((((realSz + padSz) % blockSz) < padSz) ? 1 : 0);
  945. /* Calculate whole blocks. */
  946. msgBlocks--;
  947. ret = wc_HmacUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
  948. if (ret == 0) {
  949. /* Fill the rest of the block with any available data. */
  950. currSz = ctMaskLT(msgSz, blockSz) & msgSz;
  951. currSz |= ctMaskGTE(msgSz, blockSz) & blockSz;
  952. currSz -= WOLFSSL_TLS_HMAC_INNER_SZ;
  953. currSz &= ~(0 - (currSz >> 31));
  954. ret = wc_HmacUpdate(hmac, in, currSz);
  955. offset = currSz;
  956. }
  957. if (ret == 0) {
  958. /* Do the hash operations on a block basis. */
  959. for (i = 0; i < msgBlocks; i++, offset += blockSz) {
  960. ret = wc_HmacUpdate(hmac, in + offset, blockSz);
  961. if (ret != 0)
  962. break;
  963. }
  964. }
  965. if (ret == 0)
  966. ret = wc_HmacUpdate(hmac, in + offset, msgSz - offset);
  967. if (ret == 0)
  968. ret = wc_HmacFinal(hmac, digest);
  969. if (ret == 0) {
  970. /* Do the dummy hash operations. Do at least one. */
  971. for (i = 0; i < blocks + 1; i++) {
  972. ret = wc_HmacUpdate(hmac, dummy, blockSz);
  973. if (ret != 0)
  974. break;
  975. }
  976. }
  977. return ret;
  978. }
  979. #endif
  980. int TLS_hmac(WOLFSSL* ssl, byte* digest, const byte* in, word32 sz, int padSz,
  981. int content, int verify, int epochOrder)
  982. {
  983. Hmac hmac;
  984. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  985. int ret = 0;
  986. const byte* macSecret = NULL;
  987. word32 hashSz = 0;
  988. if (ssl == NULL)
  989. return BAD_FUNC_ARG;
  990. #ifdef HAVE_TRUNCATED_HMAC
  991. hashSz = ssl->truncated_hmac ? (byte)TRUNCATED_HMAC_SZ
  992. : ssl->specs.hash_size;
  993. #else
  994. hashSz = ssl->specs.hash_size;
  995. #endif
  996. #ifdef HAVE_FUZZER
  997. /* Fuzz "in" buffer with sz to be used in HMAC algorithm */
  998. if (ssl->fuzzerCb) {
  999. if (verify && padSz >= 0) {
  1000. ssl->fuzzerCb(ssl, in, sz + hashSz + padSz + 1, FUZZ_HMAC,
  1001. ssl->fuzzerCtx);
  1002. }
  1003. else {
  1004. ssl->fuzzerCb(ssl, in, sz, FUZZ_HMAC, ssl->fuzzerCtx);
  1005. }
  1006. }
  1007. #endif
  1008. if (!ssl->options.dtls)
  1009. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, verify);
  1010. else
  1011. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, epochOrder);
  1012. ret = wc_HmacInit(&hmac, ssl->heap, ssl->devId);
  1013. if (ret != 0)
  1014. return ret;
  1015. #ifdef WOLFSSL_DTLS
  1016. if (ssl->options.dtls)
  1017. macSecret = wolfSSL_GetDtlsMacSecret(ssl, verify, epochOrder);
  1018. else
  1019. macSecret = wolfSSL_GetMacSecret(ssl, verify);
  1020. #else
  1021. macSecret = wolfSSL_GetMacSecret(ssl, verify);
  1022. #endif
  1023. ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
  1024. macSecret,
  1025. ssl->specs.hash_size);
  1026. if (ret == 0) {
  1027. /* Constant time verification required. */
  1028. if (verify && padSz >= 0) {
  1029. #if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
  1030. !defined(HAVE_SELFTEST)
  1031. #ifdef HAVE_BLAKE2
  1032. if (wolfSSL_GetHmacType(ssl) == WC_HASH_TYPE_BLAKE2B) {
  1033. ret = Hmac_UpdateFinal(&hmac, digest, in,
  1034. sz + hashSz + padSz + 1, myInner);
  1035. }
  1036. else
  1037. #endif
  1038. {
  1039. ret = Hmac_UpdateFinal_CT(&hmac, digest, in,
  1040. sz + hashSz + padSz + 1, hashSz, myInner);
  1041. }
  1042. #else
  1043. ret = Hmac_UpdateFinal(&hmac, digest, in, sz + hashSz + padSz + 1,
  1044. myInner);
  1045. #endif
  1046. }
  1047. else {
  1048. ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
  1049. if (ret == 0)
  1050. ret = wc_HmacUpdate(&hmac, in, sz); /* content */
  1051. if (ret == 0)
  1052. ret = wc_HmacFinal(&hmac, digest);
  1053. }
  1054. }
  1055. wc_HmacFree(&hmac);
  1056. return ret;
  1057. }
  1058. #endif /* WOLFSSL_AEAD_ONLY */
  1059. #endif /* !WOLFSSL_NO_TLS12 */
  1060. #ifdef HAVE_TLS_EXTENSIONS
  1061. /**
  1062. * The TLSX semaphore is used to calculate the size of the extensions to be sent
  1063. * from one peer to another.
  1064. */
  1065. /** Supports up to 72 flags. Increase as needed. */
  1066. #define SEMAPHORE_SIZE 9
  1067. /**
  1068. * Converts the extension type (id) to an index in the semaphore.
  1069. *
  1070. * Official reference for TLS extension types:
  1071. * http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xml
  1072. *
  1073. * Motivation:
  1074. * Previously, we used the extension type itself as the index of that
  1075. * extension in the semaphore as the extension types were declared
  1076. * sequentially, but maintain a semaphore as big as the number of available
  1077. * extensions is no longer an option since the release of renegotiation_info.
  1078. *
  1079. * How to update:
  1080. * Assign extension types that extrapolate the number of available semaphores
  1081. * to the first available index going backwards in the semaphore array.
  1082. * When adding a new extension type that don't extrapolate the number of
  1083. * available semaphores, check for a possible collision with with a
  1084. * 'remapped' extension type.
  1085. */
  1086. static WC_INLINE word16 TLSX_ToSemaphore(word16 type)
  1087. {
  1088. switch (type) {
  1089. case TLSX_RENEGOTIATION_INFO: /* 0xFF01 */
  1090. return 63;
  1091. #ifdef WOLFSSL_QUIC
  1092. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT: /* 0xffa5 */
  1093. return 64;
  1094. #endif
  1095. default:
  1096. if (type > 62) {
  1097. /* This message SHOULD only happens during the adding of
  1098. new TLS extensions in which its IANA number overflows
  1099. the current semaphore's range, or if its number already
  1100. is assigned to be used by another extension.
  1101. Use this check value for the new extension and decrement
  1102. the check value by one. */
  1103. WOLFSSL_MSG("### TLSX semaphore collision or overflow detected!");
  1104. }
  1105. }
  1106. return type;
  1107. }
  1108. /** Checks if a specific light (tls extension) is not set in the semaphore. */
  1109. #define IS_OFF(semaphore, light) \
  1110. (!(((semaphore)[(light) / 8] & (byte) (0x01 << ((light) % 8)))))
  1111. /** Turn on a specific light (tls extension) in the semaphore. */
  1112. /* the semaphore marks the extensions already written to the message */
  1113. #define TURN_ON(semaphore, light) \
  1114. ((semaphore)[(light) / 8] |= (byte) (0x01 << ((light) % 8)))
  1115. /** Turn off a specific light (tls extension) in the semaphore. */
  1116. #define TURN_OFF(semaphore, light) \
  1117. ((semaphore)[(light) / 8] &= (byte) ~(0x01 << ((light) % 8)))
  1118. /** Creates a new extension. */
  1119. static TLSX* TLSX_New(TLSX_Type type, const void* data, void* heap)
  1120. {
  1121. TLSX* extension = (TLSX*)XMALLOC(sizeof(TLSX), heap, DYNAMIC_TYPE_TLSX);
  1122. (void)heap;
  1123. if (extension) {
  1124. extension->type = type;
  1125. extension->data = (void*)data;
  1126. extension->resp = 0;
  1127. extension->next = NULL;
  1128. }
  1129. return extension;
  1130. }
  1131. /**
  1132. * Creates a new extension and pushes it to the provided list.
  1133. * Checks for duplicate extensions, keeps the newest.
  1134. */
  1135. int TLSX_Push(TLSX** list, TLSX_Type type, const void* data, void* heap)
  1136. {
  1137. TLSX* extension = TLSX_New(type, data, heap);
  1138. if (extension == NULL)
  1139. return MEMORY_E;
  1140. /* pushes the new extension on the list. */
  1141. extension->next = *list;
  1142. *list = extension;
  1143. /* remove duplicate extensions, there should be only one of each type. */
  1144. do {
  1145. if (extension->next && extension->next->type == type) {
  1146. TLSX *next = extension->next;
  1147. extension->next = next->next;
  1148. next->next = NULL;
  1149. TLSX_FreeAll(next, heap);
  1150. /* there is no way to occur more than
  1151. * two extensions of the same type.
  1152. */
  1153. break;
  1154. }
  1155. } while ((extension = extension->next));
  1156. return 0;
  1157. }
  1158. #ifdef WOLFSSL_TLS13
  1159. /**
  1160. * Creates a new extension and prepend it to the provided list.
  1161. * Checks for duplicate extensions, keeps the newest.
  1162. */
  1163. static int TLSX_Prepend(TLSX** list, TLSX_Type type, void* data, void* heap)
  1164. {
  1165. TLSX* extension = TLSX_New(type, data, heap);
  1166. TLSX* curr = *list;
  1167. if (extension == NULL)
  1168. return MEMORY_E;
  1169. /* remove duplicate extensions, there should be only one of each type. */
  1170. while (curr && curr->next) {
  1171. if (curr->next->type == type) {
  1172. TLSX *next = curr->next;
  1173. curr->next = next->next;
  1174. next->next = NULL;
  1175. TLSX_FreeAll(next, heap);
  1176. }
  1177. curr = curr->next;
  1178. }
  1179. if (curr)
  1180. curr->next = extension;
  1181. else
  1182. *list = extension;
  1183. return 0;
  1184. }
  1185. #endif /* WOLFSSL_TLS13 */
  1186. #ifndef NO_WOLFSSL_CLIENT
  1187. int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type);
  1188. int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type)
  1189. {
  1190. TLSX *extension = TLSX_Find(ssl->extensions, type);
  1191. if (!extension)
  1192. extension = TLSX_Find(ssl->ctx->extensions, type);
  1193. return extension == NULL;
  1194. }
  1195. int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl);
  1196. int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl)
  1197. {
  1198. SendAlert(ssl, alert_fatal, unsupported_extension);
  1199. WOLFSSL_ERROR_VERBOSE(UNSUPPORTED_EXTENSION);
  1200. return UNSUPPORTED_EXTENSION;
  1201. }
  1202. #else
  1203. #define TLSX_CheckUnsupportedExtension(ssl, type) 0
  1204. #define TLSX_HandleUnsupportedExtension(ssl) 0
  1205. #endif
  1206. #if !defined(NO_WOLFSSL_SERVER) || defined(WOLFSSL_TLS13)
  1207. /** Mark an extension to be sent back to the client. */
  1208. static void TLSX_SetResponse(WOLFSSL* ssl, TLSX_Type type)
  1209. {
  1210. TLSX *extension = TLSX_Find(ssl->extensions, type);
  1211. if (extension)
  1212. extension->resp = 1;
  1213. }
  1214. #endif
  1215. /******************************************************************************/
  1216. /* Application-Layer Protocol Negotiation */
  1217. /******************************************************************************/
  1218. #ifdef HAVE_ALPN
  1219. /** Creates a new ALPN object, providing protocol name to use. */
  1220. static ALPN* TLSX_ALPN_New(char *protocol_name, word16 protocol_nameSz,
  1221. void* heap)
  1222. {
  1223. ALPN *alpn;
  1224. WOLFSSL_ENTER("TLSX_ALPN_New");
  1225. if (protocol_name == NULL ||
  1226. protocol_nameSz > WOLFSSL_MAX_ALPN_PROTO_NAME_LEN) {
  1227. WOLFSSL_MSG("Invalid arguments");
  1228. return NULL;
  1229. }
  1230. alpn = (ALPN*)XMALLOC(sizeof(ALPN), heap, DYNAMIC_TYPE_TLSX);
  1231. if (alpn == NULL) {
  1232. WOLFSSL_MSG("Memory failure");
  1233. return NULL;
  1234. }
  1235. alpn->next = NULL;
  1236. alpn->negotiated = 0;
  1237. alpn->options = 0;
  1238. alpn->protocol_name = (char*)XMALLOC(protocol_nameSz + 1,
  1239. heap, DYNAMIC_TYPE_TLSX);
  1240. if (alpn->protocol_name == NULL) {
  1241. WOLFSSL_MSG("Memory failure");
  1242. XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
  1243. return NULL;
  1244. }
  1245. XMEMCPY(alpn->protocol_name, protocol_name, protocol_nameSz);
  1246. alpn->protocol_name[protocol_nameSz] = 0;
  1247. (void)heap;
  1248. return alpn;
  1249. }
  1250. /** Releases an ALPN object. */
  1251. static void TLSX_ALPN_Free(ALPN *alpn, void* heap)
  1252. {
  1253. (void)heap;
  1254. if (alpn == NULL)
  1255. return;
  1256. XFREE(alpn->protocol_name, heap, DYNAMIC_TYPE_TLSX);
  1257. XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
  1258. }
  1259. /** Releases all ALPN objects in the provided list. */
  1260. static void TLSX_ALPN_FreeAll(ALPN *list, void* heap)
  1261. {
  1262. ALPN* alpn;
  1263. while ((alpn = list)) {
  1264. list = alpn->next;
  1265. TLSX_ALPN_Free(alpn, heap);
  1266. }
  1267. }
  1268. /** Tells the buffered size of the ALPN objects in a list. */
  1269. static word16 TLSX_ALPN_GetSize(ALPN *list)
  1270. {
  1271. ALPN* alpn;
  1272. word16 length = OPAQUE16_LEN; /* list length */
  1273. while ((alpn = list)) {
  1274. list = alpn->next;
  1275. length++; /* protocol name length is on one byte */
  1276. length += (word16)XSTRLEN(alpn->protocol_name);
  1277. }
  1278. return length;
  1279. }
  1280. /** Writes the ALPN objects of a list in a buffer. */
  1281. static word16 TLSX_ALPN_Write(ALPN *list, byte *output)
  1282. {
  1283. ALPN* alpn;
  1284. word16 length = 0;
  1285. word16 offset = OPAQUE16_LEN; /* list length offset */
  1286. while ((alpn = list)) {
  1287. list = alpn->next;
  1288. length = (word16)XSTRLEN(alpn->protocol_name);
  1289. /* protocol name length */
  1290. output[offset++] = (byte)length;
  1291. /* protocol name value */
  1292. XMEMCPY(output + offset, alpn->protocol_name, length);
  1293. offset += length;
  1294. }
  1295. /* writing list length */
  1296. c16toa(offset - OPAQUE16_LEN, output);
  1297. return offset;
  1298. }
  1299. /** Finds a protocol name in the provided ALPN list */
  1300. static ALPN* TLSX_ALPN_Find(ALPN *list, char *protocol_name, word16 size)
  1301. {
  1302. ALPN *alpn;
  1303. if (list == NULL || protocol_name == NULL)
  1304. return NULL;
  1305. alpn = list;
  1306. while (alpn != NULL && (
  1307. (word16)XSTRLEN(alpn->protocol_name) != size ||
  1308. XSTRNCMP(alpn->protocol_name, protocol_name, size)))
  1309. alpn = alpn->next;
  1310. return alpn;
  1311. }
  1312. /** Set the ALPN matching client and server requirements */
  1313. static int TLSX_SetALPN(TLSX** extensions, const void* data, word16 size,
  1314. void* heap)
  1315. {
  1316. ALPN *alpn;
  1317. int ret;
  1318. if (extensions == NULL || data == NULL)
  1319. return BAD_FUNC_ARG;
  1320. alpn = TLSX_ALPN_New((char *)data, size, heap);
  1321. if (alpn == NULL) {
  1322. WOLFSSL_MSG("Memory failure");
  1323. return MEMORY_E;
  1324. }
  1325. alpn->negotiated = 1;
  1326. ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL, (void*)alpn,
  1327. heap);
  1328. if (ret != 0) {
  1329. TLSX_ALPN_Free(alpn, heap);
  1330. return ret;
  1331. }
  1332. return WOLFSSL_SUCCESS;
  1333. }
  1334. static int ALPN_find_match(WOLFSSL *ssl, TLSX **pextension,
  1335. const byte **psel, byte *psel_len,
  1336. const byte *alpn_val, word16 alpn_val_len)
  1337. {
  1338. TLSX *extension;
  1339. ALPN *alpn, *list;
  1340. const byte *sel = NULL, *s;
  1341. byte sel_len = 0, wlen;
  1342. extension = TLSX_Find(ssl->extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1343. if (extension == NULL)
  1344. extension = TLSX_Find(ssl->ctx->extensions,
  1345. TLSX_APPLICATION_LAYER_PROTOCOL);
  1346. /* No ALPN configured here */
  1347. if (extension == NULL || extension->data == NULL) {
  1348. *pextension = NULL;
  1349. *psel = NULL;
  1350. *psel_len = 0;
  1351. return 0;
  1352. }
  1353. list = (ALPN*)extension->data;
  1354. for (s = alpn_val;
  1355. (s - alpn_val) < alpn_val_len;
  1356. s += wlen) {
  1357. wlen = *s++; /* bounds already checked on save */
  1358. alpn = TLSX_ALPN_Find(list, (char*)s, wlen);
  1359. if (alpn != NULL) {
  1360. WOLFSSL_MSG("ALPN protocol match");
  1361. sel = s,
  1362. sel_len = wlen;
  1363. break;
  1364. }
  1365. }
  1366. if (sel == NULL) {
  1367. WOLFSSL_MSG("No ALPN protocol match");
  1368. /* do nothing if no protocol match between client and server and option
  1369. is set to continue (like OpenSSL) */
  1370. if (list->options & WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) {
  1371. WOLFSSL_MSG("Continue on mismatch");
  1372. }
  1373. else {
  1374. SendAlert(ssl, alert_fatal, no_application_protocol);
  1375. WOLFSSL_ERROR_VERBOSE(UNKNOWN_ALPN_PROTOCOL_NAME_E);
  1376. return UNKNOWN_ALPN_PROTOCOL_NAME_E;
  1377. }
  1378. }
  1379. *pextension = extension;
  1380. *psel = sel;
  1381. *psel_len = sel_len;
  1382. return 0;
  1383. }
  1384. int ALPN_Select(WOLFSSL *ssl)
  1385. {
  1386. TLSX *extension;
  1387. const byte *sel = NULL;
  1388. byte sel_len = 0;
  1389. int r = 0;
  1390. WOLFSSL_ENTER("ALPN_Select");
  1391. if (ssl->alpn_peer_requested == NULL)
  1392. return 0;
  1393. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
  1394. if (ssl->alpnSelect != NULL && ssl->options.side == WOLFSSL_SERVER_END) {
  1395. if (ssl->alpnSelect(ssl, &sel, &sel_len, ssl->alpn_peer_requested,
  1396. ssl->alpn_peer_requested_length,
  1397. ssl->alpnSelectArg) == 0) {
  1398. WOLFSSL_MSG("ALPN protocol match");
  1399. }
  1400. else {
  1401. sel = NULL;
  1402. sel_len = 0;
  1403. }
  1404. }
  1405. #endif
  1406. if (sel == NULL) {
  1407. r = ALPN_find_match(ssl, &extension, &sel, &sel_len,
  1408. ssl->alpn_peer_requested,
  1409. ssl->alpn_peer_requested_length);
  1410. if (r != 0)
  1411. return r;
  1412. }
  1413. if (sel != NULL) {
  1414. /* set the matching negotiated protocol */
  1415. r = TLSX_SetALPN(&ssl->extensions, sel, sel_len, ssl->heap);
  1416. if (r != WOLFSSL_SUCCESS) {
  1417. WOLFSSL_MSG("TLSX_SetALPN failed");
  1418. return BUFFER_ERROR;
  1419. }
  1420. /* reply to ALPN extension sent from peer */
  1421. #ifndef NO_WOLFSSL_SERVER
  1422. TLSX_SetResponse(ssl, TLSX_APPLICATION_LAYER_PROTOCOL);
  1423. #endif
  1424. }
  1425. return 0;
  1426. }
  1427. /** Parses a buffer of ALPN extensions and set the first one matching
  1428. * client and server requirements */
  1429. static int TLSX_ALPN_ParseAndSet(WOLFSSL *ssl, const byte *input, word16 length,
  1430. byte isRequest)
  1431. {
  1432. word16 size = 0, offset = 0, wlen;
  1433. int r = BUFFER_ERROR;
  1434. const byte *s;
  1435. if (OPAQUE16_LEN > length)
  1436. return BUFFER_ERROR;
  1437. ato16(input, &size);
  1438. offset += OPAQUE16_LEN;
  1439. /* validating alpn list length */
  1440. if (size == 0 || length != OPAQUE16_LEN + size)
  1441. return BUFFER_ERROR;
  1442. /* validating length of entries before accepting */
  1443. for (s = input + offset; (s - input) < size; s += wlen) {
  1444. wlen = *s++;
  1445. if (wlen == 0 || (s + wlen - input) > length)
  1446. return BUFFER_ERROR;
  1447. }
  1448. if (isRequest) {
  1449. /* keep the list sent by peer, if this is from a request. We
  1450. * use it later in ALPN_Select() for evaluation. */
  1451. if (ssl->alpn_peer_requested != NULL) {
  1452. XFREE(ssl->alpn_peer_requested, ssl->heap, DYNAMIC_TYPE_ALPN);
  1453. ssl->alpn_peer_requested_length = 0;
  1454. }
  1455. ssl->alpn_peer_requested = (byte *)XMALLOC(size, ssl->heap,
  1456. DYNAMIC_TYPE_ALPN);
  1457. if (ssl->alpn_peer_requested == NULL) {
  1458. return MEMORY_ERROR;
  1459. }
  1460. ssl->alpn_peer_requested_length = size;
  1461. XMEMCPY(ssl->alpn_peer_requested, (char*)input + offset, size);
  1462. }
  1463. else {
  1464. /* a response, we should find the value in our config */
  1465. const byte *sel = NULL;
  1466. byte sel_len = 0;
  1467. TLSX *extension = NULL;
  1468. r = ALPN_find_match(ssl, &extension, &sel, &sel_len, input + offset, size);
  1469. if (r != 0)
  1470. return r;
  1471. if (sel != NULL) {
  1472. /* set the matching negotiated protocol */
  1473. r = TLSX_SetALPN(&ssl->extensions, sel, sel_len, ssl->heap);
  1474. if (r != WOLFSSL_SUCCESS) {
  1475. WOLFSSL_MSG("TLSX_SetALPN failed");
  1476. return BUFFER_ERROR;
  1477. }
  1478. }
  1479. /* If we had nothing configured, the response is unexpected */
  1480. else if (extension == NULL) {
  1481. r = TLSX_HandleUnsupportedExtension(ssl);
  1482. if (r != 0)
  1483. return r;
  1484. }
  1485. }
  1486. return 0;
  1487. }
  1488. /** Add a protocol name to the list of accepted usable ones */
  1489. int TLSX_UseALPN(TLSX** extensions, const void* data, word16 size, byte options,
  1490. void* heap)
  1491. {
  1492. ALPN *alpn;
  1493. TLSX *extension;
  1494. int ret;
  1495. if (extensions == NULL || data == NULL)
  1496. return BAD_FUNC_ARG;
  1497. alpn = TLSX_ALPN_New((char *)data, size, heap);
  1498. if (alpn == NULL) {
  1499. WOLFSSL_MSG("Memory failure");
  1500. return MEMORY_E;
  1501. }
  1502. /* Set Options of ALPN */
  1503. alpn->options = options;
  1504. extension = TLSX_Find(*extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1505. if (extension == NULL) {
  1506. ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL,
  1507. (void*)alpn, heap);
  1508. if (ret != 0) {
  1509. TLSX_ALPN_Free(alpn, heap);
  1510. return ret;
  1511. }
  1512. }
  1513. else {
  1514. /* push new ALPN object to extension data. */
  1515. alpn->next = (ALPN*)extension->data;
  1516. extension->data = (void*)alpn;
  1517. }
  1518. return WOLFSSL_SUCCESS;
  1519. }
  1520. /** Get the protocol name set by the server */
  1521. int TLSX_ALPN_GetRequest(TLSX* extensions, void** data, word16 *dataSz)
  1522. {
  1523. TLSX *extension;
  1524. ALPN *alpn;
  1525. if (extensions == NULL || data == NULL || dataSz == NULL)
  1526. return BAD_FUNC_ARG;
  1527. extension = TLSX_Find(extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1528. if (extension == NULL) {
  1529. WOLFSSL_MSG("TLS extension not found");
  1530. WOLFSSL_ERROR_VERBOSE(WOLFSSL_ALPN_NOT_FOUND);
  1531. return WOLFSSL_ALPN_NOT_FOUND;
  1532. }
  1533. alpn = (ALPN *)extension->data;
  1534. if (alpn == NULL) {
  1535. WOLFSSL_MSG("ALPN extension not found");
  1536. *data = NULL;
  1537. *dataSz = 0;
  1538. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1539. return WOLFSSL_FATAL_ERROR;
  1540. }
  1541. if (alpn->negotiated != 1) {
  1542. /* consider as an error */
  1543. if (alpn->options & WOLFSSL_ALPN_FAILED_ON_MISMATCH) {
  1544. WOLFSSL_MSG("No protocol match with peer -> Failed");
  1545. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1546. return WOLFSSL_FATAL_ERROR;
  1547. }
  1548. /* continue without negotiated protocol */
  1549. WOLFSSL_MSG("No protocol match with peer -> Continue");
  1550. WOLFSSL_ERROR_VERBOSE(WOLFSSL_ALPN_NOT_FOUND);
  1551. return WOLFSSL_ALPN_NOT_FOUND;
  1552. }
  1553. if (alpn->next != NULL) {
  1554. WOLFSSL_MSG("Only one protocol name must be accepted");
  1555. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1556. return WOLFSSL_FATAL_ERROR;
  1557. }
  1558. *data = alpn->protocol_name;
  1559. *dataSz = (word16)XSTRLEN((char*)*data);
  1560. return WOLFSSL_SUCCESS;
  1561. }
  1562. #define ALPN_FREE_ALL TLSX_ALPN_FreeAll
  1563. #define ALPN_GET_SIZE TLSX_ALPN_GetSize
  1564. #define ALPN_WRITE TLSX_ALPN_Write
  1565. #define ALPN_PARSE TLSX_ALPN_ParseAndSet
  1566. #else /* HAVE_ALPN */
  1567. #define ALPN_FREE_ALL(list, heap)
  1568. #define ALPN_GET_SIZE(list) 0
  1569. #define ALPN_WRITE(a, b) 0
  1570. #define ALPN_PARSE(a, b, c, d) 0
  1571. #endif /* HAVE_ALPN */
  1572. /******************************************************************************/
  1573. /* Server Name Indication */
  1574. /******************************************************************************/
  1575. #ifdef HAVE_SNI
  1576. /** Creates a new SNI object. */
  1577. static SNI* TLSX_SNI_New(byte type, const void* data, word16 size, void* heap)
  1578. {
  1579. SNI* sni = (SNI*)XMALLOC(sizeof(SNI), heap, DYNAMIC_TYPE_TLSX);
  1580. (void)heap;
  1581. if (sni) {
  1582. sni->type = type;
  1583. sni->next = NULL;
  1584. #ifndef NO_WOLFSSL_SERVER
  1585. sni->options = 0;
  1586. sni->status = WOLFSSL_SNI_NO_MATCH;
  1587. #endif
  1588. switch (sni->type) {
  1589. case WOLFSSL_SNI_HOST_NAME:
  1590. sni->data.host_name = (char*)XMALLOC(size + 1, heap,
  1591. DYNAMIC_TYPE_TLSX);
  1592. if (sni->data.host_name) {
  1593. XSTRNCPY(sni->data.host_name, (const char*)data, size);
  1594. sni->data.host_name[size] = '\0';
  1595. } else {
  1596. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1597. sni = NULL;
  1598. }
  1599. break;
  1600. default: /* invalid type */
  1601. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1602. sni = NULL;
  1603. }
  1604. }
  1605. return sni;
  1606. }
  1607. /** Releases a SNI object. */
  1608. static void TLSX_SNI_Free(SNI* sni, void* heap)
  1609. {
  1610. if (sni) {
  1611. switch (sni->type) {
  1612. case WOLFSSL_SNI_HOST_NAME:
  1613. XFREE(sni->data.host_name, heap, DYNAMIC_TYPE_TLSX);
  1614. break;
  1615. }
  1616. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1617. }
  1618. (void)heap;
  1619. }
  1620. /** Releases all SNI objects in the provided list. */
  1621. static void TLSX_SNI_FreeAll(SNI* list, void* heap)
  1622. {
  1623. SNI* sni;
  1624. while ((sni = list)) {
  1625. list = sni->next;
  1626. TLSX_SNI_Free(sni, heap);
  1627. }
  1628. }
  1629. /** Tells the buffered size of the SNI objects in a list. */
  1630. static word16 TLSX_SNI_GetSize(SNI* list)
  1631. {
  1632. SNI* sni;
  1633. word16 length = OPAQUE16_LEN; /* list length */
  1634. while ((sni = list)) {
  1635. list = sni->next;
  1636. length += ENUM_LEN + OPAQUE16_LEN; /* sni type + sni length */
  1637. switch (sni->type) {
  1638. case WOLFSSL_SNI_HOST_NAME:
  1639. length += (word16)XSTRLEN((char*)sni->data.host_name);
  1640. break;
  1641. }
  1642. }
  1643. return length;
  1644. }
  1645. /** Writes the SNI objects of a list in a buffer. */
  1646. static word16 TLSX_SNI_Write(SNI* list, byte* output)
  1647. {
  1648. SNI* sni;
  1649. word16 length = 0;
  1650. word16 offset = OPAQUE16_LEN; /* list length offset */
  1651. while ((sni = list)) {
  1652. list = sni->next;
  1653. output[offset++] = sni->type; /* sni type */
  1654. switch (sni->type) {
  1655. case WOLFSSL_SNI_HOST_NAME:
  1656. length = (word16)XSTRLEN((char*)sni->data.host_name);
  1657. c16toa(length, output + offset); /* sni length */
  1658. offset += OPAQUE16_LEN;
  1659. XMEMCPY(output + offset, sni->data.host_name, length);
  1660. offset += length;
  1661. break;
  1662. }
  1663. }
  1664. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  1665. return offset;
  1666. }
  1667. /** Finds a SNI object in the provided list. */
  1668. static SNI* TLSX_SNI_Find(SNI *list, byte type)
  1669. {
  1670. SNI* sni = list;
  1671. while (sni && sni->type != type)
  1672. sni = sni->next;
  1673. return sni;
  1674. }
  1675. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  1676. /** Sets the status of a SNI object. */
  1677. static void TLSX_SNI_SetStatus(TLSX* extensions, byte type, byte status)
  1678. {
  1679. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1680. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1681. if (sni)
  1682. sni->status = status;
  1683. }
  1684. #endif
  1685. /** Gets the status of a SNI object. */
  1686. byte TLSX_SNI_Status(TLSX* extensions, byte type)
  1687. {
  1688. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1689. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1690. if (sni)
  1691. return sni->status;
  1692. return 0;
  1693. }
  1694. /** Parses a buffer of SNI extensions. */
  1695. static int TLSX_SNI_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  1696. byte isRequest)
  1697. {
  1698. #ifndef NO_WOLFSSL_SERVER
  1699. word16 size = 0;
  1700. word16 offset = 0;
  1701. int cacheOnly = 0;
  1702. SNI *sni = NULL;
  1703. byte type;
  1704. int matchStat;
  1705. byte matched;
  1706. #endif
  1707. TLSX *extension = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  1708. if (!extension)
  1709. extension = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  1710. if (!isRequest) {
  1711. #ifndef NO_WOLFSSL_CLIENT
  1712. if (!extension || !extension->data)
  1713. return TLSX_HandleUnsupportedExtension(ssl);
  1714. if (length > 0)
  1715. return BUFFER_ERROR; /* SNI response MUST be empty. */
  1716. /* This call enables wolfSSL_SNI_GetRequest() to be called in the
  1717. * client side to fetch the used SNI. It will only work if the SNI
  1718. * was set at the SSL object level. Right now we only support one
  1719. * name type, WOLFSSL_SNI_HOST_NAME, but in the future, the
  1720. * inclusion of other name types will turn this method inaccurate,
  1721. * as the extension response doesn't contains information of which
  1722. * name was accepted.
  1723. */
  1724. TLSX_SNI_SetStatus(ssl->extensions, WOLFSSL_SNI_HOST_NAME,
  1725. WOLFSSL_SNI_REAL_MATCH);
  1726. return 0;
  1727. #endif
  1728. }
  1729. #ifndef NO_WOLFSSL_SERVER
  1730. if (!extension || !extension->data) {
  1731. /* This will keep SNI even though TLSX_UseSNI has not been called.
  1732. * Enable it so that the received sni is available to functions
  1733. * that use a custom callback when SNI is received.
  1734. */
  1735. #ifdef WOLFSSL_ALWAYS_KEEP_SNI
  1736. cacheOnly = 1;
  1737. #endif
  1738. if (ssl->ctx->sniRecvCb) {
  1739. cacheOnly = 1;
  1740. }
  1741. if (cacheOnly) {
  1742. WOLFSSL_MSG("Forcing SSL object to store SNI parameter");
  1743. }
  1744. else {
  1745. /* Skipping, SNI not enabled at server side. */
  1746. return 0;
  1747. }
  1748. }
  1749. if (OPAQUE16_LEN > length)
  1750. return BUFFER_ERROR;
  1751. ato16(input, &size);
  1752. offset += OPAQUE16_LEN;
  1753. /* validating sni list length */
  1754. if (length != OPAQUE16_LEN + size || size == 0)
  1755. return BUFFER_ERROR;
  1756. /* SNI was badly specified and only one type is now recognized and allowed.
  1757. * Only one SNI value per type (RFC6066), so, no loop. */
  1758. type = input[offset++];
  1759. if (type != WOLFSSL_SNI_HOST_NAME)
  1760. return BUFFER_ERROR;
  1761. if (offset + OPAQUE16_LEN > length)
  1762. return BUFFER_ERROR;
  1763. ato16(input + offset, &size);
  1764. offset += OPAQUE16_LEN;
  1765. if (offset + size != length || size == 0)
  1766. return BUFFER_ERROR;
  1767. if (!cacheOnly && !(sni = TLSX_SNI_Find((SNI*)extension->data, type)))
  1768. return 0; /* not using this type of SNI. */
  1769. #ifdef WOLFSSL_TLS13
  1770. /* Don't process the second ClientHello SNI extension if there
  1771. * was problems with the first.
  1772. */
  1773. if (!cacheOnly && sni->status != 0)
  1774. return 0;
  1775. #endif
  1776. matched = cacheOnly || (XSTRLEN(sni->data.host_name) == size &&
  1777. XSTRNCMP(sni->data.host_name, (const char*)input + offset, size) == 0);
  1778. if (matched || sni->options & WOLFSSL_SNI_ANSWER_ON_MISMATCH) {
  1779. int r = TLSX_UseSNI(&ssl->extensions, type, input + offset, size,
  1780. ssl->heap);
  1781. if (r != WOLFSSL_SUCCESS)
  1782. return r; /* throws error. */
  1783. if (cacheOnly) {
  1784. WOLFSSL_MSG("Forcing storage of SNI, Fake match");
  1785. matchStat = WOLFSSL_SNI_FORCE_KEEP;
  1786. }
  1787. else if (matched) {
  1788. WOLFSSL_MSG("SNI did match!");
  1789. matchStat = WOLFSSL_SNI_REAL_MATCH;
  1790. }
  1791. else {
  1792. WOLFSSL_MSG("fake SNI match from ANSWER_ON_MISMATCH");
  1793. matchStat = WOLFSSL_SNI_FAKE_MATCH;
  1794. }
  1795. TLSX_SNI_SetStatus(ssl->extensions, type, (byte)matchStat);
  1796. if (!cacheOnly)
  1797. TLSX_SetResponse(ssl, TLSX_SERVER_NAME);
  1798. }
  1799. else if (!(sni->options & WOLFSSL_SNI_CONTINUE_ON_MISMATCH)) {
  1800. SendAlert(ssl, alert_fatal, unrecognized_name);
  1801. WOLFSSL_ERROR_VERBOSE(UNKNOWN_SNI_HOST_NAME_E);
  1802. return UNKNOWN_SNI_HOST_NAME_E;
  1803. }
  1804. #else
  1805. (void)input;
  1806. #endif
  1807. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1808. (void)length;
  1809. #endif
  1810. return 0;
  1811. }
  1812. static int TLSX_SNI_VerifyParse(WOLFSSL* ssl, byte isRequest)
  1813. {
  1814. (void)ssl;
  1815. if (isRequest) {
  1816. #ifndef NO_WOLFSSL_SERVER
  1817. TLSX* ctx_ext = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  1818. TLSX* ssl_ext = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  1819. SNI* ctx_sni = ctx_ext ? (SNI*)ctx_ext->data : NULL;
  1820. SNI* ssl_sni = ssl_ext ? (SNI*)ssl_ext->data : NULL;
  1821. SNI* sni = NULL;
  1822. for (; ctx_sni; ctx_sni = ctx_sni->next) {
  1823. if (ctx_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
  1824. sni = TLSX_SNI_Find(ssl_sni, ctx_sni->type);
  1825. if (sni) {
  1826. if (sni->status != WOLFSSL_SNI_NO_MATCH)
  1827. continue;
  1828. /* if ssl level overrides ctx level, it is ok. */
  1829. if ((sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) == 0)
  1830. continue;
  1831. }
  1832. SendAlert(ssl, alert_fatal, handshake_failure);
  1833. WOLFSSL_ERROR_VERBOSE(SNI_ABSENT_ERROR);
  1834. return SNI_ABSENT_ERROR;
  1835. }
  1836. }
  1837. for (; ssl_sni; ssl_sni = ssl_sni->next) {
  1838. if (ssl_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
  1839. if (ssl_sni->status != WOLFSSL_SNI_NO_MATCH)
  1840. continue;
  1841. SendAlert(ssl, alert_fatal, handshake_failure);
  1842. WOLFSSL_ERROR_VERBOSE(SNI_ABSENT_ERROR);
  1843. return SNI_ABSENT_ERROR;
  1844. }
  1845. }
  1846. #endif /* NO_WOLFSSL_SERVER */
  1847. }
  1848. return 0;
  1849. }
  1850. int TLSX_UseSNI(TLSX** extensions, byte type, const void* data, word16 size,
  1851. void* heap)
  1852. {
  1853. TLSX* extension;
  1854. SNI* sni = NULL;
  1855. if (extensions == NULL || data == NULL)
  1856. return BAD_FUNC_ARG;
  1857. if ((sni = TLSX_SNI_New(type, data, size, heap)) == NULL)
  1858. return MEMORY_E;
  1859. extension = TLSX_Find(*extensions, TLSX_SERVER_NAME);
  1860. if (!extension) {
  1861. int ret = TLSX_Push(extensions, TLSX_SERVER_NAME, (void*)sni, heap);
  1862. if (ret != 0) {
  1863. TLSX_SNI_Free(sni, heap);
  1864. return ret;
  1865. }
  1866. }
  1867. else {
  1868. /* push new SNI object to extension data. */
  1869. sni->next = (SNI*)extension->data;
  1870. extension->data = (void*)sni;
  1871. /* remove duplicate SNI, there should be only one of each type. */
  1872. do {
  1873. if (sni->next && sni->next->type == type) {
  1874. SNI* next = sni->next;
  1875. sni->next = next->next;
  1876. TLSX_SNI_Free(next, heap);
  1877. /* there is no way to occur more than
  1878. * two SNIs of the same type.
  1879. */
  1880. break;
  1881. }
  1882. } while ((sni = sni->next));
  1883. }
  1884. return WOLFSSL_SUCCESS;
  1885. }
  1886. #ifndef NO_WOLFSSL_SERVER
  1887. /** Tells the SNI requested by the client. */
  1888. word16 TLSX_SNI_GetRequest(TLSX* extensions, byte type, void** data)
  1889. {
  1890. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1891. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1892. if (sni && sni->status != WOLFSSL_SNI_NO_MATCH) {
  1893. switch (sni->type) {
  1894. case WOLFSSL_SNI_HOST_NAME:
  1895. if (data) {
  1896. *data = sni->data.host_name;
  1897. return (word16)XSTRLEN((char*)*data);
  1898. }
  1899. }
  1900. }
  1901. return 0;
  1902. }
  1903. /** Sets the options for a SNI object. */
  1904. void TLSX_SNI_SetOptions(TLSX* extensions, byte type, byte options)
  1905. {
  1906. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1907. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1908. if (sni)
  1909. sni->options = options;
  1910. }
  1911. /** Retrieves a SNI request from a client hello buffer. */
  1912. int TLSX_SNI_GetFromBuffer(const byte* clientHello, word32 helloSz,
  1913. byte type, byte* sni, word32* inOutSz)
  1914. {
  1915. word32 offset = 0;
  1916. word32 len32 = 0;
  1917. word16 len16 = 0;
  1918. if (helloSz < RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + CLIENT_HELLO_FIRST)
  1919. return INCOMPLETE_DATA;
  1920. /* TLS record header */
  1921. if ((enum ContentType) clientHello[offset++] != handshake) {
  1922. /* checking for SSLv2.0 client hello according to: */
  1923. /* http://tools.ietf.org/html/rfc4346#appendix-E.1 */
  1924. if ((enum HandShakeType) clientHello[++offset] == client_hello) {
  1925. offset += ENUM_LEN + VERSION_SZ; /* skip version */
  1926. ato16(clientHello + offset, &len16);
  1927. offset += OPAQUE16_LEN;
  1928. if (len16 % 3) /* cipher_spec_length must be multiple of 3 */
  1929. return BUFFER_ERROR;
  1930. ato16(clientHello + offset, &len16);
  1931. /* Returning SNI_UNSUPPORTED do not increment offset here */
  1932. if (len16 != 0) /* session_id_length must be 0 */
  1933. return BUFFER_ERROR;
  1934. WOLFSSL_ERROR_VERBOSE(SNI_UNSUPPORTED);
  1935. return SNI_UNSUPPORTED;
  1936. }
  1937. return BUFFER_ERROR;
  1938. }
  1939. if (clientHello[offset++] != SSLv3_MAJOR)
  1940. return BUFFER_ERROR;
  1941. if (clientHello[offset++] < TLSv1_MINOR) {
  1942. WOLFSSL_ERROR_VERBOSE(SNI_UNSUPPORTED);
  1943. return SNI_UNSUPPORTED;
  1944. }
  1945. ato16(clientHello + offset, &len16);
  1946. offset += OPAQUE16_LEN;
  1947. if (offset + len16 > helloSz)
  1948. return INCOMPLETE_DATA;
  1949. /* Handshake header */
  1950. if ((enum HandShakeType) clientHello[offset] != client_hello)
  1951. return BUFFER_ERROR;
  1952. c24to32(clientHello + offset + 1, &len32);
  1953. offset += HANDSHAKE_HEADER_SZ;
  1954. if (offset + len32 > helloSz)
  1955. return BUFFER_ERROR;
  1956. /* client hello */
  1957. offset += VERSION_SZ + RAN_LEN; /* version, random */
  1958. if (helloSz < offset + clientHello[offset])
  1959. return BUFFER_ERROR;
  1960. offset += ENUM_LEN + clientHello[offset]; /* skip session id */
  1961. /* cypher suites */
  1962. if (helloSz < offset + OPAQUE16_LEN)
  1963. return BUFFER_ERROR;
  1964. ato16(clientHello + offset, &len16);
  1965. offset += OPAQUE16_LEN;
  1966. if (helloSz < offset + len16)
  1967. return BUFFER_ERROR;
  1968. offset += len16; /* skip cypher suites */
  1969. /* compression methods */
  1970. if (helloSz < offset + 1)
  1971. return BUFFER_ERROR;
  1972. if (helloSz < offset + clientHello[offset])
  1973. return BUFFER_ERROR;
  1974. offset += ENUM_LEN + clientHello[offset]; /* skip compression methods */
  1975. /* extensions */
  1976. if (helloSz < offset + OPAQUE16_LEN)
  1977. return 0; /* no extensions in client hello. */
  1978. ato16(clientHello + offset, &len16);
  1979. offset += OPAQUE16_LEN;
  1980. if (helloSz < offset + len16)
  1981. return BUFFER_ERROR;
  1982. while (len16 >= OPAQUE16_LEN + OPAQUE16_LEN) {
  1983. word16 extType;
  1984. word16 extLen;
  1985. ato16(clientHello + offset, &extType);
  1986. offset += OPAQUE16_LEN;
  1987. ato16(clientHello + offset, &extLen);
  1988. offset += OPAQUE16_LEN;
  1989. if (helloSz < offset + extLen)
  1990. return BUFFER_ERROR;
  1991. if (extType != TLSX_SERVER_NAME) {
  1992. offset += extLen; /* skip extension */
  1993. } else {
  1994. word16 listLen;
  1995. ato16(clientHello + offset, &listLen);
  1996. offset += OPAQUE16_LEN;
  1997. if (helloSz < offset + listLen)
  1998. return BUFFER_ERROR;
  1999. while (listLen > ENUM_LEN + OPAQUE16_LEN) {
  2000. byte sniType = clientHello[offset++];
  2001. word16 sniLen;
  2002. ato16(clientHello + offset, &sniLen);
  2003. offset += OPAQUE16_LEN;
  2004. if (helloSz < offset + sniLen)
  2005. return BUFFER_ERROR;
  2006. if (sniType != type) {
  2007. offset += sniLen;
  2008. listLen -= min(ENUM_LEN + OPAQUE16_LEN + sniLen, listLen);
  2009. continue;
  2010. }
  2011. *inOutSz = min(sniLen, *inOutSz);
  2012. XMEMCPY(sni, clientHello + offset, *inOutSz);
  2013. return WOLFSSL_SUCCESS;
  2014. }
  2015. }
  2016. len16 -= min(2 * OPAQUE16_LEN + extLen, len16);
  2017. }
  2018. return len16 ? BUFFER_ERROR : 0;
  2019. }
  2020. #endif
  2021. #define SNI_FREE_ALL TLSX_SNI_FreeAll
  2022. #define SNI_GET_SIZE TLSX_SNI_GetSize
  2023. #define SNI_WRITE TLSX_SNI_Write
  2024. #define SNI_PARSE TLSX_SNI_Parse
  2025. #define SNI_VERIFY_PARSE TLSX_SNI_VerifyParse
  2026. #else
  2027. #define SNI_FREE_ALL(list, heap)
  2028. #define SNI_GET_SIZE(list) 0
  2029. #define SNI_WRITE(a, b) 0
  2030. #define SNI_PARSE(a, b, c, d) 0
  2031. #define SNI_VERIFY_PARSE(a, b) 0
  2032. #endif /* HAVE_SNI */
  2033. /******************************************************************************/
  2034. /* Trusted CA Key Indication */
  2035. /******************************************************************************/
  2036. #ifdef HAVE_TRUSTED_CA
  2037. /** Creates a new TCA object. */
  2038. static TCA* TLSX_TCA_New(byte type, const byte* id, word16 idSz, void* heap)
  2039. {
  2040. TCA* tca = (TCA*)XMALLOC(sizeof(TCA), heap, DYNAMIC_TYPE_TLSX);
  2041. if (tca) {
  2042. XMEMSET(tca, 0, sizeof(TCA));
  2043. tca->type = type;
  2044. switch (type) {
  2045. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2046. break;
  2047. #ifndef NO_SHA
  2048. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2049. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2050. if (idSz == WC_SHA_DIGEST_SIZE &&
  2051. (tca->id =
  2052. (byte*)XMALLOC(idSz, heap, DYNAMIC_TYPE_TLSX))) {
  2053. XMEMCPY(tca->id, id, idSz);
  2054. tca->idSz = idSz;
  2055. }
  2056. else {
  2057. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2058. tca = NULL;
  2059. }
  2060. break;
  2061. #endif
  2062. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2063. if (idSz > 0 &&
  2064. (tca->id =
  2065. (byte*)XMALLOC(idSz, heap, DYNAMIC_TYPE_TLSX))) {
  2066. XMEMCPY(tca->id, id, idSz);
  2067. tca->idSz = idSz;
  2068. }
  2069. else {
  2070. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2071. tca = NULL;
  2072. }
  2073. break;
  2074. default: /* invalid type */
  2075. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2076. tca = NULL;
  2077. }
  2078. }
  2079. (void)heap;
  2080. return tca;
  2081. }
  2082. /** Releases a TCA object. */
  2083. static void TLSX_TCA_Free(TCA* tca, void* heap)
  2084. {
  2085. (void)heap;
  2086. if (tca) {
  2087. if (tca->id)
  2088. XFREE(tca->id, heap, DYNAMIC_TYPE_TLSX);
  2089. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2090. }
  2091. }
  2092. /** Releases all TCA objects in the provided list. */
  2093. static void TLSX_TCA_FreeAll(TCA* list, void* heap)
  2094. {
  2095. TCA* tca;
  2096. while ((tca = list)) {
  2097. list = tca->next;
  2098. TLSX_TCA_Free(tca, heap);
  2099. }
  2100. }
  2101. /** Tells the buffered size of the TCA objects in a list. */
  2102. static word16 TLSX_TCA_GetSize(TCA* list)
  2103. {
  2104. TCA* tca;
  2105. word16 length = OPAQUE16_LEN; /* list length */
  2106. while ((tca = list)) {
  2107. list = tca->next;
  2108. length += ENUM_LEN; /* tca type */
  2109. switch (tca->type) {
  2110. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2111. break;
  2112. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2113. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2114. length += tca->idSz;
  2115. break;
  2116. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2117. length += OPAQUE16_LEN + tca->idSz;
  2118. break;
  2119. }
  2120. }
  2121. return length;
  2122. }
  2123. /** Writes the TCA objects of a list in a buffer. */
  2124. static word16 TLSX_TCA_Write(TCA* list, byte* output)
  2125. {
  2126. TCA* tca;
  2127. word16 offset = OPAQUE16_LEN; /* list length offset */
  2128. while ((tca = list)) {
  2129. list = tca->next;
  2130. output[offset++] = tca->type; /* tca type */
  2131. switch (tca->type) {
  2132. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2133. break;
  2134. #ifndef NO_SHA
  2135. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2136. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2137. if (tca->id != NULL) {
  2138. XMEMCPY(output + offset, tca->id, tca->idSz);
  2139. offset += tca->idSz;
  2140. }
  2141. else {
  2142. /* ID missing. Set to an empty string. */
  2143. c16toa(0, output + offset);
  2144. offset += OPAQUE16_LEN;
  2145. }
  2146. break;
  2147. #endif
  2148. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2149. if (tca->id != NULL) {
  2150. c16toa(tca->idSz, output + offset); /* tca length */
  2151. offset += OPAQUE16_LEN;
  2152. XMEMCPY(output + offset, tca->id, tca->idSz);
  2153. offset += tca->idSz;
  2154. }
  2155. else {
  2156. /* ID missing. Set to an empty string. */
  2157. c16toa(0, output + offset);
  2158. offset += OPAQUE16_LEN;
  2159. }
  2160. break;
  2161. default:
  2162. /* ID unknown. Set to an empty string. */
  2163. c16toa(0, output + offset);
  2164. offset += OPAQUE16_LEN;
  2165. }
  2166. }
  2167. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  2168. return offset;
  2169. }
  2170. #ifndef NO_WOLFSSL_SERVER
  2171. static TCA* TLSX_TCA_Find(TCA *list, byte type, const byte* id, word16 idSz)
  2172. {
  2173. TCA* tca = list;
  2174. while (tca && tca->type != type && type != WOLFSSL_TRUSTED_CA_PRE_AGREED &&
  2175. idSz != tca->idSz && !XMEMCMP(id, tca->id, idSz))
  2176. tca = tca->next;
  2177. return tca;
  2178. }
  2179. #endif /* NO_WOLFSSL_SERVER */
  2180. /** Parses a buffer of TCA extensions. */
  2181. static int TLSX_TCA_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2182. byte isRequest)
  2183. {
  2184. #ifndef NO_WOLFSSL_SERVER
  2185. word16 size = 0;
  2186. word16 offset = 0;
  2187. #endif
  2188. TLSX *extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
  2189. if (!extension)
  2190. extension = TLSX_Find(ssl->ctx->extensions, TLSX_TRUSTED_CA_KEYS);
  2191. if (!isRequest) {
  2192. #ifndef NO_WOLFSSL_CLIENT
  2193. if (!extension || !extension->data)
  2194. return TLSX_HandleUnsupportedExtension(ssl);
  2195. if (length > 0)
  2196. return BUFFER_ERROR; /* TCA response MUST be empty. */
  2197. /* Set the flag that we're good for keys */
  2198. TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
  2199. return 0;
  2200. #endif
  2201. }
  2202. #ifndef NO_WOLFSSL_SERVER
  2203. if (!extension || !extension->data) {
  2204. /* Skipping, TCA not enabled at server side. */
  2205. return 0;
  2206. }
  2207. if (OPAQUE16_LEN > length)
  2208. return BUFFER_ERROR;
  2209. ato16(input, &size);
  2210. offset += OPAQUE16_LEN;
  2211. /* validating tca list length */
  2212. if (length != OPAQUE16_LEN + size)
  2213. return BUFFER_ERROR;
  2214. for (size = 0; offset < length; offset += size) {
  2215. TCA *tca = NULL;
  2216. byte type;
  2217. const byte* id = NULL;
  2218. word16 idSz = 0;
  2219. if (offset + ENUM_LEN > length)
  2220. return BUFFER_ERROR;
  2221. type = input[offset++];
  2222. switch (type) {
  2223. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2224. break;
  2225. #ifndef NO_SHA
  2226. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2227. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2228. if (offset + WC_SHA_DIGEST_SIZE > length)
  2229. return BUFFER_ERROR;
  2230. idSz = WC_SHA_DIGEST_SIZE;
  2231. id = input + offset;
  2232. offset += idSz;
  2233. break;
  2234. #endif
  2235. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2236. if (offset + OPAQUE16_LEN > length)
  2237. return BUFFER_ERROR;
  2238. ato16(input + offset, &idSz);
  2239. offset += OPAQUE16_LEN;
  2240. if ((offset > length) || (idSz > length - offset))
  2241. return BUFFER_ERROR;
  2242. id = input + offset;
  2243. offset += idSz;
  2244. break;
  2245. default:
  2246. WOLFSSL_ERROR_VERBOSE(TCA_INVALID_ID_TYPE);
  2247. return TCA_INVALID_ID_TYPE;
  2248. }
  2249. /* Find the type/ID in the TCA list. */
  2250. tca = TLSX_TCA_Find((TCA*)extension->data, type, id, idSz);
  2251. if (tca != NULL) {
  2252. /* Found it. Set the response flag and break out of the loop. */
  2253. TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
  2254. break;
  2255. }
  2256. }
  2257. #else
  2258. (void)input;
  2259. #endif
  2260. return 0;
  2261. }
  2262. /* Checks to see if the server sent a response for the TCA. */
  2263. static int TLSX_TCA_VerifyParse(WOLFSSL* ssl, byte isRequest)
  2264. {
  2265. (void)ssl;
  2266. if (!isRequest) {
  2267. #ifndef NO_WOLFSSL_CLIENT
  2268. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
  2269. if (extension && !extension->resp) {
  2270. SendAlert(ssl, alert_fatal, handshake_failure);
  2271. WOLFSSL_ERROR_VERBOSE(TCA_ABSENT_ERROR);
  2272. return TCA_ABSENT_ERROR;
  2273. }
  2274. #endif /* NO_WOLFSSL_CLIENT */
  2275. }
  2276. return 0;
  2277. }
  2278. int TLSX_UseTrustedCA(TLSX** extensions, byte type,
  2279. const byte* id, word16 idSz, void* heap)
  2280. {
  2281. TLSX* extension;
  2282. TCA* tca = NULL;
  2283. if (extensions == NULL)
  2284. return BAD_FUNC_ARG;
  2285. if ((tca = TLSX_TCA_New(type, id, idSz, heap)) == NULL)
  2286. return MEMORY_E;
  2287. extension = TLSX_Find(*extensions, TLSX_TRUSTED_CA_KEYS);
  2288. if (!extension) {
  2289. int ret = TLSX_Push(extensions, TLSX_TRUSTED_CA_KEYS, (void*)tca, heap);
  2290. if (ret != 0) {
  2291. TLSX_TCA_Free(tca, heap);
  2292. return ret;
  2293. }
  2294. }
  2295. else {
  2296. /* push new TCA object to extension data. */
  2297. tca->next = (TCA*)extension->data;
  2298. extension->data = (void*)tca;
  2299. }
  2300. return WOLFSSL_SUCCESS;
  2301. }
  2302. #define TCA_FREE_ALL TLSX_TCA_FreeAll
  2303. #define TCA_GET_SIZE TLSX_TCA_GetSize
  2304. #define TCA_WRITE TLSX_TCA_Write
  2305. #define TCA_PARSE TLSX_TCA_Parse
  2306. #define TCA_VERIFY_PARSE TLSX_TCA_VerifyParse
  2307. #else /* HAVE_TRUSTED_CA */
  2308. #define TCA_FREE_ALL(list, heap)
  2309. #define TCA_GET_SIZE(list) 0
  2310. #define TCA_WRITE(a, b) 0
  2311. #define TCA_PARSE(a, b, c, d) 0
  2312. #define TCA_VERIFY_PARSE(a, b) 0
  2313. #endif /* HAVE_TRUSTED_CA */
  2314. /******************************************************************************/
  2315. /* Max Fragment Length Negotiation */
  2316. /******************************************************************************/
  2317. #ifdef HAVE_MAX_FRAGMENT
  2318. static word16 TLSX_MFL_Write(byte* data, byte* output)
  2319. {
  2320. output[0] = data[0];
  2321. return ENUM_LEN;
  2322. }
  2323. static int TLSX_MFL_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2324. byte isRequest)
  2325. {
  2326. if (length != ENUM_LEN)
  2327. return BUFFER_ERROR;
  2328. #ifdef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
  2329. (void) isRequest;
  2330. #else
  2331. if (!isRequest)
  2332. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_MAX_FRAGMENT_LENGTH))
  2333. return TLSX_HandleUnsupportedExtension(ssl);
  2334. #endif
  2335. switch (*input) {
  2336. case WOLFSSL_MFL_2_8 : ssl->max_fragment = 256; break;
  2337. case WOLFSSL_MFL_2_9 : ssl->max_fragment = 512; break;
  2338. case WOLFSSL_MFL_2_10: ssl->max_fragment = 1024; break;
  2339. case WOLFSSL_MFL_2_11: ssl->max_fragment = 2048; break;
  2340. case WOLFSSL_MFL_2_12: ssl->max_fragment = 4096; break;
  2341. case WOLFSSL_MFL_2_13: ssl->max_fragment = 8192; break;
  2342. default:
  2343. SendAlert(ssl, alert_fatal, illegal_parameter);
  2344. WOLFSSL_ERROR_VERBOSE(UNKNOWN_MAX_FRAG_LEN_E);
  2345. return UNKNOWN_MAX_FRAG_LEN_E;
  2346. }
  2347. #ifndef NO_WOLFSSL_SERVER
  2348. if (isRequest) {
  2349. int ret = TLSX_UseMaxFragment(&ssl->extensions, *input, ssl->heap);
  2350. if (ret != WOLFSSL_SUCCESS)
  2351. return ret; /* throw error */
  2352. TLSX_SetResponse(ssl, TLSX_MAX_FRAGMENT_LENGTH);
  2353. }
  2354. #endif
  2355. return 0;
  2356. }
  2357. int TLSX_UseMaxFragment(TLSX** extensions, byte mfl, void* heap)
  2358. {
  2359. byte* data = NULL;
  2360. int ret = 0;
  2361. if (extensions == NULL || mfl < WOLFSSL_MFL_MIN || mfl > WOLFSSL_MFL_MAX)
  2362. return BAD_FUNC_ARG;
  2363. data = (byte*)XMALLOC(ENUM_LEN, heap, DYNAMIC_TYPE_TLSX);
  2364. if (data == NULL)
  2365. return MEMORY_E;
  2366. data[0] = mfl;
  2367. ret = TLSX_Push(extensions, TLSX_MAX_FRAGMENT_LENGTH, data, heap);
  2368. if (ret != 0) {
  2369. XFREE(data, heap, DYNAMIC_TYPE_TLSX);
  2370. return ret;
  2371. }
  2372. return WOLFSSL_SUCCESS;
  2373. }
  2374. #define MFL_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
  2375. #define MFL_GET_SIZE(data) ENUM_LEN
  2376. #define MFL_WRITE TLSX_MFL_Write
  2377. #define MFL_PARSE TLSX_MFL_Parse
  2378. #else
  2379. #define MFL_FREE_ALL(a, b)
  2380. #define MFL_GET_SIZE(a) 0
  2381. #define MFL_WRITE(a, b) 0
  2382. #define MFL_PARSE(a, b, c, d) 0
  2383. #endif /* HAVE_MAX_FRAGMENT */
  2384. /******************************************************************************/
  2385. /* Truncated HMAC */
  2386. /******************************************************************************/
  2387. #ifdef HAVE_TRUNCATED_HMAC
  2388. static int TLSX_THM_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2389. byte isRequest)
  2390. {
  2391. if (length != 0 || input == NULL)
  2392. return BUFFER_ERROR;
  2393. if (!isRequest) {
  2394. #ifndef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
  2395. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_TRUNCATED_HMAC))
  2396. return TLSX_HandleUnsupportedExtension(ssl);
  2397. #endif
  2398. }
  2399. else {
  2400. #ifndef NO_WOLFSSL_SERVER
  2401. int ret = TLSX_UseTruncatedHMAC(&ssl->extensions, ssl->heap);
  2402. if (ret != WOLFSSL_SUCCESS)
  2403. return ret; /* throw error */
  2404. TLSX_SetResponse(ssl, TLSX_TRUNCATED_HMAC);
  2405. #endif
  2406. }
  2407. ssl->truncated_hmac = 1;
  2408. return 0;
  2409. }
  2410. int TLSX_UseTruncatedHMAC(TLSX** extensions, void* heap)
  2411. {
  2412. int ret = 0;
  2413. if (extensions == NULL)
  2414. return BAD_FUNC_ARG;
  2415. ret = TLSX_Push(extensions, TLSX_TRUNCATED_HMAC, NULL, heap);
  2416. if (ret != 0)
  2417. return ret;
  2418. return WOLFSSL_SUCCESS;
  2419. }
  2420. #define THM_PARSE TLSX_THM_Parse
  2421. #else
  2422. #define THM_PARSE(a, b, c, d) 0
  2423. #endif /* HAVE_TRUNCATED_HMAC */
  2424. /******************************************************************************/
  2425. /* Certificate Status Request */
  2426. /******************************************************************************/
  2427. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  2428. static void TLSX_CSR_Free(CertificateStatusRequest* csr, void* heap)
  2429. {
  2430. switch (csr->status_type) {
  2431. case WOLFSSL_CSR_OCSP:
  2432. FreeOcspRequest(&csr->request.ocsp);
  2433. break;
  2434. }
  2435. XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
  2436. (void)heap;
  2437. }
  2438. static word16 TLSX_CSR_GetSize(CertificateStatusRequest* csr, byte isRequest)
  2439. {
  2440. word16 size = 0;
  2441. /* shut up compiler warnings */
  2442. (void) csr; (void) isRequest;
  2443. #ifndef NO_WOLFSSL_CLIENT
  2444. if (isRequest) {
  2445. switch (csr->status_type) {
  2446. case WOLFSSL_CSR_OCSP:
  2447. size += ENUM_LEN + 2 * OPAQUE16_LEN;
  2448. if (csr->request.ocsp.nonceSz)
  2449. size += OCSP_NONCE_EXT_SZ;
  2450. break;
  2451. }
  2452. }
  2453. #endif
  2454. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  2455. if (!isRequest && csr->ssl->options.tls1_3)
  2456. return OPAQUE8_LEN + OPAQUE24_LEN + csr->response.length;
  2457. #endif
  2458. return size;
  2459. }
  2460. static word16 TLSX_CSR_Write(CertificateStatusRequest* csr, byte* output,
  2461. byte isRequest)
  2462. {
  2463. /* shut up compiler warnings */
  2464. (void) csr; (void) output; (void) isRequest;
  2465. #ifndef NO_WOLFSSL_CLIENT
  2466. if (isRequest) {
  2467. word16 offset = 0;
  2468. word16 length = 0;
  2469. /* type */
  2470. output[offset++] = csr->status_type;
  2471. switch (csr->status_type) {
  2472. case WOLFSSL_CSR_OCSP:
  2473. /* responder id list */
  2474. c16toa(0, output + offset);
  2475. offset += OPAQUE16_LEN;
  2476. /* request extensions */
  2477. if (csr->request.ocsp.nonceSz)
  2478. length = (word16)EncodeOcspRequestExtensions(
  2479. &csr->request.ocsp,
  2480. output + offset + OPAQUE16_LEN,
  2481. OCSP_NONCE_EXT_SZ);
  2482. c16toa(length, output + offset);
  2483. offset += OPAQUE16_LEN + length;
  2484. break;
  2485. }
  2486. return offset;
  2487. }
  2488. #endif
  2489. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  2490. if (!isRequest && csr->ssl->options.tls1_3) {
  2491. word16 offset = 0;
  2492. output[offset++] = csr->status_type;
  2493. c32to24(csr->response.length, output + offset);
  2494. offset += OPAQUE24_LEN;
  2495. XMEMCPY(output + offset, csr->response.buffer, csr->response.length);
  2496. offset += csr->response.length;
  2497. return offset;
  2498. }
  2499. #endif
  2500. return 0;
  2501. }
  2502. static int TLSX_CSR_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2503. byte isRequest)
  2504. {
  2505. int ret;
  2506. #if !defined(NO_WOLFSSL_SERVER)
  2507. byte status_type;
  2508. word16 size = 0;
  2509. #if defined(WOLFSSL_TLS13)
  2510. DecodedCert* cert;
  2511. #endif
  2512. #endif
  2513. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) \
  2514. && defined(WOLFSSL_TLS13)
  2515. OcspRequest* request;
  2516. TLSX* extension;
  2517. CertificateStatusRequest* csr;
  2518. #endif
  2519. #if !defined(NO_WOLFSSL_CLIENT) && defined(WOLFSSL_TLS13) \
  2520. || !defined(NO_WOLFSSL_SERVER)
  2521. word32 offset = 0;
  2522. #endif
  2523. #if !defined(NO_WOLFSSL_CLIENT) && defined(WOLFSSL_TLS13)
  2524. word32 resp_length;
  2525. #endif
  2526. /* shut up compiler warnings */
  2527. (void) ssl; (void) input;
  2528. if (!isRequest) {
  2529. #ifndef NO_WOLFSSL_CLIENT
  2530. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2531. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2532. if (!csr) {
  2533. /* look at context level */
  2534. extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST);
  2535. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2536. if (!csr) /* unexpected extension */
  2537. return TLSX_HandleUnsupportedExtension(ssl);
  2538. /* enable extension at ssl level */
  2539. ret = TLSX_UseCertificateStatusRequest(&ssl->extensions,
  2540. csr->status_type, csr->options, ssl,
  2541. ssl->heap, ssl->devId);
  2542. if (ret != WOLFSSL_SUCCESS)
  2543. return ret;
  2544. switch (csr->status_type) {
  2545. case WOLFSSL_CSR_OCSP:
  2546. /* propagate nonce */
  2547. if (csr->request.ocsp.nonceSz) {
  2548. request =
  2549. (OcspRequest*)TLSX_CSR_GetRequest(ssl->extensions);
  2550. if (request) {
  2551. XMEMCPY(request->nonce, csr->request.ocsp.nonce,
  2552. csr->request.ocsp.nonceSz);
  2553. request->nonceSz = csr->request.ocsp.nonceSz;
  2554. }
  2555. }
  2556. break;
  2557. }
  2558. }
  2559. ssl->status_request = 1;
  2560. #ifdef WOLFSSL_TLS13
  2561. if (ssl->options.tls1_3) {
  2562. /* Get the new extension potentially created above. */
  2563. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2564. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2565. if (csr == NULL)
  2566. return MEMORY_ERROR;
  2567. ret = 0;
  2568. if (OPAQUE8_LEN + OPAQUE24_LEN > length)
  2569. ret = BUFFER_ERROR;
  2570. if (ret == 0 && input[offset++] != WOLFSSL_CSR_OCSP) {
  2571. ret = BAD_CERTIFICATE_STATUS_ERROR;
  2572. WOLFSSL_ERROR_VERBOSE(ret);
  2573. }
  2574. if (ret == 0) {
  2575. c24to32(input + offset, &resp_length);
  2576. offset += OPAQUE24_LEN;
  2577. if (offset + resp_length != length)
  2578. ret = BUFFER_ERROR;
  2579. }
  2580. if (ret == 0) {
  2581. csr->response.buffer = (byte*)(input + offset);
  2582. csr->response.length = resp_length;
  2583. }
  2584. return ret;
  2585. }
  2586. else
  2587. #endif
  2588. {
  2589. /* extension_data MUST be empty. */
  2590. return length ? BUFFER_ERROR : 0;
  2591. }
  2592. #endif
  2593. }
  2594. else {
  2595. #ifndef NO_WOLFSSL_SERVER
  2596. if (length == 0)
  2597. return 0;
  2598. status_type = input[offset++];
  2599. switch (status_type) {
  2600. case WOLFSSL_CSR_OCSP: {
  2601. /* skip responder_id_list */
  2602. if ((int)(length - offset) < OPAQUE16_LEN)
  2603. return BUFFER_ERROR;
  2604. ato16(input + offset, &size);
  2605. offset += OPAQUE16_LEN + size;
  2606. /* skip request_extensions */
  2607. if ((int)(length - offset) < OPAQUE16_LEN)
  2608. return BUFFER_ERROR;
  2609. ato16(input + offset, &size);
  2610. offset += OPAQUE16_LEN + size;
  2611. if (offset > length)
  2612. return BUFFER_ERROR;
  2613. /* is able to send OCSP response? */
  2614. if (SSL_CM(ssl) == NULL || !SSL_CM(ssl)->ocspStaplingEnabled)
  2615. return 0;
  2616. }
  2617. break;
  2618. /* unknown status type */
  2619. default:
  2620. return 0;
  2621. }
  2622. /* if using status_request and already sending it, skip this one */
  2623. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2624. if (ssl->status_request_v2)
  2625. return 0;
  2626. #endif
  2627. /* accept the first good status_type and return */
  2628. ret = TLSX_UseCertificateStatusRequest(&ssl->extensions, status_type,
  2629. 0, ssl, ssl->heap, ssl->devId);
  2630. if (ret != WOLFSSL_SUCCESS)
  2631. return ret; /* throw error */
  2632. #if defined(WOLFSSL_TLS13)
  2633. if (ssl->options.tls1_3) {
  2634. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), ssl->heap,
  2635. DYNAMIC_TYPE_DCERT);
  2636. if (cert == NULL) {
  2637. return MEMORY_E;
  2638. }
  2639. InitDecodedCert(cert, ssl->buffers.certificate->buffer,
  2640. ssl->buffers.certificate->length, ssl->heap);
  2641. ret = ParseCert(cert, CERT_TYPE, 1, SSL_CM(ssl));
  2642. if (ret != 0 ) {
  2643. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2644. return ret;
  2645. }
  2646. ret = TLSX_CSR_InitRequest(ssl->extensions, cert, ssl->heap);
  2647. if (ret != 0 ) {
  2648. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2649. return ret;
  2650. }
  2651. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2652. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2653. csr = extension ?
  2654. (CertificateStatusRequest*)extension->data : NULL;
  2655. if (csr == NULL)
  2656. return MEMORY_ERROR;
  2657. request = &csr->request.ocsp;
  2658. ret = CreateOcspResponse(ssl, &request, &csr->response);
  2659. if (ret != 0)
  2660. return ret;
  2661. if (csr->response.buffer)
  2662. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
  2663. }
  2664. else
  2665. #endif
  2666. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
  2667. ssl->status_request = status_type;
  2668. #endif
  2669. }
  2670. return 0;
  2671. }
  2672. int TLSX_CSR_InitRequest(TLSX* extensions, DecodedCert* cert, void* heap)
  2673. {
  2674. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
  2675. CertificateStatusRequest* csr = extension ?
  2676. (CertificateStatusRequest*)extension->data : NULL;
  2677. int ret = 0;
  2678. if (csr) {
  2679. switch (csr->status_type) {
  2680. case WOLFSSL_CSR_OCSP: {
  2681. byte nonce[MAX_OCSP_NONCE_SZ];
  2682. int nonceSz = csr->request.ocsp.nonceSz;
  2683. /* preserve nonce */
  2684. XMEMCPY(nonce, csr->request.ocsp.nonce, nonceSz);
  2685. if ((ret = InitOcspRequest(&csr->request.ocsp, cert, 0, heap))
  2686. != 0)
  2687. return ret;
  2688. /* restore nonce */
  2689. XMEMCPY(csr->request.ocsp.nonce, nonce, nonceSz);
  2690. csr->request.ocsp.nonceSz = nonceSz;
  2691. }
  2692. break;
  2693. }
  2694. }
  2695. return ret;
  2696. }
  2697. void* TLSX_CSR_GetRequest(TLSX* extensions)
  2698. {
  2699. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
  2700. CertificateStatusRequest* csr = extension ?
  2701. (CertificateStatusRequest*)extension->data : NULL;
  2702. if (csr) {
  2703. switch (csr->status_type) {
  2704. case WOLFSSL_CSR_OCSP:
  2705. return &csr->request.ocsp;
  2706. }
  2707. }
  2708. return NULL;
  2709. }
  2710. int TLSX_CSR_ForceRequest(WOLFSSL* ssl)
  2711. {
  2712. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2713. CertificateStatusRequest* csr = extension ?
  2714. (CertificateStatusRequest*)extension->data : NULL;
  2715. if (csr) {
  2716. switch (csr->status_type) {
  2717. case WOLFSSL_CSR_OCSP:
  2718. if (SSL_CM(ssl)->ocspEnabled) {
  2719. csr->request.ocsp.ssl = ssl;
  2720. return CheckOcspRequest(SSL_CM(ssl)->ocsp,
  2721. &csr->request.ocsp, NULL);
  2722. }
  2723. else {
  2724. WOLFSSL_ERROR_VERBOSE(OCSP_LOOKUP_FAIL);
  2725. return OCSP_LOOKUP_FAIL;
  2726. }
  2727. }
  2728. }
  2729. return 0;
  2730. }
  2731. int TLSX_UseCertificateStatusRequest(TLSX** extensions, byte status_type,
  2732. byte options, WOLFSSL* ssl, void* heap,
  2733. int devId)
  2734. {
  2735. CertificateStatusRequest* csr = NULL;
  2736. int ret = 0;
  2737. if (!extensions || status_type != WOLFSSL_CSR_OCSP)
  2738. return BAD_FUNC_ARG;
  2739. csr = (CertificateStatusRequest*)
  2740. XMALLOC(sizeof(CertificateStatusRequest), heap, DYNAMIC_TYPE_TLSX);
  2741. if (!csr)
  2742. return MEMORY_E;
  2743. ForceZero(csr, sizeof(CertificateStatusRequest));
  2744. csr->status_type = status_type;
  2745. csr->options = options;
  2746. csr->ssl = ssl;
  2747. switch (csr->status_type) {
  2748. case WOLFSSL_CSR_OCSP:
  2749. if (options & WOLFSSL_CSR_OCSP_USE_NONCE) {
  2750. WC_RNG rng;
  2751. #ifndef HAVE_FIPS
  2752. ret = wc_InitRng_ex(&rng, heap, devId);
  2753. #else
  2754. ret = wc_InitRng(&rng);
  2755. (void)devId;
  2756. #endif
  2757. if (ret == 0) {
  2758. if (wc_RNG_GenerateBlock(&rng, csr->request.ocsp.nonce,
  2759. MAX_OCSP_NONCE_SZ) == 0)
  2760. csr->request.ocsp.nonceSz = MAX_OCSP_NONCE_SZ;
  2761. wc_FreeRng(&rng);
  2762. }
  2763. }
  2764. break;
  2765. }
  2766. if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST, csr, heap)) != 0) {
  2767. XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
  2768. return ret;
  2769. }
  2770. return WOLFSSL_SUCCESS;
  2771. }
  2772. #define CSR_FREE_ALL TLSX_CSR_Free
  2773. #define CSR_GET_SIZE TLSX_CSR_GetSize
  2774. #define CSR_WRITE TLSX_CSR_Write
  2775. #define CSR_PARSE TLSX_CSR_Parse
  2776. #else
  2777. #define CSR_FREE_ALL(data, heap)
  2778. #define CSR_GET_SIZE(a, b) 0
  2779. #define CSR_WRITE(a, b, c) 0
  2780. #define CSR_PARSE(a, b, c, d) 0
  2781. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
  2782. /******************************************************************************/
  2783. /* Certificate Status Request v2 */
  2784. /******************************************************************************/
  2785. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2786. static void TLSX_CSR2_FreeAll(CertificateStatusRequestItemV2* csr2, void* heap)
  2787. {
  2788. CertificateStatusRequestItemV2* next;
  2789. for (; csr2; csr2 = next) {
  2790. next = csr2->next;
  2791. switch (csr2->status_type) {
  2792. case WOLFSSL_CSR2_OCSP:
  2793. case WOLFSSL_CSR2_OCSP_MULTI:
  2794. while(csr2->requests--)
  2795. FreeOcspRequest(&csr2->request.ocsp[csr2->requests]);
  2796. break;
  2797. }
  2798. XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
  2799. }
  2800. (void)heap;
  2801. }
  2802. static word16 TLSX_CSR2_GetSize(CertificateStatusRequestItemV2* csr2,
  2803. byte isRequest)
  2804. {
  2805. word16 size = 0;
  2806. /* shut up compiler warnings */
  2807. (void) csr2; (void) isRequest;
  2808. #ifndef NO_WOLFSSL_CLIENT
  2809. if (isRequest) {
  2810. CertificateStatusRequestItemV2* next;
  2811. for (size = OPAQUE16_LEN; csr2; csr2 = next) {
  2812. next = csr2->next;
  2813. switch (csr2->status_type) {
  2814. case WOLFSSL_CSR2_OCSP:
  2815. case WOLFSSL_CSR2_OCSP_MULTI:
  2816. size += ENUM_LEN + 3 * OPAQUE16_LEN;
  2817. if (csr2->request.ocsp[0].nonceSz)
  2818. size += OCSP_NONCE_EXT_SZ;
  2819. break;
  2820. }
  2821. }
  2822. }
  2823. #endif
  2824. return size;
  2825. }
  2826. static word16 TLSX_CSR2_Write(CertificateStatusRequestItemV2* csr2,
  2827. byte* output, byte isRequest)
  2828. {
  2829. /* shut up compiler warnings */
  2830. (void) csr2; (void) output; (void) isRequest;
  2831. #ifndef NO_WOLFSSL_CLIENT
  2832. if (isRequest) {
  2833. word16 offset;
  2834. word16 length;
  2835. for (offset = OPAQUE16_LEN; csr2 != NULL; csr2 = csr2->next) {
  2836. /* status_type */
  2837. output[offset++] = csr2->status_type;
  2838. /* request */
  2839. switch (csr2->status_type) {
  2840. case WOLFSSL_CSR2_OCSP:
  2841. case WOLFSSL_CSR2_OCSP_MULTI:
  2842. /* request_length */
  2843. length = 2 * OPAQUE16_LEN;
  2844. if (csr2->request.ocsp[0].nonceSz)
  2845. length += OCSP_NONCE_EXT_SZ;
  2846. c16toa(length, output + offset);
  2847. offset += OPAQUE16_LEN;
  2848. /* responder id list */
  2849. c16toa(0, output + offset);
  2850. offset += OPAQUE16_LEN;
  2851. /* request extensions */
  2852. length = 0;
  2853. if (csr2->request.ocsp[0].nonceSz)
  2854. length = (word16)EncodeOcspRequestExtensions(
  2855. &csr2->request.ocsp[0],
  2856. output + offset + OPAQUE16_LEN,
  2857. OCSP_NONCE_EXT_SZ);
  2858. c16toa(length, output + offset);
  2859. offset += OPAQUE16_LEN + length;
  2860. break;
  2861. }
  2862. }
  2863. /* list size */
  2864. c16toa(offset - OPAQUE16_LEN, output);
  2865. return offset;
  2866. }
  2867. #endif
  2868. return 0;
  2869. }
  2870. static int TLSX_CSR2_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2871. byte isRequest)
  2872. {
  2873. int ret;
  2874. /* shut up compiler warnings */
  2875. (void) ssl; (void) input;
  2876. if (!isRequest) {
  2877. #ifndef NO_WOLFSSL_CLIENT
  2878. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
  2879. CertificateStatusRequestItemV2* csr2 = extension ?
  2880. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2881. if (!csr2) {
  2882. /* look at context level */
  2883. extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST_V2);
  2884. csr2 = extension ?
  2885. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2886. if (!csr2) /* unexpected extension */
  2887. return TLSX_HandleUnsupportedExtension(ssl);
  2888. /* enable extension at ssl level */
  2889. for (; csr2; csr2 = csr2->next) {
  2890. ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
  2891. csr2->status_type, csr2->options, ssl->heap,
  2892. ssl->devId);
  2893. if (ret != WOLFSSL_SUCCESS)
  2894. return ret;
  2895. switch (csr2->status_type) {
  2896. case WOLFSSL_CSR2_OCSP:
  2897. /* followed by */
  2898. case WOLFSSL_CSR2_OCSP_MULTI:
  2899. /* propagate nonce */
  2900. if (csr2->request.ocsp[0].nonceSz) {
  2901. OcspRequest* request =
  2902. (OcspRequest*)TLSX_CSR2_GetRequest(ssl->extensions,
  2903. csr2->status_type, 0);
  2904. if (request) {
  2905. XMEMCPY(request->nonce,
  2906. csr2->request.ocsp[0].nonce,
  2907. csr2->request.ocsp[0].nonceSz);
  2908. request->nonceSz =
  2909. csr2->request.ocsp[0].nonceSz;
  2910. }
  2911. }
  2912. break;
  2913. }
  2914. }
  2915. }
  2916. ssl->status_request_v2 = 1;
  2917. return length ? BUFFER_ERROR : 0; /* extension_data MUST be empty. */
  2918. #endif
  2919. }
  2920. else {
  2921. #ifndef NO_WOLFSSL_SERVER
  2922. byte status_type;
  2923. word16 request_length;
  2924. word16 offset = 0;
  2925. word16 size = 0;
  2926. /* list size */
  2927. if (offset + OPAQUE16_LEN >= length) {
  2928. return BUFFER_E;
  2929. }
  2930. ato16(input + offset, &request_length);
  2931. offset += OPAQUE16_LEN;
  2932. if (length - OPAQUE16_LEN != request_length)
  2933. return BUFFER_ERROR;
  2934. while (length > offset) {
  2935. if ((int)(length - offset) < ENUM_LEN + OPAQUE16_LEN)
  2936. return BUFFER_ERROR;
  2937. status_type = input[offset++];
  2938. ato16(input + offset, &request_length);
  2939. offset += OPAQUE16_LEN;
  2940. if (length - offset < request_length)
  2941. return BUFFER_ERROR;
  2942. switch (status_type) {
  2943. case WOLFSSL_CSR2_OCSP:
  2944. case WOLFSSL_CSR2_OCSP_MULTI:
  2945. /* skip responder_id_list */
  2946. if ((int)(length - offset) < OPAQUE16_LEN)
  2947. return BUFFER_ERROR;
  2948. ato16(input + offset, &size);
  2949. if (length - offset < size)
  2950. return BUFFER_ERROR;
  2951. offset += OPAQUE16_LEN + size;
  2952. /* skip request_extensions */
  2953. if ((int)(length - offset) < OPAQUE16_LEN)
  2954. return BUFFER_ERROR;
  2955. ato16(input + offset, &size);
  2956. if (length - offset < size)
  2957. return BUFFER_ERROR;
  2958. offset += OPAQUE16_LEN + size;
  2959. if (offset > length)
  2960. return BUFFER_ERROR;
  2961. /* is able to send OCSP response? */
  2962. if (SSL_CM(ssl) == NULL
  2963. || !SSL_CM(ssl)->ocspStaplingEnabled)
  2964. continue;
  2965. break;
  2966. default:
  2967. /* unknown status type, skipping! */
  2968. offset += request_length;
  2969. continue;
  2970. }
  2971. /* if using status_request and already sending it, remove it
  2972. * and prefer to use the v2 version */
  2973. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  2974. if (ssl->status_request) {
  2975. ssl->status_request = 0;
  2976. TLSX_Remove(&ssl->extensions, TLSX_STATUS_REQUEST, ssl->heap);
  2977. }
  2978. #endif
  2979. /* TLS 1.3 servers MUST NOT act upon presence or information in
  2980. * this extension (RFC 8448 Section 4.4.2.1).
  2981. */
  2982. if (!IsAtLeastTLSv1_3(ssl->version)) {
  2983. /* accept the first good status_type and return */
  2984. ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
  2985. status_type, 0, ssl->heap, ssl->devId);
  2986. if (ret != WOLFSSL_SUCCESS)
  2987. return ret; /* throw error */
  2988. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST_V2);
  2989. ssl->status_request_v2 = status_type;
  2990. }
  2991. return 0;
  2992. }
  2993. #endif
  2994. }
  2995. return 0;
  2996. }
  2997. int TLSX_CSR2_InitRequests(TLSX* extensions, DecodedCert* cert, byte isPeer,
  2998. void* heap)
  2999. {
  3000. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
  3001. CertificateStatusRequestItemV2* csr2 = extension ?
  3002. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3003. int ret = 0;
  3004. for (; csr2; csr2 = csr2->next) {
  3005. switch (csr2->status_type) {
  3006. case WOLFSSL_CSR2_OCSP:
  3007. if (!isPeer || csr2->requests != 0)
  3008. break;
  3009. FALL_THROUGH; /* followed by */
  3010. case WOLFSSL_CSR2_OCSP_MULTI: {
  3011. if (csr2->requests < 1 + MAX_CHAIN_DEPTH) {
  3012. byte nonce[MAX_OCSP_NONCE_SZ];
  3013. int nonceSz = csr2->request.ocsp[0].nonceSz;
  3014. /* preserve nonce, replicating nonce of ocsp[0] */
  3015. XMEMCPY(nonce, csr2->request.ocsp[0].nonce, nonceSz);
  3016. if ((ret = InitOcspRequest(
  3017. &csr2->request.ocsp[csr2->requests], cert,
  3018. 0, heap)) != 0)
  3019. return ret;
  3020. /* restore nonce */
  3021. XMEMCPY(csr2->request.ocsp[csr2->requests].nonce,
  3022. nonce, nonceSz);
  3023. csr2->request.ocsp[csr2->requests].nonceSz = nonceSz;
  3024. csr2->requests++;
  3025. }
  3026. }
  3027. break;
  3028. }
  3029. }
  3030. (void)cert;
  3031. return ret;
  3032. }
  3033. void* TLSX_CSR2_GetRequest(TLSX* extensions, byte status_type, byte idx)
  3034. {
  3035. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
  3036. CertificateStatusRequestItemV2* csr2 = extension ?
  3037. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3038. for (; csr2; csr2 = csr2->next) {
  3039. if (csr2->status_type == status_type) {
  3040. switch (csr2->status_type) {
  3041. case WOLFSSL_CSR2_OCSP:
  3042. /* followed by */
  3043. case WOLFSSL_CSR2_OCSP_MULTI:
  3044. /* requests are initialized in the reverse order */
  3045. return idx < csr2->requests
  3046. ? &csr2->request.ocsp[csr2->requests - idx - 1]
  3047. : NULL;
  3048. }
  3049. }
  3050. }
  3051. return NULL;
  3052. }
  3053. int TLSX_CSR2_ForceRequest(WOLFSSL* ssl)
  3054. {
  3055. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
  3056. CertificateStatusRequestItemV2* csr2 = extension ?
  3057. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3058. /* forces only the first one */
  3059. if (csr2) {
  3060. switch (csr2->status_type) {
  3061. case WOLFSSL_CSR2_OCSP:
  3062. /* followed by */
  3063. case WOLFSSL_CSR2_OCSP_MULTI:
  3064. if (SSL_CM(ssl)->ocspEnabled) {
  3065. csr2->request.ocsp[0].ssl = ssl;
  3066. return CheckOcspRequest(SSL_CM(ssl)->ocsp,
  3067. &csr2->request.ocsp[0], NULL);
  3068. }
  3069. else {
  3070. WOLFSSL_ERROR_VERBOSE(OCSP_LOOKUP_FAIL);
  3071. return OCSP_LOOKUP_FAIL;
  3072. }
  3073. }
  3074. }
  3075. return 0;
  3076. }
  3077. int TLSX_UseCertificateStatusRequestV2(TLSX** extensions, byte status_type,
  3078. byte options, void* heap, int devId)
  3079. {
  3080. TLSX* extension = NULL;
  3081. CertificateStatusRequestItemV2* csr2 = NULL;
  3082. int ret = 0;
  3083. if (!extensions)
  3084. return BAD_FUNC_ARG;
  3085. if (status_type != WOLFSSL_CSR2_OCSP
  3086. && status_type != WOLFSSL_CSR2_OCSP_MULTI)
  3087. return BAD_FUNC_ARG;
  3088. csr2 = (CertificateStatusRequestItemV2*)
  3089. XMALLOC(sizeof(CertificateStatusRequestItemV2), heap, DYNAMIC_TYPE_TLSX);
  3090. if (!csr2)
  3091. return MEMORY_E;
  3092. ForceZero(csr2, sizeof(CertificateStatusRequestItemV2));
  3093. csr2->status_type = status_type;
  3094. csr2->options = options;
  3095. csr2->next = NULL;
  3096. switch (csr2->status_type) {
  3097. case WOLFSSL_CSR2_OCSP:
  3098. case WOLFSSL_CSR2_OCSP_MULTI:
  3099. if (options & WOLFSSL_CSR2_OCSP_USE_NONCE) {
  3100. WC_RNG rng;
  3101. #ifndef HAVE_FIPS
  3102. ret = wc_InitRng_ex(&rng, heap, devId);
  3103. #else
  3104. ret = wc_InitRng(&rng);
  3105. (void)devId;
  3106. #endif
  3107. if (ret == 0) {
  3108. if (wc_RNG_GenerateBlock(&rng, csr2->request.ocsp[0].nonce,
  3109. MAX_OCSP_NONCE_SZ) == 0)
  3110. csr2->request.ocsp[0].nonceSz = MAX_OCSP_NONCE_SZ;
  3111. wc_FreeRng(&rng);
  3112. }
  3113. }
  3114. break;
  3115. }
  3116. /* append new item */
  3117. if ((extension = TLSX_Find(*extensions, TLSX_STATUS_REQUEST_V2))) {
  3118. CertificateStatusRequestItemV2* last =
  3119. (CertificateStatusRequestItemV2*)extension->data;
  3120. for (; last->next; last = last->next);
  3121. last->next = csr2;
  3122. }
  3123. else if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST_V2, csr2,heap))) {
  3124. XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
  3125. return ret;
  3126. }
  3127. return WOLFSSL_SUCCESS;
  3128. }
  3129. #define CSR2_FREE_ALL TLSX_CSR2_FreeAll
  3130. #define CSR2_GET_SIZE TLSX_CSR2_GetSize
  3131. #define CSR2_WRITE TLSX_CSR2_Write
  3132. #define CSR2_PARSE TLSX_CSR2_Parse
  3133. #else
  3134. #define CSR2_FREE_ALL(data, heap)
  3135. #define CSR2_GET_SIZE(a, b) 0
  3136. #define CSR2_WRITE(a, b, c) 0
  3137. #define CSR2_PARSE(a, b, c, d) 0
  3138. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
  3139. /******************************************************************************/
  3140. /* Supported Elliptic Curves */
  3141. /******************************************************************************/
  3142. #ifdef HAVE_SUPPORTED_CURVES
  3143. #if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && !defined(HAVE_CURVE448) \
  3144. && !defined(HAVE_FFDHE) && !defined(HAVE_PQC)
  3145. #error Elliptic Curves Extension requires Elliptic Curve Cryptography or liboqs groups. \
  3146. Use --enable-ecc and/or --enable-liboqs in the configure script or \
  3147. define HAVE_ECC. Alternatively use FFDHE for DH ciphersuites.
  3148. #endif
  3149. static int TLSX_SupportedCurve_New(SupportedCurve** curve, word16 name,
  3150. void* heap)
  3151. {
  3152. if (curve == NULL)
  3153. return BAD_FUNC_ARG;
  3154. (void)heap;
  3155. *curve = (SupportedCurve*)XMALLOC(sizeof(SupportedCurve), heap,
  3156. DYNAMIC_TYPE_TLSX);
  3157. if (*curve == NULL)
  3158. return MEMORY_E;
  3159. (*curve)->name = name;
  3160. (*curve)->next = NULL;
  3161. return 0;
  3162. }
  3163. static int TLSX_PointFormat_New(PointFormat** point, byte format, void* heap)
  3164. {
  3165. if (point == NULL)
  3166. return BAD_FUNC_ARG;
  3167. (void)heap;
  3168. *point = (PointFormat*)XMALLOC(sizeof(PointFormat), heap,
  3169. DYNAMIC_TYPE_TLSX);
  3170. if (*point == NULL)
  3171. return MEMORY_E;
  3172. (*point)->format = format;
  3173. (*point)->next = NULL;
  3174. return 0;
  3175. }
  3176. static void TLSX_SupportedCurve_FreeAll(SupportedCurve* list, void* heap)
  3177. {
  3178. SupportedCurve* curve;
  3179. while ((curve = list)) {
  3180. list = curve->next;
  3181. XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
  3182. }
  3183. (void)heap;
  3184. }
  3185. static void TLSX_PointFormat_FreeAll(PointFormat* list, void* heap)
  3186. {
  3187. PointFormat* point;
  3188. while ((point = list)) {
  3189. list = point->next;
  3190. XFREE(point, heap, DYNAMIC_TYPE_TLSX);
  3191. }
  3192. (void)heap;
  3193. }
  3194. static int TLSX_SupportedCurve_Append(SupportedCurve* list, word16 name,
  3195. void* heap)
  3196. {
  3197. int ret = BAD_FUNC_ARG;
  3198. while (list) {
  3199. if (list->name == name) {
  3200. ret = 0; /* curve already in use */
  3201. break;
  3202. }
  3203. if (list->next == NULL) {
  3204. ret = TLSX_SupportedCurve_New(&list->next, name, heap);
  3205. break;
  3206. }
  3207. list = list->next;
  3208. }
  3209. return ret;
  3210. }
  3211. static int TLSX_PointFormat_Append(PointFormat* list, byte format, void* heap)
  3212. {
  3213. int ret = BAD_FUNC_ARG;
  3214. while (list) {
  3215. if (list->format == format) {
  3216. ret = 0; /* format already in use */
  3217. break;
  3218. }
  3219. if (list->next == NULL) {
  3220. ret = TLSX_PointFormat_New(&list->next, format, heap);
  3221. break;
  3222. }
  3223. list = list->next;
  3224. }
  3225. return ret;
  3226. }
  3227. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  3228. #if defined(HAVE_FFDHE) && (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3229. defined(HAVE_CURVE448))
  3230. static void TLSX_SupportedCurve_ValidateRequest(const WOLFSSL* ssl,
  3231. const byte* semaphore)
  3232. {
  3233. /* If all pre-defined parameter types for key exchange are supported then
  3234. * always send SupportedGroups extension.
  3235. */
  3236. (void)ssl;
  3237. (void)semaphore;
  3238. }
  3239. #else
  3240. static void TLSX_SupportedCurve_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
  3241. {
  3242. word16 i;
  3243. for (i = 0; i < ssl->suites->suiteSz; i += 2) {
  3244. if (ssl->suites->suites[i] == TLS13_BYTE)
  3245. return;
  3246. if ((ssl->suites->suites[i] == ECC_BYTE) ||
  3247. (ssl->suites->suites[i] == ECDHE_PSK_BYTE) ||
  3248. (ssl->suites->suites[i] == CHACHA_BYTE)) {
  3249. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3250. defined(HAVE_CURVE448)
  3251. return;
  3252. #endif
  3253. }
  3254. #ifdef HAVE_FFDHE
  3255. else {
  3256. return;
  3257. }
  3258. #endif
  3259. }
  3260. /* turns semaphore on to avoid sending this extension. */
  3261. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_GROUPS));
  3262. }
  3263. #endif
  3264. /* Only send PointFormats if TLSv13, ECC or CHACHA cipher suite present.
  3265. */
  3266. static void TLSX_PointFormat_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
  3267. {
  3268. word16 i;
  3269. for (i = 0; i < ssl->suites->suiteSz; i += 2) {
  3270. if (ssl->suites->suites[i] == TLS13_BYTE)
  3271. return;
  3272. if ((ssl->suites->suites[i] == ECC_BYTE) ||
  3273. (ssl->suites->suites[i] == ECDHE_PSK_BYTE) ||
  3274. (ssl->suites->suites[i] == CHACHA_BYTE)) {
  3275. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3276. defined(HAVE_CURVE448)
  3277. return;
  3278. #endif
  3279. }
  3280. }
  3281. #ifdef HAVE_FFDHE
  3282. (void)semaphore;
  3283. return;
  3284. #else
  3285. /* turns semaphore on to avoid sending this extension. */
  3286. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  3287. #endif
  3288. }
  3289. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  3290. #ifndef NO_WOLFSSL_SERVER
  3291. static void TLSX_PointFormat_ValidateResponse(WOLFSSL* ssl, byte* semaphore)
  3292. {
  3293. #if defined(HAVE_FFDHE) || defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3294. defined(HAVE_CURVE448)
  3295. (void)semaphore;
  3296. #endif
  3297. if (ssl->options.cipherSuite0 == TLS13_BYTE)
  3298. return;
  3299. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  3300. if (ssl->options.cipherSuite0 == ECC_BYTE ||
  3301. ssl->options.cipherSuite0 == ECDHE_PSK_BYTE ||
  3302. ssl->options.cipherSuite0 == CHACHA_BYTE) {
  3303. return;
  3304. }
  3305. #endif
  3306. /* turns semaphore on to avoid sending this extension. */
  3307. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  3308. }
  3309. #endif /* !NO_WOLFSSL_SERVER */
  3310. #ifndef NO_WOLFSSL_CLIENT
  3311. static word16 TLSX_SupportedCurve_GetSize(SupportedCurve* list)
  3312. {
  3313. SupportedCurve* curve;
  3314. word16 length = OPAQUE16_LEN; /* list length */
  3315. while ((curve = list)) {
  3316. list = curve->next;
  3317. length += OPAQUE16_LEN; /* curve length */
  3318. }
  3319. return length;
  3320. }
  3321. #endif
  3322. static word16 TLSX_PointFormat_GetSize(PointFormat* list)
  3323. {
  3324. PointFormat* point;
  3325. word16 length = ENUM_LEN; /* list length */
  3326. while ((point = list)) {
  3327. list = point->next;
  3328. length += ENUM_LEN; /* format length */
  3329. }
  3330. return length;
  3331. }
  3332. #ifndef NO_WOLFSSL_CLIENT
  3333. static word16 TLSX_SupportedCurve_Write(SupportedCurve* list, byte* output)
  3334. {
  3335. word16 offset = OPAQUE16_LEN;
  3336. while (list) {
  3337. c16toa(list->name, output + offset);
  3338. offset += OPAQUE16_LEN;
  3339. list = list->next;
  3340. }
  3341. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  3342. return offset;
  3343. }
  3344. #endif
  3345. static word16 TLSX_PointFormat_Write(PointFormat* list, byte* output)
  3346. {
  3347. word16 offset = ENUM_LEN;
  3348. while (list) {
  3349. output[offset++] = list->format;
  3350. list = list->next;
  3351. }
  3352. output[0] = (byte)(offset - ENUM_LEN);
  3353. return offset;
  3354. }
  3355. #if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
  3356. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
  3357. static int TLSX_SupportedCurve_Parse(WOLFSSL* ssl, const byte* input,
  3358. word16 length, byte isRequest)
  3359. {
  3360. word16 offset;
  3361. word16 name;
  3362. int ret;
  3363. if(!isRequest && !IsAtLeastTLSv1_3(ssl->version)) {
  3364. #ifdef WOLFSSL_ALLOW_SERVER_SC_EXT
  3365. return 0;
  3366. #else
  3367. return BUFFER_ERROR; /* servers doesn't send this extension. */
  3368. #endif
  3369. }
  3370. if (OPAQUE16_LEN > length || length % OPAQUE16_LEN)
  3371. return BUFFER_ERROR;
  3372. ato16(input, &offset);
  3373. /* validating curve list length */
  3374. if (length != OPAQUE16_LEN + offset)
  3375. return BUFFER_ERROR;
  3376. offset = OPAQUE16_LEN;
  3377. if (offset == length)
  3378. return 0;
  3379. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3380. if (!isRequest) {
  3381. TLSX* extension;
  3382. SupportedCurve* curve;
  3383. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3384. if (extension != NULL) {
  3385. /* Replace client list with server list of supported groups. */
  3386. curve = (SupportedCurve*)extension->data;
  3387. extension->data = NULL;
  3388. TLSX_SupportedCurve_FreeAll(curve, ssl->heap);
  3389. ato16(input + offset, &name);
  3390. offset += OPAQUE16_LEN;
  3391. ret = TLSX_SupportedCurve_New(&curve, name, ssl->heap);
  3392. if (ret != 0)
  3393. return ret; /* throw error */
  3394. extension->data = (void*)curve;
  3395. }
  3396. }
  3397. #endif
  3398. for (; offset < length; offset += OPAQUE16_LEN) {
  3399. ato16(input + offset, &name);
  3400. ret = TLSX_UseSupportedCurve(&ssl->extensions, name, ssl->heap);
  3401. /* If it is BAD_FUNC_ARG then it is a group we do not support, but
  3402. * that is fine. */
  3403. if (ret != WOLFSSL_SUCCESS && ret != BAD_FUNC_ARG) {
  3404. return ret;
  3405. }
  3406. }
  3407. return 0;
  3408. }
  3409. #endif
  3410. #if !defined(NO_WOLFSSL_SERVER)
  3411. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3412. /* Checks the priority of the groups on the server and set the supported groups
  3413. * response if there is a group not advertised by the client that is preferred.
  3414. *
  3415. * ssl SSL/TLS object.
  3416. * returns 0 on success, otherwise an error.
  3417. */
  3418. int TLSX_SupportedCurve_CheckPriority(WOLFSSL* ssl)
  3419. {
  3420. int ret;
  3421. TLSX* extension;
  3422. TLSX* priority = NULL;
  3423. TLSX* ext = NULL;
  3424. word16 name;
  3425. SupportedCurve* curve;
  3426. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3427. /* May be doing PSK with no key exchange. */
  3428. if (extension == NULL)
  3429. return 0;
  3430. ret = TLSX_PopulateSupportedGroups(ssl, &priority);
  3431. if (ret != WOLFSSL_SUCCESS) {
  3432. TLSX_FreeAll(priority, ssl->heap);
  3433. return ret;
  3434. }
  3435. ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
  3436. if (ext == NULL) {
  3437. WOLFSSL_MSG("Could not find supported groups extension");
  3438. TLSX_FreeAll(priority, ssl->heap);
  3439. return 0;
  3440. }
  3441. curve = (SupportedCurve*)ext->data;
  3442. name = curve->name;
  3443. curve = (SupportedCurve*)extension->data;
  3444. while (curve != NULL) {
  3445. if (curve->name == name)
  3446. break;
  3447. curve = curve->next;
  3448. }
  3449. if (curve == NULL) {
  3450. /* Couldn't find the preferred group in client list. */
  3451. extension->resp = 1;
  3452. /* Send server list back and free client list. */
  3453. curve = (SupportedCurve*)extension->data;
  3454. extension->data = ext->data;
  3455. ext->data = curve;
  3456. }
  3457. TLSX_FreeAll(priority, ssl->heap);
  3458. return 0;
  3459. }
  3460. #endif /* WOLFSSL_TLS13 && !WOLFSSL_NO_SERVER_GROUPS_EXT */
  3461. #if defined(HAVE_FFDHE) && !defined(WOLFSSL_NO_TLS12)
  3462. #ifdef HAVE_PUBLIC_FFDHE
  3463. static int tlsx_ffdhe_find_group(WOLFSSL* ssl, SupportedCurve* clientGroup,
  3464. SupportedCurve* serverGroup)
  3465. {
  3466. int ret = 0;
  3467. SupportedCurve* group;
  3468. const DhParams* params = NULL;
  3469. for (; serverGroup != NULL; serverGroup = serverGroup->next) {
  3470. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(serverGroup->name))
  3471. continue;
  3472. for (group = clientGroup; group != NULL; group = group->next) {
  3473. if (serverGroup->name != group->name)
  3474. continue;
  3475. switch (serverGroup->name) {
  3476. #ifdef HAVE_FFDHE_2048
  3477. case WOLFSSL_FFDHE_2048:
  3478. params = wc_Dh_ffdhe2048_Get();
  3479. break;
  3480. #endif
  3481. #ifdef HAVE_FFDHE_3072
  3482. case WOLFSSL_FFDHE_3072:
  3483. params = wc_Dh_ffdhe3072_Get();
  3484. break;
  3485. #endif
  3486. #ifdef HAVE_FFDHE_4096
  3487. case WOLFSSL_FFDHE_4096:
  3488. params = wc_Dh_ffdhe4096_Get();
  3489. break;
  3490. #endif
  3491. #ifdef HAVE_FFDHE_6144
  3492. case WOLFSSL_FFDHE_6144:
  3493. params = wc_Dh_ffdhe6144_Get();
  3494. break;
  3495. #endif
  3496. #ifdef HAVE_FFDHE_8192
  3497. case WOLFSSL_FFDHE_8192:
  3498. params = wc_Dh_ffdhe8192_Get();
  3499. break;
  3500. #endif
  3501. default:
  3502. break;
  3503. }
  3504. if (params == NULL) {
  3505. ret = BAD_FUNC_ARG;
  3506. break;
  3507. }
  3508. if (params->p_len >= ssl->options.minDhKeySz &&
  3509. params->p_len <= ssl->options.maxDhKeySz) {
  3510. break;
  3511. }
  3512. }
  3513. if (ret != 0)
  3514. break;
  3515. if ((group != NULL) && (serverGroup->name == group->name))
  3516. break;
  3517. }
  3518. if ((ret == 0) && (serverGroup != NULL) && (params != NULL)) {
  3519. ssl->buffers.serverDH_P.buffer = (unsigned char *)params->p;
  3520. ssl->buffers.serverDH_P.length = params->p_len;
  3521. ssl->buffers.serverDH_G.buffer = (unsigned char *)params->g;
  3522. ssl->buffers.serverDH_G.length = params->g_len;
  3523. ssl->namedGroup = serverGroup->name;
  3524. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3525. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3526. ssl->options.dhDoKeyTest = 0;
  3527. #endif
  3528. ssl->options.haveDH = 1;
  3529. }
  3530. return ret;
  3531. }
  3532. #else
  3533. static int tlsx_ffdhe_find_group(WOLFSSL* ssl, SupportedCurve* clientGroup,
  3534. SupportedCurve* serverGroup)
  3535. {
  3536. int ret = 0;
  3537. SupportedCurve* group;
  3538. word32 p_len;
  3539. for (; serverGroup != NULL; serverGroup = serverGroup->next) {
  3540. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(serverGroup->name))
  3541. continue;
  3542. for (group = clientGroup; group != NULL; group = group->next) {
  3543. if (serverGroup->name != group->name)
  3544. continue;
  3545. wc_DhGetNamedKeyParamSize(serverGroup->name, &p_len, NULL, NULL);
  3546. if (p_len == 0) {
  3547. ret = BAD_FUNC_ARG;
  3548. break;
  3549. }
  3550. if (p_len >= ssl->options.minDhKeySz &&
  3551. p_len <= ssl->options.maxDhKeySz) {
  3552. break;
  3553. }
  3554. }
  3555. if (ret != 0)
  3556. break;
  3557. if ((group != NULL) && (serverGroup->name == group->name))
  3558. break;
  3559. }
  3560. if ((ret == 0) && (serverGroup != NULL)) {
  3561. word32 pSz, gSz;
  3562. ssl->buffers.serverDH_P.buffer = NULL;
  3563. ssl->buffers.serverDH_G.buffer = NULL;
  3564. ret = wc_DhGetNamedKeyParamSize(serverGroup->name, &pSz, &gSz, NULL);
  3565. if (ret == 0) {
  3566. ssl->buffers.serverDH_P.buffer =
  3567. (byte*)XMALLOC(pSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  3568. if (ssl->buffers.serverDH_P.buffer == NULL)
  3569. ret = MEMORY_E;
  3570. else
  3571. ssl->buffers.serverDH_P.length = pSz;
  3572. }
  3573. if (ret == 0) {
  3574. ssl->buffers.serverDH_G.buffer =
  3575. (byte*)XMALLOC(gSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  3576. if (ssl->buffers.serverDH_G.buffer == NULL) {
  3577. ret = MEMORY_E;
  3578. } else
  3579. ssl->buffers.serverDH_G.length = gSz;
  3580. }
  3581. if (ret == 0) {
  3582. ret = wc_DhCopyNamedKey(serverGroup->name,
  3583. ssl->buffers.serverDH_P.buffer, &pSz,
  3584. ssl->buffers.serverDH_G.buffer, &gSz,
  3585. NULL, NULL);
  3586. }
  3587. if (ret == 0) {
  3588. ssl->buffers.weOwnDH = 1;
  3589. ssl->namedGroup = serverGroup->name;
  3590. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3591. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3592. ssl->options.dhDoKeyTest = 0;
  3593. #endif
  3594. ssl->options.haveDH = 1;
  3595. }
  3596. else {
  3597. if (ssl->buffers.serverDH_P.buffer != NULL) {
  3598. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
  3599. DYNAMIC_TYPE_PUBLIC_KEY);
  3600. ssl->buffers.serverDH_P.length = 0;
  3601. ssl->buffers.serverDH_P.buffer = NULL;
  3602. }
  3603. if (ssl->buffers.serverDH_G.buffer != NULL) {
  3604. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
  3605. DYNAMIC_TYPE_PUBLIC_KEY);
  3606. ssl->buffers.serverDH_G.length = 0;
  3607. ssl->buffers.serverDH_G.buffer = NULL;
  3608. }
  3609. }
  3610. }
  3611. return ret;
  3612. }
  3613. #endif
  3614. /* Set the highest priority common FFDHE group on the server as compared to
  3615. * client extensions.
  3616. *
  3617. * ssl SSL/TLS object.
  3618. * returns 0 on success, otherwise an error.
  3619. */
  3620. int TLSX_SupportedFFDHE_Set(WOLFSSL* ssl)
  3621. {
  3622. int ret;
  3623. TLSX* priority = NULL;
  3624. TLSX* ext = NULL;
  3625. TLSX* extension;
  3626. SupportedCurve* clientGroup;
  3627. SupportedCurve* serverGroup;
  3628. SupportedCurve* group;
  3629. int found = 0;
  3630. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3631. /* May be doing PSK with no key exchange. */
  3632. if (extension == NULL)
  3633. return 0;
  3634. clientGroup = (SupportedCurve*)extension->data;
  3635. for (group = clientGroup; group != NULL; group = group->next) {
  3636. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(group->name)) {
  3637. found = 1;
  3638. break;
  3639. }
  3640. }
  3641. if (!found)
  3642. return 0;
  3643. if (ssl->buffers.serverDH_P.buffer && ssl->buffers.weOwnDH) {
  3644. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
  3645. DYNAMIC_TYPE_PUBLIC_KEY);
  3646. }
  3647. if (ssl->buffers.serverDH_G.buffer && ssl->buffers.weOwnDH) {
  3648. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
  3649. DYNAMIC_TYPE_PUBLIC_KEY);
  3650. }
  3651. ssl->buffers.serverDH_P.buffer = NULL;
  3652. ssl->buffers.serverDH_G.buffer = NULL;
  3653. ssl->buffers.weOwnDH = 0;
  3654. ssl->options.haveDH = 0;
  3655. ret = TLSX_PopulateSupportedGroups(ssl, &priority);
  3656. if (ret == WOLFSSL_SUCCESS) {
  3657. ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
  3658. serverGroup = (SupportedCurve*)ext->data;
  3659. ret = tlsx_ffdhe_find_group(ssl, clientGroup, serverGroup);
  3660. }
  3661. TLSX_FreeAll(priority, ssl->heap);
  3662. return ret;
  3663. }
  3664. #endif /* HAVE_FFDHE && !WOLFSSL_NO_TLS12 */
  3665. #endif /* !NO_WOLFSSL_SERVER */
  3666. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3667. /* Return the preferred group.
  3668. *
  3669. * ssl SSL/TLS object.
  3670. * checkSupported Whether to check for the first supported group.
  3671. * returns BAD_FUNC_ARG if no group found, otherwise the group.
  3672. */
  3673. int TLSX_SupportedCurve_Preferred(WOLFSSL* ssl, int checkSupported)
  3674. {
  3675. TLSX* extension;
  3676. SupportedCurve* curve;
  3677. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3678. if (extension == NULL)
  3679. return BAD_FUNC_ARG;
  3680. curve = (SupportedCurve*)extension->data;
  3681. while (curve != NULL) {
  3682. if (!checkSupported || TLSX_KeyShare_IsSupported(curve->name))
  3683. return curve->name;
  3684. curve = curve->next;
  3685. }
  3686. return BAD_FUNC_ARG;
  3687. }
  3688. #endif /* HAVE_SUPPORTED_CURVES */
  3689. #ifndef NO_WOLFSSL_SERVER
  3690. static int TLSX_PointFormat_Parse(WOLFSSL* ssl, const byte* input,
  3691. word16 length, byte isRequest)
  3692. {
  3693. int ret;
  3694. /* validating formats list length */
  3695. if (ENUM_LEN > length || length != (word16)ENUM_LEN + input[0])
  3696. return BUFFER_ERROR;
  3697. if (isRequest) {
  3698. /* adding uncompressed point format to response */
  3699. ret = TLSX_UsePointFormat(&ssl->extensions, WOLFSSL_EC_PF_UNCOMPRESSED,
  3700. ssl->heap);
  3701. if (ret != WOLFSSL_SUCCESS)
  3702. return ret; /* throw error */
  3703. TLSX_SetResponse(ssl, TLSX_EC_POINT_FORMATS);
  3704. }
  3705. return 0;
  3706. }
  3707. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  3708. int TLSX_ValidateSupportedCurves(WOLFSSL* ssl, byte first, byte second) {
  3709. TLSX* extension = NULL;
  3710. SupportedCurve* curve = NULL;
  3711. word32 oid = 0;
  3712. word32 defOid = 0;
  3713. word32 defSz = 80; /* Maximum known curve size is 66. */
  3714. word32 nextOid = 0;
  3715. word32 nextSz = 80; /* Maximum known curve size is 66. */
  3716. word32 currOid = ssl->ecdhCurveOID;
  3717. int ephmSuite = 0;
  3718. word16 octets = 0; /* according to 'ecc_set_type ecc_sets[];' */
  3719. int key = 0; /* validate key */
  3720. (void)oid;
  3721. if (first == CHACHA_BYTE) {
  3722. switch (second) {
  3723. case TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  3724. case TLS_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3725. case TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3726. case TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
  3727. return 1; /* no suite restriction */
  3728. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  3729. case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
  3730. case TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3731. break;
  3732. }
  3733. }
  3734. if (first == ECC_BYTE || first == ECDHE_PSK_BYTE || first == CHACHA_BYTE)
  3735. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3736. if (!extension)
  3737. return 1; /* no suite restriction */
  3738. for (curve = (SupportedCurve*)extension->data;
  3739. curve && !key;
  3740. curve = curve->next) {
  3741. #ifdef OPENSSL_EXTRA
  3742. /* skip if name is not in supported ECC range
  3743. * or disabled by user */
  3744. if (curve->name > WOLFSSL_ECC_MAX ||
  3745. wolfSSL_curve_is_disabled(ssl, curve->name))
  3746. continue;
  3747. #endif
  3748. /* find supported curve */
  3749. switch (curve->name) {
  3750. #ifdef HAVE_ECC
  3751. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  3752. #ifndef NO_ECC_SECP
  3753. case WOLFSSL_ECC_SECP160R1:
  3754. oid = ECC_SECP160R1_OID;
  3755. octets = 20;
  3756. break;
  3757. #endif /* !NO_ECC_SECP */
  3758. #ifdef HAVE_ECC_SECPR2
  3759. case WOLFSSL_ECC_SECP160R2:
  3760. oid = ECC_SECP160R2_OID;
  3761. octets = 20;
  3762. break;
  3763. #endif /* HAVE_ECC_SECPR2 */
  3764. #ifdef HAVE_ECC_KOBLITZ
  3765. case WOLFSSL_ECC_SECP160K1:
  3766. oid = ECC_SECP160K1_OID;
  3767. octets = 20;
  3768. break;
  3769. #endif /* HAVE_ECC_KOBLITZ */
  3770. #endif
  3771. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  3772. #ifndef NO_ECC_SECP
  3773. case WOLFSSL_ECC_SECP192R1:
  3774. oid = ECC_SECP192R1_OID;
  3775. octets = 24;
  3776. break;
  3777. #endif /* !NO_ECC_SECP */
  3778. #ifdef HAVE_ECC_KOBLITZ
  3779. case WOLFSSL_ECC_SECP192K1:
  3780. oid = ECC_SECP192K1_OID;
  3781. octets = 24;
  3782. break;
  3783. #endif /* HAVE_ECC_KOBLITZ */
  3784. #endif
  3785. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  3786. #ifndef NO_ECC_SECP
  3787. case WOLFSSL_ECC_SECP224R1:
  3788. oid = ECC_SECP224R1_OID;
  3789. octets = 28;
  3790. break;
  3791. #endif /* !NO_ECC_SECP */
  3792. #ifdef HAVE_ECC_KOBLITZ
  3793. case WOLFSSL_ECC_SECP224K1:
  3794. oid = ECC_SECP224K1_OID;
  3795. octets = 28;
  3796. break;
  3797. #endif /* HAVE_ECC_KOBLITZ */
  3798. #endif
  3799. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  3800. #ifndef NO_ECC_SECP
  3801. case WOLFSSL_ECC_SECP256R1:
  3802. oid = ECC_SECP256R1_OID;
  3803. octets = 32;
  3804. break;
  3805. #endif /* !NO_ECC_SECP */
  3806. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  3807. #endif
  3808. #if (defined(HAVE_CURVE25519) || defined(HAVE_ED25519)) && ECC_MIN_KEY_SZ <= 256
  3809. case WOLFSSL_ECC_X25519:
  3810. oid = ECC_X25519_OID;
  3811. octets = 32;
  3812. break;
  3813. #endif /* HAVE_CURVE25519 */
  3814. #ifdef HAVE_ECC
  3815. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  3816. #ifdef HAVE_ECC_KOBLITZ
  3817. case WOLFSSL_ECC_SECP256K1:
  3818. oid = ECC_SECP256K1_OID;
  3819. octets = 32;
  3820. break;
  3821. #endif /* HAVE_ECC_KOBLITZ */
  3822. #ifdef HAVE_ECC_BRAINPOOL
  3823. case WOLFSSL_ECC_BRAINPOOLP256R1:
  3824. oid = ECC_BRAINPOOLP256R1_OID;
  3825. octets = 32;
  3826. break;
  3827. #endif /* HAVE_ECC_BRAINPOOL */
  3828. #endif
  3829. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  3830. #ifndef NO_ECC_SECP
  3831. case WOLFSSL_ECC_SECP384R1:
  3832. oid = ECC_SECP384R1_OID;
  3833. octets = 48;
  3834. break;
  3835. #endif /* !NO_ECC_SECP */
  3836. #ifdef HAVE_ECC_BRAINPOOL
  3837. case WOLFSSL_ECC_BRAINPOOLP384R1:
  3838. oid = ECC_BRAINPOOLP384R1_OID;
  3839. octets = 48;
  3840. break;
  3841. #endif /* HAVE_ECC_BRAINPOOL */
  3842. #endif
  3843. #endif
  3844. #if (defined(HAVE_CURVE448) || defined(HAVE_ED448)) && ECC_MIN_KEY_SZ <= 448
  3845. case WOLFSSL_ECC_X448:
  3846. oid = ECC_X448_OID;
  3847. octets = 57;
  3848. break;
  3849. #endif /* HAVE_CURVE448 */
  3850. #ifdef HAVE_ECC
  3851. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  3852. #ifdef HAVE_ECC_BRAINPOOL
  3853. case WOLFSSL_ECC_BRAINPOOLP512R1:
  3854. oid = ECC_BRAINPOOLP512R1_OID;
  3855. octets = 64;
  3856. break;
  3857. #endif /* HAVE_ECC_BRAINPOOL */
  3858. #endif
  3859. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  3860. #ifndef NO_ECC_SECP
  3861. case WOLFSSL_ECC_SECP521R1:
  3862. oid = ECC_SECP521R1_OID;
  3863. octets = 66;
  3864. break;
  3865. #endif /* !NO_ECC_SECP */
  3866. #endif
  3867. #endif
  3868. default: continue; /* unsupported curve */
  3869. }
  3870. #ifdef HAVE_ECC
  3871. /* Set default Oid */
  3872. if (defOid == 0 && ssl->eccTempKeySz <= octets && defSz > octets) {
  3873. defOid = oid;
  3874. defSz = octets;
  3875. }
  3876. /* The eccTempKeySz is the preferred ephemeral key size */
  3877. if (currOid == 0 && ssl->eccTempKeySz == octets)
  3878. currOid = oid;
  3879. if ((nextOid == 0 || nextSz > octets) && ssl->eccTempKeySz <= octets) {
  3880. nextOid = oid;
  3881. nextSz = octets;
  3882. }
  3883. #else
  3884. if (defOid == 0 && defSz > octets) {
  3885. defOid = oid;
  3886. defSz = octets;
  3887. }
  3888. if (currOid == 0)
  3889. currOid = oid;
  3890. if (nextOid == 0 || nextSz > octets) {
  3891. nextOid = oid;
  3892. nextSz = octets;
  3893. }
  3894. #endif
  3895. if (first == ECC_BYTE) {
  3896. switch (second) {
  3897. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  3898. /* ECDHE_ECDSA */
  3899. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:
  3900. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:
  3901. case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA:
  3902. case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA:
  3903. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
  3904. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384:
  3905. case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  3906. case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
  3907. case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
  3908. case TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8:
  3909. key |= ssl->ecdhCurveOID == oid;
  3910. ephmSuite = 1;
  3911. break;
  3912. #ifdef WOLFSSL_STATIC_DH
  3913. /* ECDH_ECDSA */
  3914. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA:
  3915. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA:
  3916. case TLS_ECDH_ECDSA_WITH_RC4_128_SHA:
  3917. case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA:
  3918. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256:
  3919. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384:
  3920. case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256:
  3921. case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384:
  3922. if (oid == ECC_X25519_OID && defOid == oid) {
  3923. defOid = 0;
  3924. defSz = 80;
  3925. }
  3926. if (oid == ECC_X448_OID && defOid == oid) {
  3927. defOid = 0;
  3928. defSz = 80;
  3929. }
  3930. key |= ssl->pkCurveOID == oid;
  3931. break;
  3932. #endif /* WOLFSSL_STATIC_DH */
  3933. #endif /* HAVE_ECC || HAVE_ED25519 || HAVE_ED448 */
  3934. #ifndef NO_RSA
  3935. /* ECDHE_RSA */
  3936. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
  3937. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
  3938. case TLS_ECDHE_RSA_WITH_RC4_128_SHA:
  3939. case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:
  3940. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
  3941. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384:
  3942. case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
  3943. case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
  3944. key |= ssl->ecdhCurveOID == oid;
  3945. ephmSuite = 1;
  3946. break;
  3947. #if defined(HAVE_ECC) && defined(WOLFSSL_STATIC_DH)
  3948. /* ECDH_RSA */
  3949. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA:
  3950. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA:
  3951. case TLS_ECDH_RSA_WITH_RC4_128_SHA:
  3952. case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA:
  3953. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256:
  3954. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384:
  3955. case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256:
  3956. case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384:
  3957. if (oid == ECC_X25519_OID && defOid == oid) {
  3958. defOid = 0;
  3959. defSz = 80;
  3960. }
  3961. if (oid == ECC_X448_OID && defOid == oid) {
  3962. defOid = 0;
  3963. defSz = 80;
  3964. }
  3965. key |= ssl->pkCurveOID == oid;
  3966. break;
  3967. #endif /* HAVE_ECC && WOLFSSL_STATIC_DH */
  3968. #endif
  3969. default:
  3970. if (oid == ECC_X25519_OID && defOid == oid) {
  3971. defOid = 0;
  3972. defSz = 80;
  3973. }
  3974. if (oid == ECC_X448_OID && defOid == oid) {
  3975. defOid = 0;
  3976. defSz = 80;
  3977. }
  3978. key = 1;
  3979. break;
  3980. }
  3981. }
  3982. /* ChaCha20-Poly1305 ECC cipher suites */
  3983. if (first == CHACHA_BYTE) {
  3984. switch (second) {
  3985. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  3986. /* ECDHE_ECDSA */
  3987. case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 :
  3988. case TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
  3989. key |= ssl->ecdhCurveOID == oid;
  3990. ephmSuite = 1;
  3991. break;
  3992. #endif /* HAVE_ECC || HAVE_ED25519 || HAVE_ED448 */
  3993. #ifndef NO_RSA
  3994. /* ECDHE_RSA */
  3995. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 :
  3996. case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
  3997. key |= ssl->ecdhCurveOID == oid;
  3998. ephmSuite = 1;
  3999. break;
  4000. #endif
  4001. default:
  4002. key = 1;
  4003. break;
  4004. }
  4005. }
  4006. }
  4007. /* Choose the default if it is at the required strength. */
  4008. #ifdef HAVE_ECC
  4009. if (ssl->ecdhCurveOID == 0 && defSz == ssl->eccTempKeySz)
  4010. #else
  4011. if (ssl->ecdhCurveOID == 0)
  4012. #endif
  4013. {
  4014. key = 1;
  4015. ssl->ecdhCurveOID = defOid;
  4016. }
  4017. /* Choose any curve at the required strength. */
  4018. if (ssl->ecdhCurveOID == 0) {
  4019. key = 1;
  4020. ssl->ecdhCurveOID = currOid;
  4021. }
  4022. /* Choose the default if it is at the next highest strength. */
  4023. if (ssl->ecdhCurveOID == 0 && defSz == nextSz)
  4024. ssl->ecdhCurveOID = defOid;
  4025. /* Choose any curve at the next highest strength. */
  4026. if (ssl->ecdhCurveOID == 0)
  4027. ssl->ecdhCurveOID = nextOid;
  4028. /* No curve and ephemeral ECC suite requires a matching curve. */
  4029. if (ssl->ecdhCurveOID == 0 && ephmSuite)
  4030. key = 0;
  4031. return key;
  4032. }
  4033. #endif
  4034. #endif /* NO_WOLFSSL_SERVER */
  4035. int TLSX_UseSupportedCurve(TLSX** extensions, word16 name, void* heap)
  4036. {
  4037. TLSX* extension = NULL;
  4038. SupportedCurve* curve = NULL;
  4039. int ret;
  4040. if (extensions == NULL) {
  4041. return BAD_FUNC_ARG;
  4042. }
  4043. #ifdef WOLFSSL_TLS13
  4044. if (! TLSX_KeyShare_IsSupported(name)) {
  4045. return BAD_FUNC_ARG;
  4046. }
  4047. #endif
  4048. extension = TLSX_Find(*extensions, TLSX_SUPPORTED_GROUPS);
  4049. if (!extension) {
  4050. ret = TLSX_SupportedCurve_New(&curve, name, heap);
  4051. if (ret != 0)
  4052. return ret;
  4053. ret = TLSX_Push(extensions, TLSX_SUPPORTED_GROUPS, curve, heap);
  4054. if (ret != 0) {
  4055. XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
  4056. return ret;
  4057. }
  4058. }
  4059. else {
  4060. ret = TLSX_SupportedCurve_Append((SupportedCurve*)extension->data, name,
  4061. heap);
  4062. if (ret != 0)
  4063. return ret;
  4064. }
  4065. return WOLFSSL_SUCCESS;
  4066. }
  4067. int TLSX_UsePointFormat(TLSX** extensions, byte format, void* heap)
  4068. {
  4069. TLSX* extension = NULL;
  4070. PointFormat* point = NULL;
  4071. int ret = 0;
  4072. if (extensions == NULL)
  4073. return BAD_FUNC_ARG;
  4074. extension = TLSX_Find(*extensions, TLSX_EC_POINT_FORMATS);
  4075. if (!extension) {
  4076. ret = TLSX_PointFormat_New(&point, format, heap);
  4077. if (ret != 0)
  4078. return ret;
  4079. ret = TLSX_Push(extensions, TLSX_EC_POINT_FORMATS, point, heap);
  4080. if (ret != 0) {
  4081. XFREE(point, heap, DYNAMIC_TYPE_TLSX);
  4082. return ret;
  4083. }
  4084. }
  4085. else {
  4086. ret = TLSX_PointFormat_Append((PointFormat*)extension->data, format,
  4087. heap);
  4088. if (ret != 0)
  4089. return ret;
  4090. }
  4091. return WOLFSSL_SUCCESS;
  4092. }
  4093. #define EC_FREE_ALL TLSX_SupportedCurve_FreeAll
  4094. #define EC_VALIDATE_REQUEST TLSX_SupportedCurve_ValidateRequest
  4095. #ifndef NO_WOLFSSL_CLIENT
  4096. #define EC_GET_SIZE TLSX_SupportedCurve_GetSize
  4097. #define EC_WRITE TLSX_SupportedCurve_Write
  4098. #else
  4099. #define EC_GET_SIZE(list) 0
  4100. #define EC_WRITE(a, b) 0
  4101. #endif
  4102. #if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
  4103. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
  4104. #define EC_PARSE TLSX_SupportedCurve_Parse
  4105. #else
  4106. #define EC_PARSE(a, b, c, d) 0
  4107. #endif
  4108. #define PF_FREE_ALL TLSX_PointFormat_FreeAll
  4109. #define PF_VALIDATE_REQUEST TLSX_PointFormat_ValidateRequest
  4110. #define PF_VALIDATE_RESPONSE TLSX_PointFormat_ValidateResponse
  4111. #define PF_GET_SIZE TLSX_PointFormat_GetSize
  4112. #define PF_WRITE TLSX_PointFormat_Write
  4113. #ifndef NO_WOLFSSL_SERVER
  4114. #define PF_PARSE TLSX_PointFormat_Parse
  4115. #else
  4116. #define PF_PARSE(a, b, c, d) 0
  4117. #endif
  4118. #else
  4119. #define EC_FREE_ALL(list, heap)
  4120. #define EC_GET_SIZE(list) 0
  4121. #define EC_WRITE(a, b) 0
  4122. #define EC_PARSE(a, b, c, d) 0
  4123. #define EC_VALIDATE_REQUEST(a, b)
  4124. #define PF_FREE_ALL(list, heap)
  4125. #define PF_GET_SIZE(list) 0
  4126. #define PF_WRITE(a, b) 0
  4127. #define PF_PARSE(a, b, c, d) 0
  4128. #define PF_VALIDATE_REQUEST(a, b)
  4129. #define PF_VALIDATE_RESPONSE(a, b)
  4130. #endif /* HAVE_SUPPORTED_CURVES */
  4131. /******************************************************************************/
  4132. /* Renegotiation Indication */
  4133. /******************************************************************************/
  4134. #if defined(HAVE_SECURE_RENEGOTIATION) \
  4135. || defined(HAVE_SERVER_RENEGOTIATION_INFO)
  4136. static byte TLSX_SecureRenegotiation_GetSize(SecureRenegotiation* data,
  4137. int isRequest)
  4138. {
  4139. byte length = OPAQUE8_LEN; /* empty info length */
  4140. /* data will be NULL for HAVE_SERVER_RENEGOTIATION_INFO only */
  4141. if (data && data->enabled && data->verifySet) {
  4142. /* client sends client_verify_data only */
  4143. length += TLS_FINISHED_SZ;
  4144. /* server also sends server_verify_data */
  4145. if (!isRequest)
  4146. length += TLS_FINISHED_SZ;
  4147. }
  4148. return length;
  4149. }
  4150. static word16 TLSX_SecureRenegotiation_Write(SecureRenegotiation* data,
  4151. byte* output, int isRequest)
  4152. {
  4153. word16 offset = OPAQUE8_LEN; /* RenegotiationInfo length */
  4154. if (data && data->enabled && data->verifySet) {
  4155. /* client sends client_verify_data only */
  4156. XMEMCPY(output + offset, data->client_verify_data, TLS_FINISHED_SZ);
  4157. offset += TLS_FINISHED_SZ;
  4158. /* server also sends server_verify_data */
  4159. if (!isRequest) {
  4160. XMEMCPY(output + offset, data->server_verify_data, TLS_FINISHED_SZ);
  4161. offset += TLS_FINISHED_SZ;
  4162. }
  4163. }
  4164. output[0] = (byte)(offset - 1); /* info length - self */
  4165. return offset;
  4166. }
  4167. static int TLSX_SecureRenegotiation_Parse(WOLFSSL* ssl, const byte* input,
  4168. word16 length, byte isRequest)
  4169. {
  4170. int ret = SECURE_RENEGOTIATION_E;
  4171. if (length >= OPAQUE8_LEN) {
  4172. if (isRequest) {
  4173. #ifndef NO_WOLFSSL_SERVER
  4174. if (ssl->secure_renegotiation == NULL) {
  4175. ret = wolfSSL_UseSecureRenegotiation(ssl);
  4176. if (ret == WOLFSSL_SUCCESS)
  4177. ret = 0;
  4178. }
  4179. if (ret != 0 && ret != SECURE_RENEGOTIATION_E) {
  4180. }
  4181. else if (ssl->secure_renegotiation == NULL) {
  4182. }
  4183. else if (!ssl->secure_renegotiation->enabled) {
  4184. if (*input == 0) {
  4185. input++; /* get past size */
  4186. ssl->secure_renegotiation->enabled = 1;
  4187. TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
  4188. ret = 0;
  4189. }
  4190. else {
  4191. /* already in error state */
  4192. WOLFSSL_MSG("SCR client verify data present");
  4193. }
  4194. }
  4195. else if (*input == TLS_FINISHED_SZ) {
  4196. if (length < TLS_FINISHED_SZ + 1) {
  4197. WOLFSSL_MSG("SCR malformed buffer");
  4198. ret = BUFFER_E;
  4199. }
  4200. else {
  4201. input++; /* get past size */
  4202. /* validate client verify data */
  4203. if (XMEMCMP(input,
  4204. ssl->secure_renegotiation->client_verify_data,
  4205. TLS_FINISHED_SZ) == 0) {
  4206. WOLFSSL_MSG("SCR client verify data match");
  4207. TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
  4208. ret = 0; /* verified */
  4209. }
  4210. else {
  4211. /* already in error state */
  4212. WOLFSSL_MSG("SCR client verify data Failure");
  4213. }
  4214. }
  4215. }
  4216. #endif
  4217. }
  4218. else if (ssl->secure_renegotiation != NULL) {
  4219. #ifndef NO_WOLFSSL_CLIENT
  4220. if (!ssl->secure_renegotiation->enabled) {
  4221. if (*input == 0) {
  4222. ssl->secure_renegotiation->enabled = 1;
  4223. ret = 0;
  4224. }
  4225. }
  4226. else if (*input == 2 * TLS_FINISHED_SZ &&
  4227. length == 2 * TLS_FINISHED_SZ + OPAQUE8_LEN) {
  4228. input++; /* get past size */
  4229. /* validate client and server verify data */
  4230. if (XMEMCMP(input,
  4231. ssl->secure_renegotiation->client_verify_data,
  4232. TLS_FINISHED_SZ) == 0 &&
  4233. XMEMCMP(input + TLS_FINISHED_SZ,
  4234. ssl->secure_renegotiation->server_verify_data,
  4235. TLS_FINISHED_SZ) == 0) {
  4236. WOLFSSL_MSG("SCR client and server verify data match");
  4237. ret = 0; /* verified */
  4238. }
  4239. else {
  4240. /* already in error state */
  4241. WOLFSSL_MSG("SCR client and server verify data Failure");
  4242. }
  4243. }
  4244. #endif
  4245. }
  4246. }
  4247. if (ret != 0) {
  4248. WOLFSSL_ERROR_VERBOSE(ret);
  4249. SendAlert(ssl, alert_fatal, handshake_failure);
  4250. }
  4251. return ret;
  4252. }
  4253. int TLSX_UseSecureRenegotiation(TLSX** extensions, void* heap)
  4254. {
  4255. int ret = 0;
  4256. SecureRenegotiation* data;
  4257. data = (SecureRenegotiation*)XMALLOC(sizeof(SecureRenegotiation), heap,
  4258. DYNAMIC_TYPE_TLSX);
  4259. if (data == NULL)
  4260. return MEMORY_E;
  4261. XMEMSET(data, 0, sizeof(SecureRenegotiation));
  4262. ret = TLSX_Push(extensions, TLSX_RENEGOTIATION_INFO, data, heap);
  4263. if (ret != 0) {
  4264. XFREE(data, heap, DYNAMIC_TYPE_TLSX);
  4265. return ret;
  4266. }
  4267. return WOLFSSL_SUCCESS;
  4268. }
  4269. #ifdef HAVE_SERVER_RENEGOTIATION_INFO
  4270. int TLSX_AddEmptyRenegotiationInfo(TLSX** extensions, void* heap)
  4271. {
  4272. int ret;
  4273. /* send empty renegotiation_info extension */
  4274. TLSX* ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
  4275. if (ext == NULL) {
  4276. ret = TLSX_UseSecureRenegotiation(extensions, heap);
  4277. if (ret != WOLFSSL_SUCCESS)
  4278. return ret;
  4279. ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
  4280. }
  4281. if (ext)
  4282. ext->resp = 1;
  4283. return WOLFSSL_SUCCESS;
  4284. }
  4285. #endif /* HAVE_SERVER_RENEGOTIATION_INFO */
  4286. #define SCR_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
  4287. #define SCR_GET_SIZE TLSX_SecureRenegotiation_GetSize
  4288. #define SCR_WRITE TLSX_SecureRenegotiation_Write
  4289. #define SCR_PARSE TLSX_SecureRenegotiation_Parse
  4290. #else
  4291. #define SCR_FREE_ALL(a, heap)
  4292. #define SCR_GET_SIZE(a, b) 0
  4293. #define SCR_WRITE(a, b, c) 0
  4294. #define SCR_PARSE(a, b, c, d) 0
  4295. #endif /* HAVE_SECURE_RENEGOTIATION || HAVE_SERVER_RENEGOTIATION_INFO */
  4296. /******************************************************************************/
  4297. /* Session Tickets */
  4298. /******************************************************************************/
  4299. #ifdef HAVE_SESSION_TICKET
  4300. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  4301. static void TLSX_SessionTicket_ValidateRequest(WOLFSSL* ssl)
  4302. {
  4303. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_SESSION_TICKET);
  4304. SessionTicket* ticket = extension ?
  4305. (SessionTicket*)extension->data : NULL;
  4306. if (ticket) {
  4307. /* TODO validate ticket timeout here! */
  4308. if (ticket->lifetime == 0xfffffff) {
  4309. /* send empty ticket on timeout */
  4310. TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4311. }
  4312. }
  4313. }
  4314. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  4315. static word16 TLSX_SessionTicket_GetSize(SessionTicket* ticket, int isRequest)
  4316. {
  4317. (void)isRequest;
  4318. return ticket ? ticket->size : 0;
  4319. }
  4320. static word16 TLSX_SessionTicket_Write(SessionTicket* ticket, byte* output,
  4321. int isRequest)
  4322. {
  4323. word16 offset = 0; /* empty ticket */
  4324. if (isRequest && ticket) {
  4325. XMEMCPY(output + offset, ticket->data, ticket->size);
  4326. offset += ticket->size;
  4327. }
  4328. return offset;
  4329. }
  4330. static int TLSX_SessionTicket_Parse(WOLFSSL* ssl, const byte* input,
  4331. word16 length, byte isRequest)
  4332. {
  4333. int ret = 0;
  4334. (void) input; /* avoid unused parameter if NO_WOLFSSL_SERVER defined */
  4335. if (!isRequest) {
  4336. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_SESSION_TICKET))
  4337. return TLSX_HandleUnsupportedExtension(ssl);
  4338. if (length != 0)
  4339. return BUFFER_ERROR;
  4340. #ifndef NO_WOLFSSL_CLIENT
  4341. ssl->expect_session_ticket = 1;
  4342. #endif
  4343. }
  4344. #ifndef NO_WOLFSSL_SERVER
  4345. else {
  4346. /* server side */
  4347. if (ssl->ctx->ticketEncCb == NULL) {
  4348. WOLFSSL_MSG("Client sent session ticket, server has no callback");
  4349. return 0;
  4350. }
  4351. if (length > SESSION_TICKET_LEN) {
  4352. ret = BAD_TICKET_MSG_SZ;
  4353. WOLFSSL_ERROR_VERBOSE(ret);
  4354. } else if (IsAtLeastTLSv1_3(ssl->version)) {
  4355. WOLFSSL_MSG("Process client ticket rejected, TLS 1.3 no support");
  4356. ssl->options.rejectTicket = 1;
  4357. ret = 0; /* not fatal */
  4358. } else if (ssl->options.noTicketTls12) {
  4359. /* ignore ticket request */
  4360. } else if (length == 0) {
  4361. /* blank ticket */
  4362. ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4363. if (ret == WOLFSSL_SUCCESS) {
  4364. ret = 0;
  4365. /* send blank ticket */
  4366. TLSX_SetResponse(ssl, TLSX_SESSION_TICKET);
  4367. ssl->options.createTicket = 1; /* will send ticket msg */
  4368. ssl->options.useTicket = 1;
  4369. ssl->options.resuming = 0; /* no standard resumption */
  4370. ssl->arrays->sessionIDSz = 0; /* no echo on blank ticket */
  4371. }
  4372. } else {
  4373. /* got actual ticket from client */
  4374. ret = DoClientTicket(ssl, input, length);
  4375. if (ret == WOLFSSL_TICKET_RET_OK) { /* use ticket to resume */
  4376. WOLFSSL_MSG("Using existing client ticket");
  4377. ssl->options.useTicket = 1;
  4378. ssl->options.resuming = 1;
  4379. /* SERVER: ticket is peer auth. */
  4380. ssl->options.peerAuthGood = 1;
  4381. } else if (ret == WOLFSSL_TICKET_RET_CREATE) {
  4382. WOLFSSL_MSG("Using existing client ticket, creating new one");
  4383. ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4384. if (ret == WOLFSSL_SUCCESS) {
  4385. ret = 0;
  4386. TLSX_SetResponse(ssl, TLSX_SESSION_TICKET);
  4387. /* send blank ticket */
  4388. ssl->options.createTicket = 1; /* will send ticket msg */
  4389. ssl->options.useTicket = 1;
  4390. ssl->options.resuming = 1;
  4391. /* SERVER: ticket is peer auth. */
  4392. ssl->options.peerAuthGood = 1;
  4393. }
  4394. } else if (ret == WOLFSSL_TICKET_RET_REJECT) {
  4395. WOLFSSL_MSG("Process client ticket rejected, not using");
  4396. ssl->options.rejectTicket = 1;
  4397. ret = 0; /* not fatal */
  4398. } else if (ret == VERSION_ERROR) {
  4399. WOLFSSL_MSG("Process client ticket rejected, bad TLS version");
  4400. ssl->options.rejectTicket = 1;
  4401. ret = 0; /* not fatal */
  4402. } else if (ret == WOLFSSL_TICKET_RET_FATAL) {
  4403. WOLFSSL_MSG("Process client ticket fatal error, not using");
  4404. } else if (ret < 0) {
  4405. WOLFSSL_MSG("Process client ticket unknown error, not using");
  4406. }
  4407. }
  4408. }
  4409. #endif /* NO_WOLFSSL_SERVER */
  4410. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  4411. (void)ssl;
  4412. #endif
  4413. return ret;
  4414. }
  4415. WOLFSSL_LOCAL SessionTicket* TLSX_SessionTicket_Create(word32 lifetime,
  4416. byte* data, word16 size, void* heap)
  4417. {
  4418. SessionTicket* ticket = (SessionTicket*)XMALLOC(sizeof(SessionTicket),
  4419. heap, DYNAMIC_TYPE_TLSX);
  4420. if (ticket) {
  4421. ticket->data = (byte*)XMALLOC(size, heap, DYNAMIC_TYPE_TLSX);
  4422. if (ticket->data == NULL) {
  4423. XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
  4424. return NULL;
  4425. }
  4426. XMEMCPY(ticket->data, data, size);
  4427. ticket->size = size;
  4428. ticket->lifetime = lifetime;
  4429. }
  4430. (void)heap;
  4431. return ticket;
  4432. }
  4433. WOLFSSL_LOCAL void TLSX_SessionTicket_Free(SessionTicket* ticket, void* heap)
  4434. {
  4435. if (ticket) {
  4436. XFREE(ticket->data, heap, DYNAMIC_TYPE_TLSX);
  4437. XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
  4438. }
  4439. (void)heap;
  4440. }
  4441. int TLSX_UseSessionTicket(TLSX** extensions, SessionTicket* ticket, void* heap)
  4442. {
  4443. int ret = 0;
  4444. if (extensions == NULL)
  4445. return BAD_FUNC_ARG;
  4446. /* If the ticket is NULL, the client will request a new ticket from the
  4447. server. Otherwise, the client will use it in the next client hello. */
  4448. if ((ret = TLSX_Push(extensions, TLSX_SESSION_TICKET, (void*)ticket, heap))
  4449. != 0)
  4450. return ret;
  4451. return WOLFSSL_SUCCESS;
  4452. }
  4453. #define WOLF_STK_VALIDATE_REQUEST TLSX_SessionTicket_ValidateRequest
  4454. #define WOLF_STK_GET_SIZE TLSX_SessionTicket_GetSize
  4455. #define WOLF_STK_WRITE TLSX_SessionTicket_Write
  4456. #define WOLF_STK_PARSE TLSX_SessionTicket_Parse
  4457. #define WOLF_STK_FREE(stk, heap) TLSX_SessionTicket_Free((SessionTicket*)(stk),(heap))
  4458. #else
  4459. #define WOLF_STK_FREE(a, b)
  4460. #define WOLF_STK_VALIDATE_REQUEST(a)
  4461. #define WOLF_STK_GET_SIZE(a, b) 0
  4462. #define WOLF_STK_WRITE(a, b, c) 0
  4463. #define WOLF_STK_PARSE(a, b, c, d) 0
  4464. #endif /* HAVE_SESSION_TICKET */
  4465. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  4466. /******************************************************************************/
  4467. /* Encrypt-then-MAC */
  4468. /******************************************************************************/
  4469. #ifndef WOLFSSL_NO_TLS12
  4470. static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl);
  4471. /**
  4472. * Get the size of the Encrypt-Then-MAC extension.
  4473. *
  4474. * msgType Type of message to put extension into.
  4475. * pSz Size of extension data.
  4476. * return SANITY_MSG_E when the message is not allowed to have extension and
  4477. * 0 otherwise.
  4478. */
  4479. static int TLSX_EncryptThenMac_GetSize(byte msgType, word16* pSz)
  4480. {
  4481. (void)pSz;
  4482. if (msgType != client_hello && msgType != server_hello) {
  4483. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4484. return SANITY_MSG_E;
  4485. }
  4486. /* Empty extension */
  4487. return 0;
  4488. }
  4489. /**
  4490. * Write the Encrypt-Then-MAC extension.
  4491. *
  4492. * data Unused
  4493. * output Extension data buffer. Unused.
  4494. * msgType Type of message to put extension into.
  4495. * pSz Size of extension data.
  4496. * return SANITY_MSG_E when the message is not allowed to have extension and
  4497. * 0 otherwise.
  4498. */
  4499. static int TLSX_EncryptThenMac_Write(void* data, byte* output, byte msgType,
  4500. word16* pSz)
  4501. {
  4502. (void)data;
  4503. (void)output;
  4504. (void)pSz;
  4505. if (msgType != client_hello && msgType != server_hello) {
  4506. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4507. return SANITY_MSG_E;
  4508. }
  4509. /* Empty extension */
  4510. return 0;
  4511. }
  4512. /**
  4513. * Parse the Encrypt-Then-MAC extension.
  4514. *
  4515. * ssl SSL object
  4516. * input Extension data buffer.
  4517. * length Length of this extension's data.
  4518. * msgType Type of message to extension appeared in.
  4519. * return SANITY_MSG_E when the message is not allowed to have extension,
  4520. * BUFFER_ERROR when the extension's data is invalid,
  4521. * MEMORY_E when unable to allocate memory and
  4522. * 0 otherwise.
  4523. */
  4524. static int TLSX_EncryptThenMac_Parse(WOLFSSL* ssl, const byte* input,
  4525. word16 length, byte msgType)
  4526. {
  4527. int ret;
  4528. (void)input;
  4529. if (msgType != client_hello && msgType != server_hello) {
  4530. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4531. return SANITY_MSG_E;
  4532. }
  4533. /* Empty extension */
  4534. if (length != 0)
  4535. return BUFFER_ERROR;
  4536. if (msgType == client_hello) {
  4537. /* Check the user hasn't disallowed use of Encrypt-Then-Mac. */
  4538. if (!ssl->options.disallowEncThenMac) {
  4539. ssl->options.encThenMac = 1;
  4540. /* Set the extension reply. */
  4541. ret = TLSX_EncryptThenMac_Use(ssl);
  4542. if (ret != 0)
  4543. return ret;
  4544. }
  4545. return 0;
  4546. }
  4547. /* Server Hello */
  4548. if (ssl->options.disallowEncThenMac) {
  4549. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4550. return SANITY_MSG_E;
  4551. }
  4552. ssl->options.encThenMac = 1;
  4553. return 0;
  4554. }
  4555. /**
  4556. * Add the Encrypt-Then-MAC extension to list.
  4557. *
  4558. * ssl SSL object
  4559. * return MEMORY_E when unable to allocate memory and 0 otherwise.
  4560. */
  4561. static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl)
  4562. {
  4563. int ret = 0;
  4564. TLSX* extension;
  4565. /* Find the Encrypt-Then-Mac extension if it exists. */
  4566. extension = TLSX_Find(ssl->extensions, TLSX_ENCRYPT_THEN_MAC);
  4567. if (extension == NULL) {
  4568. /* Push new Encrypt-Then-Mac extension. */
  4569. ret = TLSX_Push(&ssl->extensions, TLSX_ENCRYPT_THEN_MAC, NULL,
  4570. ssl->heap);
  4571. if (ret != 0)
  4572. return ret;
  4573. }
  4574. return 0;
  4575. }
  4576. /**
  4577. * Set the Encrypt-Then-MAC extension as one to respond too.
  4578. *
  4579. * ssl SSL object
  4580. * return EXT_MISSING when EncryptThenMac extension not in list.
  4581. */
  4582. int TLSX_EncryptThenMac_Respond(WOLFSSL* ssl)
  4583. {
  4584. TLSX* extension;
  4585. extension = TLSX_Find(ssl->extensions, TLSX_ENCRYPT_THEN_MAC);
  4586. if (extension == NULL)
  4587. return EXT_MISSING;
  4588. extension->resp = 1;
  4589. return 0;
  4590. }
  4591. #define ETM_GET_SIZE TLSX_EncryptThenMac_GetSize
  4592. #define ETM_WRITE TLSX_EncryptThenMac_Write
  4593. #define ETM_PARSE TLSX_EncryptThenMac_Parse
  4594. #else
  4595. #define ETM_GET_SIZE(a, b) 0
  4596. #define ETM_WRITE(a, b, c, d) 0
  4597. #define ETM_PARSE(a, b, c, d) 0
  4598. #endif /* !WOLFSSL_NO_TLS12 */
  4599. #endif /* HAVE_ENCRYPT_THEN_MAC && !WOLFSSL_AEAD_ONLY */
  4600. #ifdef WOLFSSL_SRTP
  4601. /******************************************************************************/
  4602. /* DTLS SRTP (Secure Real-time Transport Protocol) */
  4603. /******************************************************************************/
  4604. /* Only support single SRTP profile */
  4605. typedef struct TlsxSrtp {
  4606. word16 profileCount;
  4607. word16 ids; /* selected bits */
  4608. } TlsxSrtp;
  4609. static int TLSX_UseSRTP_GetSize(TlsxSrtp *srtp)
  4610. {
  4611. /* SRTP Profile Len (2)
  4612. * SRTP Profiles (2)
  4613. * MKI (master key id) Length */
  4614. return (OPAQUE16_LEN + (srtp->profileCount * OPAQUE16_LEN) + 1);
  4615. }
  4616. static TlsxSrtp* TLSX_UseSRTP_New(word16 ids, void* heap)
  4617. {
  4618. TlsxSrtp* srtp;
  4619. int i;
  4620. srtp = (TlsxSrtp*)XMALLOC(sizeof(TlsxSrtp), heap, DYNAMIC_TYPE_TLSX);
  4621. if (srtp == NULL) {
  4622. WOLFSSL_MSG("TLSX SRTP Memory failure");
  4623. return NULL;
  4624. }
  4625. /* count and test each bit set */
  4626. srtp->profileCount = 0;
  4627. for (i=0; i<16; i++) {
  4628. if (ids & (1 << i)) {
  4629. srtp->profileCount++;
  4630. }
  4631. }
  4632. srtp->ids = ids;
  4633. return srtp;
  4634. }
  4635. static void TLSX_UseSRTP_Free(TlsxSrtp *srtp, void* heap)
  4636. {
  4637. if (srtp != NULL) {
  4638. XFREE(srtp, heap, DYNAMIC_TYPE_TLSX);
  4639. }
  4640. (void)heap;
  4641. }
  4642. static int TLSX_UseSRTP_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  4643. byte isRequest)
  4644. {
  4645. int ret = BAD_FUNC_ARG;
  4646. word16 profile_len = 0;
  4647. word16 profile_value = 0;
  4648. word16 offset = 0;
  4649. #ifndef NO_WOLFSSL_SERVER
  4650. int i;
  4651. TlsxSrtp* srtp = NULL;
  4652. #endif
  4653. if (length < OPAQUE16_LEN) {
  4654. return BUFFER_ERROR;
  4655. }
  4656. /* reset selected DTLS SRTP profile ID */
  4657. ssl->dtlsSrtpId = 0;
  4658. /* total length, not include itself */
  4659. ato16(input, &profile_len);
  4660. offset += OPAQUE16_LEN;
  4661. if (!isRequest) {
  4662. #ifndef NO_WOLFSSL_CLIENT
  4663. if (length < offset + OPAQUE16_LEN)
  4664. return BUFFER_ERROR;
  4665. ato16(input + offset, &profile_value);
  4666. /* check that the profile received was in the ones we support */
  4667. if (profile_value < 16 &&
  4668. (ssl->dtlsSrtpProfiles & (1 << profile_value))) {
  4669. ssl->dtlsSrtpId = profile_value;
  4670. ret = 0; /* success */
  4671. }
  4672. #endif
  4673. }
  4674. #ifndef NO_WOLFSSL_SERVER
  4675. else {
  4676. /* parse remainder one profile at a time, looking for match in CTX */
  4677. ret = 0;
  4678. for (i=offset; i<length; i+=OPAQUE16_LEN) {
  4679. ato16(input+i, &profile_value);
  4680. /* find first match */
  4681. if (profile_value < 16 &&
  4682. ssl->dtlsSrtpProfiles & (1 << profile_value)) {
  4683. ssl->dtlsSrtpId = profile_value;
  4684. /* make sure we respond with selected SRTP id selected */
  4685. srtp = TLSX_UseSRTP_New((1 << profile_value), ssl->heap);
  4686. if (srtp != NULL) {
  4687. ret = TLSX_Push(&ssl->extensions, TLSX_USE_SRTP,
  4688. (void*)srtp, ssl->heap);
  4689. if (ret == 0) {
  4690. TLSX_SetResponse(ssl, TLSX_USE_SRTP);
  4691. /* successfully set extension */
  4692. }
  4693. }
  4694. else {
  4695. ret = MEMORY_E;
  4696. }
  4697. break;
  4698. }
  4699. }
  4700. }
  4701. if (ret == 0 && ssl->dtlsSrtpId == 0) {
  4702. WOLFSSL_MSG("TLSX_UseSRTP_Parse profile not found!");
  4703. /* not fatal */
  4704. }
  4705. else if (ret != 0) {
  4706. ssl->dtlsSrtpId = 0;
  4707. TLSX_UseSRTP_Free(srtp, ssl->heap);
  4708. }
  4709. #endif
  4710. (void)profile_len;
  4711. return ret;
  4712. }
  4713. static word16 TLSX_UseSRTP_Write(TlsxSrtp* srtp, byte* output)
  4714. {
  4715. word16 offset = 0;
  4716. int i, j;
  4717. c16toa(srtp->profileCount*2, output+offset);
  4718. offset += OPAQUE16_LEN;
  4719. for (i=0; i< srtp->profileCount; i+=2) {
  4720. for (j=0; j<16; j++) {
  4721. if (srtp->ids & (1 << j)) {
  4722. c16toa(j, output+offset);
  4723. offset += OPAQUE16_LEN;
  4724. }
  4725. }
  4726. }
  4727. output[offset++] = 0x00; /* MKI Length */
  4728. return offset;
  4729. }
  4730. static int TLSX_UseSRTP(TLSX** extensions, word16 profiles, void* heap)
  4731. {
  4732. int ret = 0;
  4733. TLSX* extension;
  4734. if (extensions == NULL) {
  4735. return BAD_FUNC_ARG;
  4736. }
  4737. extension = TLSX_Find(*extensions, TLSX_USE_SRTP);
  4738. if (extension == NULL) {
  4739. TlsxSrtp* srtp = TLSX_UseSRTP_New(profiles, heap);
  4740. if (srtp == NULL) {
  4741. return MEMORY_E;
  4742. }
  4743. ret = TLSX_Push(extensions, TLSX_USE_SRTP, (void*)srtp, heap);
  4744. if (ret != 0) {
  4745. TLSX_UseSRTP_Free(srtp, heap);
  4746. }
  4747. }
  4748. return ret;
  4749. }
  4750. #ifndef NO_WOLFSSL_SERVER
  4751. #define SRTP_FREE TLSX_UseSRTP_Free
  4752. #define SRTP_PARSE TLSX_UseSRTP_Parse
  4753. #define SRTP_WRITE TLSX_UseSRTP_Write
  4754. #define SRTP_GET_SIZE TLSX_UseSRTP_GetSize
  4755. #else
  4756. #define SRTP_FREE(a, b)
  4757. #define SRTP_PARSE(a, b, c, d) 0
  4758. #define SRTP_WRITE(a, b) 0
  4759. #define SRTP_GET_SIZE(a) 0
  4760. #endif
  4761. #endif /* WOLFSSL_SRTP */
  4762. /******************************************************************************/
  4763. /* Supported Versions */
  4764. /******************************************************************************/
  4765. #ifdef WOLFSSL_TLS13
  4766. static WC_INLINE int versionIsGreater(byte isDtls, byte a, byte b)
  4767. {
  4768. (void)isDtls;
  4769. #ifdef WOLFSSL_DTLS
  4770. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4771. if (isDtls)
  4772. return a < b;
  4773. #endif /* WOLFSSL_DTLS */
  4774. return a > b;
  4775. }
  4776. static WC_INLINE int versionIsLesser(byte isDtls, byte a, byte b)
  4777. {
  4778. (void)isDtls;
  4779. #ifdef WOLFSSL_DTLS
  4780. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4781. if (isDtls)
  4782. return a > b;
  4783. #endif /* WOLFSSL_DTLS */
  4784. return a < b;
  4785. }
  4786. static WC_INLINE int versionIsAtLeast(byte isDtls, byte a, byte b)
  4787. {
  4788. (void)isDtls;
  4789. #ifdef WOLFSSL_DTLS
  4790. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4791. if (isDtls)
  4792. return a <= b;
  4793. #endif /* WOLFSSL_DTLS */
  4794. return a >= b;
  4795. }
  4796. static WC_INLINE int versionIsLessEqual(byte isDtls, byte a, byte b)
  4797. {
  4798. (void)isDtls;
  4799. #ifdef WOLFSSL_DTLS
  4800. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4801. if (isDtls)
  4802. return a >= b;
  4803. #endif /* WOLFSSL_DTLS */
  4804. return a <= b;
  4805. }
  4806. /* Return the size of the SupportedVersions extension's data.
  4807. *
  4808. * data The SSL/TLS object.
  4809. * msgType The type of the message this extension is being written into.
  4810. * returns the length of data that will be in the extension.
  4811. */
  4812. static int TLSX_SupportedVersions_GetSize(void* data, byte msgType, word16* pSz)
  4813. {
  4814. WOLFSSL* ssl = (WOLFSSL*)data;
  4815. byte tls13Minor, tls12Minor, tls11Minor, isDtls;
  4816. isDtls = !!ssl->options.dtls;
  4817. tls13Minor = (byte)(isDtls ? DTLSv1_3_MINOR : TLSv1_3_MINOR);
  4818. tls12Minor = (byte)(isDtls ? DTLSv1_2_MINOR : TLSv1_2_MINOR);
  4819. tls11Minor = (byte)(isDtls ? DTLS_MINOR : TLSv1_1_MINOR);
  4820. /* unused on some configuration */
  4821. (void)tls12Minor;
  4822. (void)tls13Minor;
  4823. (void)tls11Minor;
  4824. if (msgType == client_hello) {
  4825. /* TLS v1.2 and TLS v1.3 */
  4826. int cnt = 0;
  4827. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls13Minor)
  4828. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4829. defined(WOLFSSL_WPAS_SMALL)
  4830. && (ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0
  4831. #endif
  4832. ) {
  4833. cnt++;
  4834. }
  4835. if (ssl->options.downgrade) {
  4836. #ifndef WOLFSSL_NO_TLS12
  4837. if (versionIsLessEqual(
  4838. isDtls, ssl->options.minDowngrade, tls12Minor)
  4839. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4840. defined(WOLFSSL_WPAS_SMALL)
  4841. && (ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0
  4842. #endif
  4843. ) {
  4844. cnt++;
  4845. }
  4846. #endif
  4847. #ifndef NO_OLD_TLS
  4848. if (versionIsLessEqual(
  4849. isDtls, ssl->options.minDowngrade, tls11Minor)
  4850. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4851. defined(WOLFSSL_WPAS_SMALL)
  4852. && (ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0
  4853. #endif
  4854. ) {
  4855. cnt++;
  4856. }
  4857. #ifdef WOLFSSL_ALLOW_TLSV10
  4858. if (!ssl->options.dtls && (ssl->options.minDowngrade <= TLSv1_MINOR)
  4859. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4860. defined(WOLFSSL_WPAS_SMALL)
  4861. && (ssl->options.mask & SSL_OP_NO_TLSv1) == 0
  4862. #endif
  4863. ) {
  4864. cnt++;
  4865. }
  4866. #endif
  4867. #endif
  4868. }
  4869. *pSz += (word16)(OPAQUE8_LEN + cnt * OPAQUE16_LEN);
  4870. }
  4871. else if (msgType == server_hello || msgType == hello_retry_request) {
  4872. *pSz += OPAQUE16_LEN;
  4873. }
  4874. else {
  4875. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4876. return SANITY_MSG_E;
  4877. }
  4878. return 0;
  4879. }
  4880. /* Writes the SupportedVersions extension into the buffer.
  4881. *
  4882. * data The SSL/TLS object.
  4883. * output The buffer to write the extension into.
  4884. * msgType The type of the message this extension is being written into.
  4885. * returns the length of data that was written.
  4886. */
  4887. static int TLSX_SupportedVersions_Write(void* data, byte* output,
  4888. byte msgType, word16* pSz)
  4889. {
  4890. WOLFSSL* ssl = (WOLFSSL*)data;
  4891. byte major;
  4892. byte* cnt;
  4893. byte tls13minor, tls12minor, tls11minor, isDtls = 0;
  4894. tls13minor = (byte)TLSv1_3_MINOR;
  4895. tls12minor = (byte)TLSv1_2_MINOR;
  4896. tls11minor = (byte)TLSv1_1_MINOR;
  4897. /* unused in some configuration */
  4898. (void)tls11minor;
  4899. (void)tls12minor;
  4900. #ifdef WOLFSSL_DTLS13
  4901. if (ssl->options.dtls) {
  4902. tls13minor = (byte)DTLSv1_3_MINOR;
  4903. tls12minor = (byte)DTLSv1_2_MINOR;
  4904. tls11minor = (byte)DTLS_MINOR;
  4905. isDtls = 1;
  4906. }
  4907. #endif /* WOLFSSL_DTLS13 */
  4908. if (msgType == client_hello) {
  4909. major = ssl->ctx->method->version.major;
  4910. cnt = output++;
  4911. *cnt = 0;
  4912. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls13minor)
  4913. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4914. defined(WOLFSSL_WPAS_SMALL)
  4915. && (ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0
  4916. #endif
  4917. ) {
  4918. *cnt += OPAQUE16_LEN;
  4919. #ifdef WOLFSSL_TLS13_DRAFT
  4920. /* The TLS draft major number. */
  4921. *(output++) = TLS_DRAFT_MAJOR;
  4922. /* Version of draft supported. */
  4923. *(output++) = TLS_DRAFT_MINOR;
  4924. #else
  4925. *(output++) = major;
  4926. *(output++) = tls13minor;
  4927. #endif
  4928. }
  4929. if (ssl->options.downgrade) {
  4930. #ifndef WOLFSSL_NO_TLS12
  4931. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls12minor)
  4932. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4933. defined(WOLFSSL_WPAS_SMALL)
  4934. && (ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0
  4935. #endif
  4936. ) {
  4937. *cnt += OPAQUE16_LEN;
  4938. *(output++) = major;
  4939. *(output++) = tls12minor;
  4940. }
  4941. #endif
  4942. #ifndef NO_OLD_TLS
  4943. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls11minor)
  4944. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4945. defined(WOLFSSL_WPAS_SMALL)
  4946. && (ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0
  4947. #endif
  4948. ) {
  4949. *cnt += OPAQUE16_LEN;
  4950. *(output++) = major;
  4951. *(output++) = tls11minor;
  4952. }
  4953. #ifdef WOLFSSL_ALLOW_TLSV10
  4954. if (!ssl->options.dtls && (ssl->options.minDowngrade <= TLSv1_MINOR)
  4955. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4956. defined(WOLFSSL_WPAS_SMALL)
  4957. && (ssl->options.mask & SSL_OP_NO_TLSv1) == 0
  4958. #endif
  4959. ) {
  4960. *cnt += OPAQUE16_LEN;
  4961. *(output++) = major;
  4962. *(output++) = (byte)TLSv1_MINOR;
  4963. }
  4964. #endif
  4965. #endif
  4966. }
  4967. *pSz += (word16)(OPAQUE8_LEN + *cnt);
  4968. }
  4969. else if (msgType == server_hello || msgType == hello_retry_request) {
  4970. output[0] = ssl->version.major;
  4971. output[1] = ssl->version.minor;
  4972. *pSz += OPAQUE16_LEN;
  4973. }
  4974. else {
  4975. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4976. return SANITY_MSG_E;
  4977. }
  4978. return 0;
  4979. }
  4980. /* Parse the SupportedVersions extension.
  4981. *
  4982. * ssl The SSL/TLS object.
  4983. * input The buffer with the extension data.
  4984. * length The length of the extension data.
  4985. * msgType The type of the message this extension is being parsed from.
  4986. * returns 0 on success, otherwise failure.
  4987. */
  4988. static int TLSX_SupportedVersions_Parse(WOLFSSL* ssl, const byte* input,
  4989. word16 length, byte msgType)
  4990. {
  4991. ProtocolVersion pv = ssl->ctx->method->version;
  4992. int i;
  4993. int len;
  4994. byte newMinor = 0;
  4995. int set = 0;
  4996. int ret;
  4997. byte major, minor;
  4998. byte tls13minor, tls12minor;
  4999. byte isDtls;
  5000. tls13minor = TLSv1_3_MINOR;
  5001. tls12minor = TLSv1_2_MINOR;
  5002. isDtls = ssl->options.dtls == 1;
  5003. #ifdef WOLFSSL_DTLS13
  5004. if (ssl->options.dtls) {
  5005. tls13minor = DTLSv1_3_MINOR;
  5006. tls12minor = DTLSv1_2_MINOR;
  5007. }
  5008. #endif /* WOLFSSL_DTLS13 */
  5009. if (msgType == client_hello) {
  5010. /* Must contain a length and at least one version. */
  5011. if (length < OPAQUE8_LEN + OPAQUE16_LEN || (length & 1) != 1)
  5012. return BUFFER_ERROR;
  5013. len = *input;
  5014. /* Protocol version array must fill rest of data. */
  5015. if (length != (word16)OPAQUE8_LEN + len)
  5016. return BUFFER_ERROR;
  5017. input++;
  5018. /* Find first match. */
  5019. for (i = 0; i < len; i += OPAQUE16_LEN) {
  5020. major = input[i];
  5021. minor = input[i + OPAQUE8_LEN];
  5022. #ifdef WOLFSSL_TLS13_DRAFT
  5023. if (major == TLS_DRAFT_MAJOR && minor == TLS_DRAFT_MINOR) {
  5024. major = SSLv3_MAJOR;
  5025. minor = TLSv1_3_MINOR;
  5026. }
  5027. #else
  5028. if (major == TLS_DRAFT_MAJOR)
  5029. continue;
  5030. #endif
  5031. if (major != pv.major)
  5032. continue;
  5033. /* No upgrade allowed. */
  5034. if (versionIsGreater(isDtls, minor, ssl->version.minor))
  5035. continue;
  5036. /* Check downgrade. */
  5037. if (versionIsLesser(isDtls, minor, ssl->version.minor)) {
  5038. if (!ssl->options.downgrade)
  5039. continue;
  5040. if (versionIsLesser(
  5041. isDtls, minor, ssl->options.minDowngrade))
  5042. continue;
  5043. if (newMinor == 0 &&
  5044. versionIsGreater(
  5045. isDtls, minor, ssl->options.oldMinor)) {
  5046. /* Downgrade the version. */
  5047. ssl->version.minor = minor;
  5048. }
  5049. }
  5050. if (versionIsAtLeast(isDtls, minor, tls13minor)) {
  5051. ssl->options.tls1_3 = 1;
  5052. /* TLS v1.3 requires supported version extension */
  5053. if (TLSX_Find(ssl->extensions, TLSX_SUPPORTED_VERSIONS) == NULL) {
  5054. ret = TLSX_Prepend(&ssl->extensions,
  5055. TLSX_SUPPORTED_VERSIONS, ssl, ssl->heap);
  5056. if (ret != 0) {
  5057. return ret;
  5058. }
  5059. TLSX_SetResponse(ssl, TLSX_SUPPORTED_VERSIONS);
  5060. }
  5061. if (versionIsGreater(isDtls, minor, newMinor)) {
  5062. ssl->version.minor = minor;
  5063. newMinor = minor;
  5064. }
  5065. }
  5066. else if (versionIsGreater(
  5067. isDtls, minor, ssl->options.oldMinor))
  5068. ssl->options.oldMinor = minor;
  5069. set = 1;
  5070. }
  5071. if (!set) {
  5072. SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version);
  5073. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5074. return VERSION_ERROR;
  5075. }
  5076. }
  5077. else if (msgType == server_hello || msgType == hello_retry_request) {
  5078. /* Must contain one version. */
  5079. if (length != OPAQUE16_LEN)
  5080. return BUFFER_ERROR;
  5081. major = input[0];
  5082. minor = input[OPAQUE8_LEN];
  5083. if (major != pv.major) {
  5084. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5085. return VERSION_ERROR;
  5086. }
  5087. /* Can't downgrade with this extension below TLS v1.3. */
  5088. if (versionIsLesser(isDtls, minor, tls13minor)) {
  5089. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5090. return VERSION_ERROR;
  5091. }
  5092. /* Version is TLS v1.2 to handle downgrading from TLS v1.3+. */
  5093. if (ssl->options.downgrade && ssl->version.minor == tls12minor) {
  5094. /* Set minor version back to TLS v1.3+ */
  5095. ssl->version.minor = ssl->ctx->method->version.minor;
  5096. }
  5097. /* No upgrade allowed. */
  5098. if (versionIsLesser(isDtls, ssl->version.minor, minor)) {
  5099. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5100. return VERSION_ERROR;
  5101. }
  5102. /* Check downgrade. */
  5103. if (versionIsGreater(isDtls, ssl->version.minor, minor)) {
  5104. if (!ssl->options.downgrade) {
  5105. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5106. return VERSION_ERROR;
  5107. }
  5108. if (versionIsLesser(
  5109. isDtls, minor, ssl->options.minDowngrade)) {
  5110. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5111. return VERSION_ERROR;
  5112. }
  5113. /* Downgrade the version. */
  5114. ssl->version.minor = minor;
  5115. }
  5116. }
  5117. else {
  5118. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5119. return SANITY_MSG_E;
  5120. }
  5121. return 0;
  5122. }
  5123. /* Sets a new SupportedVersions extension into the extension list.
  5124. *
  5125. * extensions The list of extensions.
  5126. * data The extensions specific data.
  5127. * heap The heap used for allocation.
  5128. * returns 0 on success, otherwise failure.
  5129. */
  5130. static int TLSX_SetSupportedVersions(TLSX** extensions, const void* data,
  5131. void* heap)
  5132. {
  5133. if (extensions == NULL || data == NULL)
  5134. return BAD_FUNC_ARG;
  5135. return TLSX_Push(extensions, TLSX_SUPPORTED_VERSIONS, data, heap);
  5136. }
  5137. #define SV_GET_SIZE TLSX_SupportedVersions_GetSize
  5138. #define SV_WRITE TLSX_SupportedVersions_Write
  5139. #define SV_PARSE TLSX_SupportedVersions_Parse
  5140. #else
  5141. #define SV_GET_SIZE(a, b, c) 0
  5142. #define SV_WRITE(a, b, c, d) 0
  5143. #define SV_PARSE(a, b, c, d) 0
  5144. #endif /* WOLFSSL_TLS13 */
  5145. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  5146. /******************************************************************************/
  5147. /* Cookie */
  5148. /******************************************************************************/
  5149. /* Free the cookie data.
  5150. *
  5151. * cookie Cookie data.
  5152. * heap The heap used for allocation.
  5153. */
  5154. static void TLSX_Cookie_FreeAll(Cookie* cookie, void* heap)
  5155. {
  5156. (void)heap;
  5157. if (cookie != NULL)
  5158. XFREE(cookie, heap, DYNAMIC_TYPE_TLSX);
  5159. }
  5160. /* Get the size of the encoded Cookie extension.
  5161. * In messages: ClientHello and HelloRetryRequest.
  5162. *
  5163. * cookie The cookie to write.
  5164. * msgType The type of the message this extension is being written into.
  5165. * returns the number of bytes of the encoded Cookie extension.
  5166. */
  5167. static int TLSX_Cookie_GetSize(Cookie* cookie, byte msgType, word16* pSz)
  5168. {
  5169. if (msgType == client_hello || msgType == hello_retry_request) {
  5170. *pSz += OPAQUE16_LEN + cookie->len;
  5171. }
  5172. else {
  5173. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5174. return SANITY_MSG_E;
  5175. }
  5176. return 0;
  5177. }
  5178. /* Writes the Cookie extension into the output buffer.
  5179. * Assumes that the the output buffer is big enough to hold data.
  5180. * In messages: ClientHello and HelloRetryRequest.
  5181. *
  5182. * cookie The cookie to write.
  5183. * output The buffer to write into.
  5184. * msgType The type of the message this extension is being written into.
  5185. * returns the number of bytes written into the buffer.
  5186. */
  5187. static int TLSX_Cookie_Write(Cookie* cookie, byte* output, byte msgType,
  5188. word16* pSz)
  5189. {
  5190. if (msgType == client_hello || msgType == hello_retry_request) {
  5191. c16toa(cookie->len, output);
  5192. output += OPAQUE16_LEN;
  5193. XMEMCPY(output, &cookie->data, cookie->len);
  5194. *pSz += OPAQUE16_LEN + cookie->len;
  5195. }
  5196. else {
  5197. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5198. return SANITY_MSG_E;
  5199. }
  5200. return 0;
  5201. }
  5202. /* Parse the Cookie extension.
  5203. * In messages: ClientHello and HelloRetryRequest.
  5204. *
  5205. * ssl The SSL/TLS object.
  5206. * input The extension data.
  5207. * length The length of the extension data.
  5208. * msgType The type of the message this extension is being parsed from.
  5209. * returns 0 on success and other values indicate failure.
  5210. */
  5211. static int TLSX_Cookie_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  5212. byte msgType)
  5213. {
  5214. word16 len;
  5215. word16 idx = 0;
  5216. TLSX* extension;
  5217. Cookie* cookie;
  5218. if (msgType != client_hello && msgType != hello_retry_request) {
  5219. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5220. return SANITY_MSG_E;
  5221. }
  5222. /* Message contains length and Cookie which must be at least one byte
  5223. * in length.
  5224. */
  5225. if (length < OPAQUE16_LEN + 1)
  5226. return BUFFER_E;
  5227. ato16(input + idx, &len);
  5228. idx += OPAQUE16_LEN;
  5229. if (length - idx != len)
  5230. return BUFFER_E;
  5231. if (msgType == hello_retry_request)
  5232. return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0);
  5233. /* client_hello */
  5234. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5235. if (extension == NULL) {
  5236. #ifdef WOLFSSL_DTLS13
  5237. if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version))
  5238. /* Allow a cookie extension with DTLS 1.3 because it is possible
  5239. * that a different SSL instance sent the cookie but we are now
  5240. * receiving it. */
  5241. return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0);
  5242. else
  5243. #endif
  5244. {
  5245. WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR);
  5246. return HRR_COOKIE_ERROR;
  5247. }
  5248. }
  5249. cookie = (Cookie*)extension->data;
  5250. if (cookie->len != len || XMEMCMP(&cookie->data, input + idx, len) != 0) {
  5251. WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR);
  5252. return HRR_COOKIE_ERROR;
  5253. }
  5254. /* Request seen. */
  5255. extension->resp = 0;
  5256. return 0;
  5257. }
  5258. /* Use the data to create a new Cookie object in the extensions.
  5259. *
  5260. * ssl SSL/TLS object.
  5261. * data Cookie data.
  5262. * len Length of cookie data in bytes.
  5263. * mac MAC data.
  5264. * macSz Length of MAC data in bytes.
  5265. * resp Indicates the extension will go into a response (HelloRetryRequest).
  5266. * returns 0 on success and other values indicate failure.
  5267. */
  5268. int TLSX_Cookie_Use(WOLFSSL* ssl, const byte* data, word16 len, byte* mac,
  5269. byte macSz, int resp)
  5270. {
  5271. int ret = 0;
  5272. TLSX* extension;
  5273. Cookie* cookie;
  5274. /* Find the cookie extension if it exists. */
  5275. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5276. if (extension == NULL) {
  5277. /* Push new cookie extension. */
  5278. ret = TLSX_Push(&ssl->extensions, TLSX_COOKIE, NULL, ssl->heap);
  5279. if (ret != 0)
  5280. return ret;
  5281. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5282. if (extension == NULL)
  5283. return MEMORY_E;
  5284. }
  5285. /* The Cookie structure has one byte for cookie data already. */
  5286. cookie = (Cookie*)XMALLOC(sizeof(Cookie) + len + macSz - 1, ssl->heap,
  5287. DYNAMIC_TYPE_TLSX);
  5288. if (cookie == NULL)
  5289. return MEMORY_E;
  5290. cookie->len = len + macSz;
  5291. XMEMCPY(&cookie->data, data, len);
  5292. if (mac != NULL)
  5293. XMEMCPY(&cookie->data + len, mac, macSz);
  5294. if (extension->data != NULL)
  5295. XFREE(extension->data, ssl->heap, DYNAMIC_TYPE_TLSX);
  5296. extension->data = (void*)cookie;
  5297. extension->resp = (byte)resp;
  5298. return 0;
  5299. }
  5300. #define CKE_FREE_ALL TLSX_Cookie_FreeAll
  5301. #define CKE_GET_SIZE TLSX_Cookie_GetSize
  5302. #define CKE_WRITE TLSX_Cookie_Write
  5303. #define CKE_PARSE TLSX_Cookie_Parse
  5304. #else
  5305. #define CKE_FREE_ALL(a, b) 0
  5306. #define CKE_GET_SIZE(a, b, c) 0
  5307. #define CKE_WRITE(a, b, c, d) 0
  5308. #define CKE_PARSE(a, b, c, d) 0
  5309. #endif
  5310. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  5311. /******************************************************************************/
  5312. /* Signature Algorithms */
  5313. /******************************************************************************/
  5314. /* Return the size of the SignatureAlgorithms extension's data.
  5315. *
  5316. * data Unused
  5317. * returns the length of data that will be in the extension.
  5318. */
  5319. static word16 TLSX_SignatureAlgorithms_GetSize(void* data)
  5320. {
  5321. WOLFSSL* ssl = (WOLFSSL*)data;
  5322. return OPAQUE16_LEN + ssl->suites->hashSigAlgoSz;
  5323. }
  5324. /* Creates a bit string of supported hash algorithms with RSA PSS.
  5325. * The bit string is used when determining which signature algorithm to use
  5326. * when creating the CertificateVerify message.
  5327. * Note: Valid data has an even length as each signature algorithm is two bytes.
  5328. *
  5329. * ssl The SSL/TLS object.
  5330. * input The buffer with the list of supported signature algorithms.
  5331. * length The length of the list in bytes.
  5332. * returns 0 on success, BUFFER_ERROR when the length is not even.
  5333. */
  5334. static int TLSX_SignatureAlgorithms_MapPss(WOLFSSL *ssl, const byte* input,
  5335. word16 length)
  5336. {
  5337. word16 i;
  5338. if ((length & 1) == 1)
  5339. return BUFFER_ERROR;
  5340. ssl->pssAlgo = 0;
  5341. for (i = 0; i < length; i += 2) {
  5342. if (input[i] == rsa_pss_sa_algo && input[i + 1] <= sha512_mac)
  5343. ssl->pssAlgo |= 1 << input[i + 1];
  5344. #ifdef WOLFSSL_TLS13
  5345. if (input[i] == rsa_pss_sa_algo && input[i + 1] >= pss_sha256 &&
  5346. input[i + 1] <= pss_sha512) {
  5347. ssl->pssAlgo |= 1 << input[i + 1];
  5348. }
  5349. #endif
  5350. }
  5351. return 0;
  5352. }
  5353. /* Writes the SignatureAlgorithms extension into the buffer.
  5354. *
  5355. * data Unused
  5356. * output The buffer to write the extension into.
  5357. * returns the length of data that was written.
  5358. */
  5359. static word16 TLSX_SignatureAlgorithms_Write(void* data, byte* output)
  5360. {
  5361. WOLFSSL* ssl = (WOLFSSL*)data;
  5362. c16toa(ssl->suites->hashSigAlgoSz, output);
  5363. XMEMCPY(output + OPAQUE16_LEN, ssl->suites->hashSigAlgo,
  5364. ssl->suites->hashSigAlgoSz);
  5365. TLSX_SignatureAlgorithms_MapPss(ssl, output + OPAQUE16_LEN,
  5366. ssl->suites->hashSigAlgoSz);
  5367. return OPAQUE16_LEN + ssl->suites->hashSigAlgoSz;
  5368. }
  5369. /* Parse the SignatureAlgorithms extension.
  5370. *
  5371. * ssl The SSL/TLS object.
  5372. * input The buffer with the extension data.
  5373. * length The length of the extension data.
  5374. * returns 0 on success, otherwise failure.
  5375. */
  5376. static int TLSX_SignatureAlgorithms_Parse(WOLFSSL *ssl, const byte* input,
  5377. word16 length, byte isRequest, Suites* suites)
  5378. {
  5379. word16 len;
  5380. if (!isRequest)
  5381. return BUFFER_ERROR;
  5382. /* Must contain a length and at least algorithm. */
  5383. if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
  5384. return BUFFER_ERROR;
  5385. ato16(input, &len);
  5386. input += OPAQUE16_LEN;
  5387. /* Algorithm array must fill rest of data. */
  5388. if (length != OPAQUE16_LEN + len)
  5389. return BUFFER_ERROR;
  5390. /* Sig Algo list size must be even. */
  5391. if (suites->hashSigAlgoSz % 2 != 0)
  5392. return BUFFER_ERROR;
  5393. /* truncate hashSigAlgo list if too long */
  5394. suites->hashSigAlgoSz = len;
  5395. if (suites->hashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
  5396. WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
  5397. suites->hashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
  5398. }
  5399. XMEMCPY(suites->hashSigAlgo, input, suites->hashSigAlgoSz);
  5400. return TLSX_SignatureAlgorithms_MapPss(ssl, input, len);
  5401. }
  5402. /* Sets a new SignatureAlgorithms extension into the extension list.
  5403. *
  5404. * extensions The list of extensions.
  5405. * data The extensions specific data.
  5406. * heap The heap used for allocation.
  5407. * returns 0 on success, otherwise failure.
  5408. */
  5409. static int TLSX_SetSignatureAlgorithms(TLSX** extensions, const void* data,
  5410. void* heap)
  5411. {
  5412. if (extensions == NULL)
  5413. return BAD_FUNC_ARG;
  5414. return TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS, data, heap);
  5415. }
  5416. #define SA_GET_SIZE TLSX_SignatureAlgorithms_GetSize
  5417. #define SA_WRITE TLSX_SignatureAlgorithms_Write
  5418. #define SA_PARSE TLSX_SignatureAlgorithms_Parse
  5419. #endif
  5420. /******************************************************************************/
  5421. /* Signature Algorithms Certificate */
  5422. /******************************************************************************/
  5423. #if defined(WOLFSSL_TLS13) && !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  5424. /* Return the size of the SignatureAlgorithms extension's data.
  5425. *
  5426. * data Unused
  5427. * returns the length of data that will be in the extension.
  5428. */
  5429. static word16 TLSX_SignatureAlgorithmsCert_GetSize(void* data)
  5430. {
  5431. WOLFSSL* ssl = (WOLFSSL*)data;
  5432. return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
  5433. }
  5434. /* Writes the SignatureAlgorithmsCert extension into the buffer.
  5435. *
  5436. * data Unused
  5437. * output The buffer to write the extension into.
  5438. * returns the length of data that was written.
  5439. */
  5440. static word16 TLSX_SignatureAlgorithmsCert_Write(void* data, byte* output)
  5441. {
  5442. WOLFSSL* ssl = (WOLFSSL*)data;
  5443. c16toa(ssl->certHashSigAlgoSz, output);
  5444. XMEMCPY(output + OPAQUE16_LEN, ssl->certHashSigAlgo,
  5445. ssl->certHashSigAlgoSz);
  5446. return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
  5447. }
  5448. /* Parse the SignatureAlgorithmsCert extension.
  5449. *
  5450. * ssl The SSL/TLS object.
  5451. * input The buffer with the extension data.
  5452. * length The length of the extension data.
  5453. * returns 0 on success, otherwise failure.
  5454. */
  5455. static int TLSX_SignatureAlgorithmsCert_Parse(WOLFSSL *ssl, const byte* input,
  5456. word16 length, byte isRequest)
  5457. {
  5458. word16 len;
  5459. if (!isRequest)
  5460. return BUFFER_ERROR;
  5461. /* Must contain a length and at least algorithm. */
  5462. if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
  5463. return BUFFER_ERROR;
  5464. ato16(input, &len);
  5465. input += OPAQUE16_LEN;
  5466. /* Algorithm array must fill rest of data. */
  5467. if (length != OPAQUE16_LEN + len)
  5468. return BUFFER_ERROR;
  5469. /* truncate hashSigAlgo list if too long */
  5470. ssl->certHashSigAlgoSz = len;
  5471. if (ssl->certHashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
  5472. WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
  5473. ssl->certHashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
  5474. }
  5475. XMEMCPY(ssl->certHashSigAlgo, input, ssl->certHashSigAlgoSz);
  5476. return 0;
  5477. }
  5478. /* Sets a new SignatureAlgorithmsCert extension into the extension list.
  5479. *
  5480. * extensions The list of extensions.
  5481. * data The extensions specific data.
  5482. * heap The heap used for allocation.
  5483. * returns 0 on success, otherwise failure.
  5484. */
  5485. static int TLSX_SetSignatureAlgorithmsCert(TLSX** extensions, const void* data,
  5486. void* heap)
  5487. {
  5488. if (extensions == NULL)
  5489. return BAD_FUNC_ARG;
  5490. return TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS_CERT, data, heap);
  5491. }
  5492. #define SAC_GET_SIZE TLSX_SignatureAlgorithmsCert_GetSize
  5493. #define SAC_WRITE TLSX_SignatureAlgorithmsCert_Write
  5494. #define SAC_PARSE TLSX_SignatureAlgorithmsCert_Parse
  5495. #endif /* WOLFSSL_TLS13 */
  5496. /******************************************************************************/
  5497. /* Key Share */
  5498. /******************************************************************************/
  5499. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  5500. /* Create a key share entry using named Diffie-Hellman parameters group.
  5501. * Generates a key pair.
  5502. *
  5503. * ssl The SSL/TLS object.
  5504. * kse The key share entry object.
  5505. * returns 0 on success, otherwise failure.
  5506. */
  5507. static int TLSX_KeyShare_GenDhKey(WOLFSSL *ssl, KeyShareEntry* kse)
  5508. {
  5509. int ret = 0;
  5510. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  5511. word32 pSz = 0, pvtSz = 0;
  5512. DhKey* dhKey = (DhKey*)kse->key;
  5513. /* Pick the parameters from the named group. */
  5514. #ifdef HAVE_PUBLIC_FFDHE
  5515. const DhParams* params = NULL;
  5516. switch (kse->group) {
  5517. #ifdef HAVE_FFDHE_2048
  5518. case WOLFSSL_FFDHE_2048:
  5519. params = wc_Dh_ffdhe2048_Get();
  5520. kse->keyLen = 29;
  5521. break;
  5522. #endif
  5523. #ifdef HAVE_FFDHE_3072
  5524. case WOLFSSL_FFDHE_3072:
  5525. params = wc_Dh_ffdhe3072_Get();
  5526. kse->keyLen = 34;
  5527. break;
  5528. #endif
  5529. #ifdef HAVE_FFDHE_4096
  5530. case WOLFSSL_FFDHE_4096:
  5531. params = wc_Dh_ffdhe4096_Get();
  5532. kse->keyLen = 39;
  5533. break;
  5534. #endif
  5535. #ifdef HAVE_FFDHE_6144
  5536. case WOLFSSL_FFDHE_6144:
  5537. params = wc_Dh_ffdhe6144_Get();
  5538. kse->keyLen = 46;
  5539. break;
  5540. #endif
  5541. #ifdef HAVE_FFDHE_8192
  5542. case WOLFSSL_FFDHE_8192:
  5543. params = wc_Dh_ffdhe8192_Get();
  5544. kse->keyLen = 52;
  5545. break;
  5546. #endif
  5547. default:
  5548. break;
  5549. }
  5550. if (params == NULL)
  5551. return BAD_FUNC_ARG;
  5552. pSz = params->p_len;
  5553. pvtSz = kse->keyLen;
  5554. #else
  5555. kse->keyLen = wc_DhGetNamedKeyMinSize(kse->group);
  5556. if (kse->keyLen == 0) {
  5557. return BAD_FUNC_ARG;
  5558. }
  5559. ret = wc_DhGetNamedKeyParamSize(kse->group, &pSz, NULL, NULL);
  5560. if (ret != 0) {
  5561. return BAD_FUNC_ARG;
  5562. }
  5563. pvtSz = kse->keyLen;
  5564. #endif
  5565. kse->pubKeyLen = pSz;
  5566. /* Trigger Key Generation */
  5567. if (kse->pubKey == NULL || kse->privKey == NULL) {
  5568. if (kse->key == NULL) {
  5569. kse->key = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap,
  5570. DYNAMIC_TYPE_DH);
  5571. if (kse->key == NULL)
  5572. return MEMORY_E;
  5573. /* Setup Key */
  5574. ret = wc_InitDhKey_ex((DhKey*)kse->key, ssl->heap, ssl->devId);
  5575. if (ret == 0) {
  5576. dhKey = (DhKey*)kse->key;
  5577. #ifdef HAVE_PUBLIC_FFDHE
  5578. ret = wc_DhSetKey(dhKey, params->p, params->p_len, params->g,
  5579. params->g_len);
  5580. #else
  5581. ret = wc_DhSetNamedKey(dhKey, kse->group);
  5582. #endif
  5583. }
  5584. }
  5585. /* Allocate space for the private and public key */
  5586. if (ret == 0 && kse->pubKey == NULL) {
  5587. kse->pubKey = (byte*)XMALLOC(kse->pubKeyLen, ssl->heap,
  5588. DYNAMIC_TYPE_PUBLIC_KEY);
  5589. if (kse->pubKey == NULL)
  5590. ret = MEMORY_E;
  5591. }
  5592. if (ret == 0 && kse->privKey == NULL) {
  5593. kse->privKey = (byte*)XMALLOC(kse->keyLen, ssl->heap,
  5594. DYNAMIC_TYPE_PRIVATE_KEY);
  5595. if (kse->privKey == NULL)
  5596. ret = MEMORY_E;
  5597. }
  5598. if (ret == 0) {
  5599. #if defined(WOLFSSL_STATIC_EPHEMERAL) && defined(WOLFSSL_DH_EXTRA)
  5600. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_DH, kse->key);
  5601. if (ret == 0) {
  5602. ret = wc_DhExportKeyPair(dhKey,
  5603. (byte*)kse->privKey, &kse->keyLen, /* private */
  5604. kse->pubKey, &kse->pubKeyLen /* public */
  5605. );
  5606. }
  5607. else
  5608. #endif
  5609. {
  5610. /* Generate a new key pair */
  5611. /* For async this is called once and when event is done, the
  5612. * provided buffers will be populated.
  5613. * Final processing is zero pad below. */
  5614. ret = DhGenKeyPair(ssl, dhKey,
  5615. (byte*)kse->privKey, &kse->keyLen, /* private */
  5616. kse->pubKey, &kse->pubKeyLen /* public */
  5617. );
  5618. #ifdef WOLFSSL_ASYNC_CRYPT
  5619. if (ret == WC_PENDING_E) {
  5620. return ret;
  5621. }
  5622. #endif
  5623. }
  5624. }
  5625. }
  5626. if (ret == 0) {
  5627. if (pSz != kse->pubKeyLen) {
  5628. /* Zero pad the front of the public key to match prime "p" size */
  5629. XMEMMOVE(kse->pubKey + pSz - kse->pubKeyLen, kse->pubKey,
  5630. kse->pubKeyLen);
  5631. XMEMSET(kse->pubKey, 0, pSz - kse->pubKeyLen);
  5632. kse->pubKeyLen = pSz;
  5633. }
  5634. if (pvtSz != kse->keyLen) {
  5635. /* Zero pad the front of the private key */
  5636. XMEMMOVE(kse->privKey + pvtSz - kse->keyLen, kse->privKey,
  5637. kse->keyLen);
  5638. XMEMSET(kse->privKey, 0, pvtSz - kse->keyLen);
  5639. kse->keyLen = pvtSz;
  5640. }
  5641. #ifdef WOLFSSL_DEBUG_TLS
  5642. WOLFSSL_MSG("Public DH Key");
  5643. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5644. #endif
  5645. }
  5646. /* Always release the DH key to free up memory.
  5647. * The DhKey will be setup again in TLSX_KeyShare_ProcessDh */
  5648. if (dhKey != NULL)
  5649. wc_FreeDhKey(dhKey);
  5650. if (kse->key != NULL) {
  5651. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_DH);
  5652. kse->key = NULL;
  5653. }
  5654. if (ret != 0) {
  5655. /* Cleanup on error, otherwise data owned by key share entry */
  5656. if (kse->privKey != NULL) {
  5657. XFREE(kse->privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5658. kse->privKey = NULL;
  5659. }
  5660. if (kse->pubKey != NULL) {
  5661. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5662. kse->pubKey = NULL;
  5663. }
  5664. }
  5665. #else
  5666. (void)ssl;
  5667. (void)kse;
  5668. ret = NOT_COMPILED_IN;
  5669. WOLFSSL_ERROR_VERBOSE(ret);
  5670. #endif
  5671. return ret;
  5672. }
  5673. /* Create a key share entry using X25519 parameters group.
  5674. * Generates a key pair.
  5675. *
  5676. * ssl The SSL/TLS object.
  5677. * kse The key share entry object.
  5678. * returns 0 on success, otherwise failure.
  5679. */
  5680. static int TLSX_KeyShare_GenX25519Key(WOLFSSL *ssl, KeyShareEntry* kse)
  5681. {
  5682. int ret = 0;
  5683. #ifdef HAVE_CURVE25519
  5684. curve25519_key* key = (curve25519_key*)kse->key;
  5685. if (kse->key == NULL) {
  5686. /* Allocate a Curve25519 key to hold private key. */
  5687. kse->key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
  5688. DYNAMIC_TYPE_PRIVATE_KEY);
  5689. if (kse->key == NULL) {
  5690. WOLFSSL_MSG("GenX25519Key memory error");
  5691. return MEMORY_E;
  5692. }
  5693. /* Make an Curve25519 key. */
  5694. ret = wc_curve25519_init_ex((curve25519_key*)kse->key, ssl->heap,
  5695. INVALID_DEVID);
  5696. if (ret == 0) {
  5697. /* setting "key" means okay to call wc_curve25519_free */
  5698. key = (curve25519_key*)kse->key;
  5699. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5700. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_CURVE25519, kse->key);
  5701. if (ret != 0)
  5702. #endif
  5703. {
  5704. ret = wc_curve25519_make_key(ssl->rng, CURVE25519_KEYSIZE, key);
  5705. }
  5706. }
  5707. }
  5708. if (ret == 0 && kse->pubKey == NULL) {
  5709. /* Allocate space for the public key. */
  5710. kse->pubKey = (byte*)XMALLOC(CURVE25519_KEYSIZE, ssl->heap,
  5711. DYNAMIC_TYPE_PUBLIC_KEY);
  5712. if (kse->pubKey == NULL) {
  5713. WOLFSSL_MSG("GenX25519Key pub memory error");
  5714. ret = MEMORY_E;
  5715. }
  5716. }
  5717. if (ret == 0) {
  5718. /* Export Curve25519 public key. */
  5719. kse->pubKeyLen = CURVE25519_KEYSIZE;
  5720. if (wc_curve25519_export_public_ex(key, kse->pubKey, &kse->pubKeyLen,
  5721. EC25519_LITTLE_ENDIAN) != 0) {
  5722. ret = ECC_EXPORT_ERROR;
  5723. WOLFSSL_ERROR_VERBOSE(ret);
  5724. }
  5725. kse->pubKeyLen = CURVE25519_KEYSIZE; /* always CURVE25519_KEYSIZE */
  5726. }
  5727. #ifdef WOLFSSL_DEBUG_TLS
  5728. if (ret == 0) {
  5729. WOLFSSL_MSG("Public Curve25519 Key");
  5730. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5731. }
  5732. #endif
  5733. if (ret != 0) {
  5734. /* Data owned by key share entry otherwise. */
  5735. if (kse->pubKey != NULL) {
  5736. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5737. kse->pubKey = NULL;
  5738. }
  5739. if (key != NULL)
  5740. wc_curve25519_free(key);
  5741. if (kse->key != NULL) {
  5742. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5743. kse->key = NULL;
  5744. }
  5745. }
  5746. #else
  5747. (void)ssl;
  5748. (void)kse;
  5749. ret = NOT_COMPILED_IN;
  5750. WOLFSSL_ERROR_VERBOSE(ret);
  5751. #endif /* HAVE_CURVE25519 */
  5752. return ret;
  5753. }
  5754. /* Create a key share entry using X448 parameters group.
  5755. * Generates a key pair.
  5756. *
  5757. * ssl The SSL/TLS object.
  5758. * kse The key share entry object.
  5759. * returns 0 on success, otherwise failure.
  5760. */
  5761. static int TLSX_KeyShare_GenX448Key(WOLFSSL *ssl, KeyShareEntry* kse)
  5762. {
  5763. int ret = 0;
  5764. #ifdef HAVE_CURVE448
  5765. curve448_key* key = (curve448_key*)kse->key;
  5766. if (kse->key == NULL) {
  5767. /* Allocate a Curve448 key to hold private key. */
  5768. kse->key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
  5769. DYNAMIC_TYPE_PRIVATE_KEY);
  5770. if (kse->key == NULL) {
  5771. WOLFSSL_MSG("GenX448Key memory error");
  5772. return MEMORY_E;
  5773. }
  5774. /* Make an Curve448 key. */
  5775. ret = wc_curve448_init((curve448_key*)kse->key);
  5776. if (ret == 0) {
  5777. key = (curve448_key*)kse->key;
  5778. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5779. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_CURVE448, kse->key);
  5780. if (ret != 0)
  5781. #endif
  5782. {
  5783. ret = wc_curve448_make_key(ssl->rng, CURVE448_KEY_SIZE, key);
  5784. }
  5785. }
  5786. }
  5787. if (ret == 0 && kse->pubKey == NULL) {
  5788. /* Allocate space for the public key. */
  5789. kse->pubKey = (byte*)XMALLOC(CURVE448_KEY_SIZE, ssl->heap,
  5790. DYNAMIC_TYPE_PUBLIC_KEY);
  5791. if (kse->pubKey == NULL) {
  5792. WOLFSSL_MSG("GenX448Key pub memory error");
  5793. ret = MEMORY_E;
  5794. }
  5795. }
  5796. if (ret == 0) {
  5797. /* Export Curve448 public key. */
  5798. kse->pubKeyLen = CURVE448_KEY_SIZE;
  5799. if (wc_curve448_export_public_ex(key, kse->pubKey, &kse->pubKeyLen,
  5800. EC448_LITTLE_ENDIAN) != 0) {
  5801. ret = ECC_EXPORT_ERROR;
  5802. }
  5803. kse->pubKeyLen = CURVE448_KEY_SIZE; /* always CURVE448_KEY_SIZE */
  5804. }
  5805. #ifdef WOLFSSL_DEBUG_TLS
  5806. if (ret == 0) {
  5807. WOLFSSL_MSG("Public Curve448 Key");
  5808. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5809. }
  5810. #endif
  5811. if (ret != 0) {
  5812. /* Data owned by key share entry otherwise. */
  5813. if (kse->pubKey != NULL) {
  5814. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5815. kse->pubKey = NULL;
  5816. }
  5817. if (key != NULL)
  5818. wc_curve448_free(key);
  5819. if (kse->key != NULL) {
  5820. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5821. kse->key = NULL;
  5822. }
  5823. }
  5824. #else
  5825. (void)ssl;
  5826. (void)kse;
  5827. ret = NOT_COMPILED_IN;
  5828. WOLFSSL_ERROR_VERBOSE(ret);
  5829. #endif /* HAVE_CURVE448 */
  5830. return ret;
  5831. }
  5832. /* Create a key share entry using named elliptic curve parameters group.
  5833. * Generates a key pair.
  5834. *
  5835. * ssl The SSL/TLS object.
  5836. * kse The key share entry object.
  5837. * returns 0 on success, otherwise failure.
  5838. */
  5839. static int TLSX_KeyShare_GenEccKey(WOLFSSL *ssl, KeyShareEntry* kse)
  5840. {
  5841. int ret = 0;
  5842. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT)
  5843. word32 keySize = 0;
  5844. word16 curveId = (word16) ECC_CURVE_INVALID;
  5845. ecc_key* eccKey = (ecc_key*)kse->key;
  5846. /* TODO: [TLS13] The key sizes should come from wolfcrypt. */
  5847. /* Translate named group to a curve id. */
  5848. switch (kse->group) {
  5849. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  5850. #ifndef NO_ECC_SECP
  5851. case WOLFSSL_ECC_SECP256R1:
  5852. curveId = ECC_SECP256R1;
  5853. keySize = 32;
  5854. break;
  5855. #endif /* !NO_ECC_SECP */
  5856. #endif
  5857. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  5858. #ifndef NO_ECC_SECP
  5859. case WOLFSSL_ECC_SECP384R1:
  5860. curveId = ECC_SECP384R1;
  5861. keySize = 48;
  5862. break;
  5863. #endif /* !NO_ECC_SECP */
  5864. #endif
  5865. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  5866. #ifndef NO_ECC_SECP
  5867. case WOLFSSL_ECC_SECP521R1:
  5868. curveId = ECC_SECP521R1;
  5869. keySize = 66;
  5870. break;
  5871. #endif /* !NO_ECC_SECP */
  5872. #endif
  5873. default:
  5874. WOLFSSL_ERROR_VERBOSE(BAD_FUNC_ARG);
  5875. return BAD_FUNC_ARG;
  5876. }
  5877. if (kse->key == NULL) {
  5878. kse->keyLen = keySize;
  5879. kse->pubKeyLen = keySize * 2 + 1;
  5880. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER >= 115)
  5881. ret = tsip_Tls13GenEccKeyPair(ssl, kse);
  5882. if (ret != CRYPTOCB_UNAVAILABLE) {
  5883. return ret;
  5884. }
  5885. #endif
  5886. /* Allocate an ECC key to hold private key. */
  5887. kse->key = (byte*)XMALLOC(sizeof(ecc_key), ssl->heap, DYNAMIC_TYPE_ECC);
  5888. if (kse->key == NULL) {
  5889. WOLFSSL_MSG("EccTempKey Memory error");
  5890. return MEMORY_E;
  5891. }
  5892. /* Make an ECC key */
  5893. ret = wc_ecc_init_ex((ecc_key*)kse->key, ssl->heap, ssl->devId);
  5894. if (ret == 0) {
  5895. /* setting eccKey means okay to call wc_ecc_free */
  5896. eccKey = (ecc_key*)kse->key;
  5897. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5898. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_ECDH, kse->key);
  5899. if (ret != 0)
  5900. #endif
  5901. {
  5902. /* set curve info for EccMakeKey "peer" info */
  5903. ret = wc_ecc_set_curve(eccKey, kse->keyLen, curveId);
  5904. if (ret == 0) {
  5905. /* Generate ephemeral ECC key */
  5906. /* For async this is called once and when event is done, the
  5907. * provided buffers in key be populated.
  5908. * Final processing is x963 key export below. */
  5909. ret = EccMakeKey(ssl, eccKey, eccKey);
  5910. }
  5911. #ifdef WOLFSSL_ASYNC_CRYPT
  5912. if (ret == WC_PENDING_E)
  5913. return ret;
  5914. #endif
  5915. }
  5916. }
  5917. }
  5918. if (ret == 0 && kse->pubKey == NULL) {
  5919. /* Allocate space for the public key */
  5920. kse->pubKey = (byte*)XMALLOC(kse->pubKeyLen, ssl->heap,
  5921. DYNAMIC_TYPE_PUBLIC_KEY);
  5922. if (kse->pubKey == NULL) {
  5923. WOLFSSL_MSG("Key data Memory error");
  5924. ret = MEMORY_E;
  5925. }
  5926. }
  5927. if (ret == 0) {
  5928. XMEMSET(kse->pubKey, 0, kse->pubKeyLen);
  5929. /* Export public key. */
  5930. PRIVATE_KEY_UNLOCK();
  5931. if (wc_ecc_export_x963(eccKey, kse->pubKey, &kse->pubKeyLen) != 0) {
  5932. ret = ECC_EXPORT_ERROR;
  5933. WOLFSSL_ERROR_VERBOSE(ret);
  5934. }
  5935. PRIVATE_KEY_LOCK();
  5936. }
  5937. #ifdef WOLFSSL_DEBUG_TLS
  5938. if (ret == 0) {
  5939. WOLFSSL_MSG("Public ECC Key");
  5940. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5941. }
  5942. #endif
  5943. if (ret != 0) {
  5944. /* Cleanup on error, otherwise data owned by key share entry */
  5945. if (kse->pubKey != NULL) {
  5946. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5947. kse->pubKey = NULL;
  5948. }
  5949. if (eccKey != NULL)
  5950. wc_ecc_free(eccKey);
  5951. if (kse->key != NULL) {
  5952. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5953. kse->key = NULL;
  5954. }
  5955. }
  5956. #else
  5957. (void)ssl;
  5958. (void)kse;
  5959. ret = NOT_COMPILED_IN;
  5960. WOLFSSL_ERROR_VERBOSE(ret);
  5961. #endif /* HAVE_ECC && HAVE_ECC_KEY_EXPORT */
  5962. return ret;
  5963. }
  5964. #ifdef HAVE_PQC
  5965. #ifdef WOLFSSL_WC_KYBER
  5966. static int kyber_id2type(int id, int *type)
  5967. {
  5968. int ret = 0;
  5969. switch (id) {
  5970. #ifdef WOLFSSL_KYBER512
  5971. case WOLFSSL_KYBER_LEVEL1:
  5972. *type = KYBER512;
  5973. break;
  5974. #endif
  5975. #ifdef WOLFSSL_KYBER768
  5976. case WOLFSSL_KYBER_LEVEL3:
  5977. *type = KYBER768;
  5978. break;
  5979. #endif
  5980. #ifdef WOLFSSL_KYBER1024
  5981. case WOLFSSL_KYBER_LEVEL5:
  5982. *type = KYBER1024;
  5983. break;
  5984. #endif
  5985. default:
  5986. ret = NOT_COMPILED_IN;
  5987. break;
  5988. }
  5989. return ret;
  5990. }
  5991. #elif defined(HAVE_LIBOQS)
  5992. /* Transform a group ID into an OQS Algorithm name as a string. */
  5993. static const char* OQS_ID2name(int id)
  5994. {
  5995. switch (id) {
  5996. case WOLFSSL_KYBER_LEVEL1: return OQS_KEM_alg_kyber_512;
  5997. case WOLFSSL_KYBER_LEVEL3: return OQS_KEM_alg_kyber_768;
  5998. case WOLFSSL_KYBER_LEVEL5: return OQS_KEM_alg_kyber_1024;
  5999. case WOLFSSL_NTRU_HPS_LEVEL1: return OQS_KEM_alg_ntru_hps2048509;
  6000. case WOLFSSL_NTRU_HPS_LEVEL3: return OQS_KEM_alg_ntru_hps2048677;
  6001. case WOLFSSL_NTRU_HPS_LEVEL5: return OQS_KEM_alg_ntru_hps4096821;
  6002. case WOLFSSL_NTRU_HRSS_LEVEL3: return OQS_KEM_alg_ntru_hrss701;
  6003. case WOLFSSL_SABER_LEVEL1: return OQS_KEM_alg_saber_lightsaber;
  6004. case WOLFSSL_SABER_LEVEL3: return OQS_KEM_alg_saber_saber;
  6005. case WOLFSSL_SABER_LEVEL5: return OQS_KEM_alg_saber_firesaber;
  6006. case WOLFSSL_KYBER_90S_LEVEL1: return OQS_KEM_alg_kyber_512_90s;
  6007. case WOLFSSL_KYBER_90S_LEVEL3: return OQS_KEM_alg_kyber_768_90s;
  6008. case WOLFSSL_KYBER_90S_LEVEL5: return OQS_KEM_alg_kyber_1024_90s;
  6009. default: break;
  6010. }
  6011. return NULL;
  6012. }
  6013. #endif /* HAVE_LIBOQS */
  6014. typedef struct PqcHybridMapping {
  6015. int hybrid;
  6016. int ecc;
  6017. int pqc;
  6018. } PqcHybridMapping;
  6019. static const PqcHybridMapping pqc_hybrid_mapping[] = {
  6020. {.hybrid = WOLFSSL_P256_NTRU_HPS_LEVEL1, .ecc = WOLFSSL_ECC_SECP256R1,
  6021. .pqc = WOLFSSL_NTRU_HPS_LEVEL1},
  6022. {.hybrid = WOLFSSL_P384_NTRU_HPS_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  6023. .pqc = WOLFSSL_NTRU_HPS_LEVEL3},
  6024. {.hybrid = WOLFSSL_P521_NTRU_HPS_LEVEL5, .ecc = WOLFSSL_ECC_SECP521R1,
  6025. .pqc = WOLFSSL_NTRU_HPS_LEVEL5},
  6026. {.hybrid = WOLFSSL_P384_NTRU_HRSS_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  6027. .pqc = WOLFSSL_NTRU_HRSS_LEVEL3},
  6028. {.hybrid = WOLFSSL_P256_SABER_LEVEL1, .ecc = WOLFSSL_ECC_SECP256R1,
  6029. .pqc = WOLFSSL_SABER_LEVEL1},
  6030. {.hybrid = WOLFSSL_P384_SABER_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  6031. .pqc = WOLFSSL_SABER_LEVEL3},
  6032. {.hybrid = WOLFSSL_P521_SABER_LEVEL5, .ecc = WOLFSSL_ECC_SECP521R1,
  6033. .pqc = WOLFSSL_SABER_LEVEL5},
  6034. {.hybrid = WOLFSSL_P256_KYBER_LEVEL1, .ecc = WOLFSSL_ECC_SECP256R1,
  6035. .pqc = WOLFSSL_KYBER_LEVEL1},
  6036. {.hybrid = WOLFSSL_P384_KYBER_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  6037. .pqc = WOLFSSL_KYBER_LEVEL3},
  6038. {.hybrid = WOLFSSL_P521_KYBER_LEVEL5, .ecc = WOLFSSL_ECC_SECP521R1,
  6039. .pqc = WOLFSSL_KYBER_LEVEL5},
  6040. {.hybrid = WOLFSSL_P256_KYBER_90S_LEVEL1, .ecc = WOLFSSL_ECC_SECP256R1,
  6041. .pqc = WOLFSSL_KYBER_90S_LEVEL1},
  6042. {.hybrid = WOLFSSL_P384_KYBER_90S_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  6043. .pqc = WOLFSSL_KYBER_90S_LEVEL3},
  6044. {.hybrid = WOLFSSL_P521_KYBER_90S_LEVEL5, .ecc = WOLFSSL_ECC_SECP521R1,
  6045. .pqc = WOLFSSL_KYBER_90S_LEVEL5},
  6046. {.hybrid = 0, .ecc = 0, .pqc = 0}
  6047. };
  6048. /* This will map an ecc-pqs hybrid group into its ecc group and pqc kem group.
  6049. * If it cannot find a mapping then *pqc is set to group. ecc is optional. */
  6050. static void findEccPqc(int *ecc, int *pqc, int group)
  6051. {
  6052. int i;
  6053. if (pqc == NULL) {
  6054. return;
  6055. }
  6056. *pqc = 0;
  6057. if (ecc != NULL) {
  6058. *ecc = 0;
  6059. }
  6060. for (i = 0; pqc_hybrid_mapping[i].hybrid != 0; i++) {
  6061. if (pqc_hybrid_mapping[i].hybrid == group) {
  6062. *pqc = pqc_hybrid_mapping[i].pqc;
  6063. if (ecc != NULL) {
  6064. *ecc = pqc_hybrid_mapping[i].ecc;
  6065. }
  6066. break;
  6067. }
  6068. }
  6069. if (*pqc == 0) {
  6070. /* It is not a hybrid, so maybe its simple. */
  6071. *pqc = group;
  6072. }
  6073. }
  6074. /* Create a key share entry using liboqs parameters group.
  6075. * Generates a key pair.
  6076. *
  6077. * ssl The SSL/TLS object.
  6078. * kse The key share entry object.
  6079. * returns 0 on success, otherwise failure.
  6080. */
  6081. #ifdef WOLFSSL_WC_KYBER
  6082. static int TLSX_KeyShare_GenPqcKey(WOLFSSL *ssl, KeyShareEntry* kse)
  6083. {
  6084. int ret = 0;
  6085. int type = 0;
  6086. KyberKey kem[1];
  6087. byte* pubKey = NULL;
  6088. byte* privKey = NULL;
  6089. KeyShareEntry *ecc_kse = NULL;
  6090. int oqs_group = 0;
  6091. int ecc_group = 0;
  6092. word32 privSz = 0;
  6093. word32 pubSz = 0;
  6094. findEccPqc(&ecc_group, &oqs_group, kse->group);
  6095. ret = kyber_id2type(oqs_group, &type);
  6096. if (ret == NOT_COMPILED_IN) {
  6097. WOLFSSL_MSG("Invalid Kyber algorithm specified.");
  6098. ret = BAD_FUNC_ARG;
  6099. }
  6100. if (ret == 0) {
  6101. ret = wc_KyberKey_Init(type, kem, ssl->heap, ssl->devId);
  6102. if (ret != 0) {
  6103. WOLFSSL_MSG("Failed to intialize Kyber Key.");
  6104. }
  6105. }
  6106. if (ret == 0) {
  6107. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  6108. DYNAMIC_TYPE_TLSX);
  6109. if (ecc_kse == NULL) {
  6110. WOLFSSL_MSG("ecc_kse memory allocation failure");
  6111. ret = MEMORY_ERROR;
  6112. }
  6113. }
  6114. if (ret == 0) {
  6115. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  6116. ret = wc_KyberKey_PrivateKeySize(kem, &privSz);
  6117. }
  6118. if (ret == 0) {
  6119. ret = wc_KyberKey_PublicKeySize(kem, &pubSz);
  6120. }
  6121. if (ret == 0 && ecc_group != 0) {
  6122. ecc_kse->group = ecc_group;
  6123. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  6124. /* If fail, no error message, TLSX_KeyShare_GenEccKey will do it. */
  6125. }
  6126. if (ret == 0) {
  6127. pubKey = (byte*)XMALLOC(ecc_kse->pubKeyLen + pubSz, ssl->heap,
  6128. DYNAMIC_TYPE_PUBLIC_KEY);
  6129. if (pubKey == NULL) {
  6130. WOLFSSL_MSG("pubkey memory allocation failure");
  6131. ret = MEMORY_ERROR;
  6132. }
  6133. }
  6134. if (ret == 0) {
  6135. privKey = (byte*)XMALLOC(privSz, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6136. if (privKey == NULL) {
  6137. WOLFSSL_MSG("privkey memory allocation failure");
  6138. ret = MEMORY_ERROR;
  6139. }
  6140. }
  6141. if (ret == 0) {
  6142. ret = wc_KyberKey_MakeKey(kem, ssl->rng);
  6143. if (ret != 0) {
  6144. WOLFSSL_MSG("lKyber keygen failure");
  6145. }
  6146. }
  6147. if (ret == 0) {
  6148. ret = wc_KyberKey_EncodePublicKey(kem, pubKey + ecc_kse->pubKeyLen,
  6149. pubSz);
  6150. }
  6151. if (ret == 0) {
  6152. ret = wc_KyberKey_EncodePrivateKey(kem, privKey, privSz);
  6153. }
  6154. if (ret == 0) {
  6155. XMEMCPY(pubKey, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  6156. kse->pubKey = pubKey;
  6157. kse->pubKeyLen = ecc_kse->pubKeyLen + pubSz;
  6158. pubKey = NULL;
  6159. /* Note we are saving the OQS private key and ECC private key
  6160. * separately. That's because the ECC private key is not simply a
  6161. * buffer. Its is an ecc_key struct.
  6162. */
  6163. kse->privKey = privKey;
  6164. privKey = NULL;
  6165. kse->key = ecc_kse->key;
  6166. ecc_kse->key = NULL;
  6167. }
  6168. #ifdef WOLFSSL_DEBUG_TLS
  6169. WOLFSSL_MSG("Public Kyber Key");
  6170. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen );
  6171. #endif
  6172. wc_KyberKey_Free(kem);
  6173. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  6174. if (pubKey != NULL)
  6175. XFREE(pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6176. if (privKey != NULL)
  6177. XFREE(privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6178. return ret;
  6179. }
  6180. #elif defined(HAVE_LIBOQS)
  6181. static int TLSX_KeyShare_GenPqcKey(WOLFSSL *ssl, KeyShareEntry* kse)
  6182. {
  6183. int ret = 0;
  6184. const char* algName = NULL;
  6185. OQS_KEM* kem = NULL;
  6186. byte* pubKey = NULL;
  6187. byte* privKey = NULL;
  6188. KeyShareEntry *ecc_kse = NULL;
  6189. int oqs_group = 0;
  6190. int ecc_group = 0;
  6191. findEccPqc(&ecc_group, &oqs_group, kse->group);
  6192. algName = OQS_ID2name(oqs_group);
  6193. if (algName == NULL) {
  6194. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  6195. return BAD_FUNC_ARG;
  6196. }
  6197. kem = OQS_KEM_new(algName);
  6198. if (kem == NULL) {
  6199. WOLFSSL_MSG("Error creating OQS KEM, ensure algorithm support"
  6200. "was enabled in liboqs.");
  6201. return BAD_FUNC_ARG;
  6202. }
  6203. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  6204. DYNAMIC_TYPE_TLSX);
  6205. if (ecc_kse == NULL) {
  6206. WOLFSSL_MSG("ecc_kse memory allocation failure");
  6207. ret = MEMORY_ERROR;
  6208. }
  6209. if (ret == 0) {
  6210. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  6211. }
  6212. if (ret == 0 && ecc_group != 0) {
  6213. ecc_kse->group = ecc_group;
  6214. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  6215. /* If fail, no error message, TLSX_KeyShare_GenEccKey will do it. */
  6216. }
  6217. if (ret == 0) {
  6218. pubKey = (byte*)XMALLOC(ecc_kse->pubKeyLen + kem->length_public_key,
  6219. ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6220. if (pubKey == NULL) {
  6221. WOLFSSL_MSG("pubkey memory allocation failure");
  6222. ret = MEMORY_ERROR;
  6223. }
  6224. }
  6225. if (ret == 0) {
  6226. privKey = (byte*)XMALLOC(kem->length_secret_key,
  6227. ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6228. if (privKey == NULL) {
  6229. WOLFSSL_MSG("privkey memory allocation failure");
  6230. ret = MEMORY_ERROR;
  6231. }
  6232. }
  6233. if (ret == 0) {
  6234. if (OQS_KEM_keypair(kem, pubKey + ecc_kse->pubKeyLen, privKey) ==
  6235. OQS_SUCCESS) {
  6236. XMEMCPY(pubKey, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  6237. kse->pubKey = pubKey;
  6238. kse->pubKeyLen = ecc_kse->pubKeyLen +
  6239. (word32) kem->length_public_key;
  6240. pubKey = NULL;
  6241. /* Note we are saving the OQS private key and ECC private key
  6242. * separately. That's because the ECC private key is not simply a
  6243. * buffer. Its is an ecc_key struct.
  6244. */
  6245. kse->privKey = privKey;
  6246. privKey = NULL;
  6247. kse->key = ecc_kse->key;
  6248. ecc_kse->key = NULL;
  6249. ret = 0;
  6250. }
  6251. else {
  6252. WOLFSSL_MSG("liboqs keygen failure");
  6253. ret = BAD_FUNC_ARG;
  6254. WOLFSSL_ERROR_VERBOSE(ret);
  6255. }
  6256. }
  6257. #ifdef WOLFSSL_DEBUG_TLS
  6258. WOLFSSL_MSG("Public liboqs Key");
  6259. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  6260. #endif
  6261. OQS_KEM_free(kem);
  6262. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  6263. if (pubKey != NULL)
  6264. XFREE(pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6265. if (privKey != NULL)
  6266. XFREE(privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6267. return ret;
  6268. }
  6269. #elif defined(HAVE_PQM4)
  6270. static int TLSX_KeyShare_GenPqcKey(WOLFSSL *ssl, KeyShareEntry* kse)
  6271. {
  6272. /* This assumes KYBER LEVEL 1 (512) implementation is compiled in. */
  6273. int ret = 0;
  6274. byte* pubKey = NULL;
  6275. byte* privKey = NULL;
  6276. KeyShareEntry *ecc_kse = NULL;
  6277. int oqs_group = 0;
  6278. int ecc_group = 0;
  6279. findEccPqc(&ecc_group, &oqs_group, kse->group);
  6280. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  6281. DYNAMIC_TYPE_TLSX);
  6282. if (ecc_kse == NULL) {
  6283. WOLFSSL_MSG("ecc_kse memory allocation failure");
  6284. ret = MEMORY_ERROR;
  6285. }
  6286. if (ret == 0) {
  6287. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  6288. }
  6289. if (ret == 0 && ecc_group != 0) {
  6290. ecc_kse->group = ecc_group;
  6291. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  6292. /* If fail, no error message, TLSX_KeyShare_GenEccKey will do it. */
  6293. }
  6294. if (ret == 0) {
  6295. pubKey = (byte*)XMALLOC(ecc_kse->pubKeyLen + PQM4_PUBLIC_KEY_LENGTH,
  6296. ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6297. if (pubKey == NULL) {
  6298. WOLFSSL_MSG("pubkey memory allocation failure");
  6299. ret = MEMORY_ERROR;
  6300. }
  6301. }
  6302. if (ret == 0) {
  6303. privKey = (byte*)XMALLOC(PQM4_PRIVATE_KEY_LENGTH,
  6304. ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6305. if (privKey == NULL) {
  6306. WOLFSSL_MSG("privkey memory allocation failure");
  6307. ret = MEMORY_ERROR;
  6308. }
  6309. }
  6310. if (ret == 0) {
  6311. if (crypto_kem_keypair(pubKey + ecc_kse->pubKeyLen, privKey) == 0) {
  6312. XMEMCPY(pubKey, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  6313. kse->pubKey = pubKey;
  6314. kse->pubKeyLen = ecc_kse->pubKeyLen +
  6315. (word32) PQM4_PUBLIC_KEY_LENGTH;
  6316. pubKey = NULL;
  6317. /* Note we are saving the PQ private key and ECC private key
  6318. * separately. That's because the ECC private key is not simply a
  6319. * buffer. Its is an ecc_key struct.
  6320. */
  6321. kse->privKey = privKey;
  6322. privKey = NULL;
  6323. kse->key = ecc_kse->key;
  6324. ecc_kse->key = NULL;
  6325. ret = 0;
  6326. }
  6327. else {
  6328. WOLFSSL_MSG("liboqs keygen failure");
  6329. ret = BAD_FUNC_ARG;
  6330. WOLFSSL_ERROR_VERBOSE(ret);
  6331. }
  6332. }
  6333. #ifdef WOLFSSL_DEBUG_TLS
  6334. WOLFSSL_MSG("Public PQM4 Key");
  6335. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen );
  6336. #endif
  6337. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  6338. if (pubKey != NULL)
  6339. XFREE(pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6340. if (privKey != NULL)
  6341. XFREE(privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6342. return ret;
  6343. }
  6344. #endif /* HAVE_PQM4 */
  6345. #endif /* HAVE_PQC */
  6346. /* Generate a secret/key using the key share entry.
  6347. *
  6348. * ssl The SSL/TLS object.
  6349. * kse The key share entry holding peer data.
  6350. */
  6351. static int TLSX_KeyShare_GenKey(WOLFSSL *ssl, KeyShareEntry *kse)
  6352. {
  6353. int ret;
  6354. /* Named FFDHE groups have a bit set to identify them. */
  6355. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(kse->group))
  6356. ret = TLSX_KeyShare_GenDhKey(ssl, kse);
  6357. else if (kse->group == WOLFSSL_ECC_X25519)
  6358. ret = TLSX_KeyShare_GenX25519Key(ssl, kse);
  6359. else if (kse->group == WOLFSSL_ECC_X448)
  6360. ret = TLSX_KeyShare_GenX448Key(ssl, kse);
  6361. #ifdef HAVE_PQC
  6362. else if (WOLFSSL_NAMED_GROUP_IS_PQC(kse->group))
  6363. ret = TLSX_KeyShare_GenPqcKey(ssl, kse);
  6364. #endif
  6365. else
  6366. ret = TLSX_KeyShare_GenEccKey(ssl, kse);
  6367. #ifdef WOLFSSL_ASYNC_CRYPT
  6368. kse->lastRet = ret;
  6369. #endif
  6370. return ret;
  6371. }
  6372. /* Free the key share dynamic data.
  6373. *
  6374. * list The linked list of key share entry objects.
  6375. * heap The heap used for allocation.
  6376. */
  6377. static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap)
  6378. {
  6379. KeyShareEntry* current;
  6380. while ((current = list) != NULL) {
  6381. list = current->next;
  6382. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(current->group)) {
  6383. #ifndef NO_DH
  6384. wc_FreeDhKey((DhKey*)current->key);
  6385. #endif
  6386. }
  6387. else if (current->group == WOLFSSL_ECC_X25519) {
  6388. #ifdef HAVE_CURVE25519
  6389. wc_curve25519_free((curve25519_key*)current->key);
  6390. #endif
  6391. }
  6392. else if (current->group == WOLFSSL_ECC_X448) {
  6393. #ifdef HAVE_CURVE448
  6394. wc_curve448_free((curve448_key*)current->key);
  6395. #endif
  6396. }
  6397. #ifdef HAVE_PQC
  6398. else if (WOLFSSL_NAMED_GROUP_IS_PQC(current->group) &&
  6399. current->key != NULL) {
  6400. ForceZero((byte*)current->key, current->keyLen);
  6401. }
  6402. #endif
  6403. else {
  6404. #ifdef HAVE_ECC
  6405. wc_ecc_free((ecc_key*)current->key);
  6406. #endif
  6407. }
  6408. XFREE(current->key, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6409. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  6410. XFREE(current->privKey, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6411. #endif
  6412. XFREE(current->pubKey, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6413. XFREE(current->ke, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6414. XFREE(current, heap, DYNAMIC_TYPE_TLSX);
  6415. }
  6416. (void)heap;
  6417. }
  6418. /* Get the size of the encoded key share extension.
  6419. *
  6420. * list The linked list of key share extensions.
  6421. * msgType The type of the message this extension is being written into.
  6422. * returns the number of bytes of the encoded key share extension.
  6423. */
  6424. static word16 TLSX_KeyShare_GetSize(KeyShareEntry* list, byte msgType)
  6425. {
  6426. word16 len = 0;
  6427. byte isRequest = (msgType == client_hello);
  6428. KeyShareEntry* current;
  6429. /* The named group the server wants to use. */
  6430. if (msgType == hello_retry_request)
  6431. return OPAQUE16_LEN;
  6432. /* List of key exchange groups. */
  6433. if (isRequest)
  6434. len += OPAQUE16_LEN;
  6435. while ((current = list) != NULL) {
  6436. list = current->next;
  6437. if (!isRequest && current->pubKey == NULL)
  6438. continue;
  6439. len += (word16)(KE_GROUP_LEN + OPAQUE16_LEN + current->pubKeyLen);
  6440. }
  6441. return len;
  6442. }
  6443. /* Writes the key share extension into the output buffer.
  6444. * Assumes that the the output buffer is big enough to hold data.
  6445. *
  6446. * list The linked list of key share entries.
  6447. * output The buffer to write into.
  6448. * msgType The type of the message this extension is being written into.
  6449. * returns the number of bytes written into the buffer.
  6450. */
  6451. static word16 TLSX_KeyShare_Write(KeyShareEntry* list, byte* output,
  6452. byte msgType)
  6453. {
  6454. word16 i = 0;
  6455. byte isRequest = (msgType == client_hello);
  6456. KeyShareEntry* current;
  6457. if (msgType == hello_retry_request) {
  6458. c16toa(list->group, output);
  6459. return OPAQUE16_LEN;
  6460. }
  6461. /* ClientHello has a list but ServerHello is only the chosen. */
  6462. if (isRequest)
  6463. i += OPAQUE16_LEN;
  6464. /* Write out all in the list. */
  6465. while ((current = list) != NULL) {
  6466. list = current->next;
  6467. if (!isRequest && current->pubKey == NULL)
  6468. continue;
  6469. c16toa(current->group, &output[i]);
  6470. i += KE_GROUP_LEN;
  6471. c16toa((word16)(current->pubKeyLen), &output[i]);
  6472. i += OPAQUE16_LEN;
  6473. XMEMCPY(&output[i], current->pubKey, current->pubKeyLen);
  6474. i += (word16)current->pubKeyLen;
  6475. }
  6476. /* Write the length of the list if required. */
  6477. if (isRequest)
  6478. c16toa(i - OPAQUE16_LEN, output);
  6479. return i;
  6480. }
  6481. /* Process the DH key share extension on the client side.
  6482. *
  6483. * ssl The SSL/TLS object.
  6484. * keyShareEntry The key share entry object to use to calculate shared secret.
  6485. * returns 0 on success and other values indicate failure.
  6486. */
  6487. static int TLSX_KeyShare_ProcessDh(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6488. {
  6489. int ret = 0;
  6490. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  6491. word32 pSz = 0;
  6492. DhKey* dhKey = (DhKey*)keyShareEntry->key;
  6493. #ifdef HAVE_PUBLIC_FFDHE
  6494. const DhParams* params = NULL;
  6495. switch (keyShareEntry->group) {
  6496. #ifdef HAVE_FFDHE_2048
  6497. case WOLFSSL_FFDHE_2048:
  6498. params = wc_Dh_ffdhe2048_Get();
  6499. break;
  6500. #endif
  6501. #ifdef HAVE_FFDHE_3072
  6502. case WOLFSSL_FFDHE_3072:
  6503. params = wc_Dh_ffdhe3072_Get();
  6504. break;
  6505. #endif
  6506. #ifdef HAVE_FFDHE_4096
  6507. case WOLFSSL_FFDHE_4096:
  6508. params = wc_Dh_ffdhe4096_Get();
  6509. break;
  6510. #endif
  6511. #ifdef HAVE_FFDHE_6144
  6512. case WOLFSSL_FFDHE_6144:
  6513. params = wc_Dh_ffdhe6144_Get();
  6514. break;
  6515. #endif
  6516. #ifdef HAVE_FFDHE_8192
  6517. case WOLFSSL_FFDHE_8192:
  6518. params = wc_Dh_ffdhe8192_Get();
  6519. break;
  6520. #endif
  6521. default:
  6522. break;
  6523. }
  6524. if (params == NULL) {
  6525. WOLFSSL_ERROR_VERBOSE(PEER_KEY_ERROR);
  6526. return PEER_KEY_ERROR;
  6527. }
  6528. pSz = params->p_len;
  6529. #else
  6530. ret = wc_DhGetNamedKeyParamSize(keyShareEntry->group, &pSz, NULL, NULL);
  6531. if (ret != 0 || pSz == 0) {
  6532. WOLFSSL_ERROR_VERBOSE(PEER_KEY_ERROR);
  6533. return PEER_KEY_ERROR;
  6534. }
  6535. #endif
  6536. /* if DhKey is not setup, do it now */
  6537. if (keyShareEntry->key == NULL) {
  6538. keyShareEntry->key = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap,
  6539. DYNAMIC_TYPE_DH);
  6540. if (keyShareEntry->key == NULL)
  6541. return MEMORY_E;
  6542. /* Setup Key */
  6543. ret = wc_InitDhKey_ex((DhKey*)keyShareEntry->key, ssl->heap, ssl->devId);
  6544. if (ret == 0) {
  6545. dhKey = (DhKey*)keyShareEntry->key;
  6546. /* Set key */
  6547. #ifdef HAVE_PUBLIC_FFDHE
  6548. ret = wc_DhSetKey(dhKey, params->p, params->p_len, params->g,
  6549. params->g_len);
  6550. #else
  6551. ret = wc_DhSetNamedKey(dhKey, keyShareEntry->group);
  6552. #endif
  6553. }
  6554. }
  6555. if (ret == 0
  6556. #ifdef WOLFSSL_ASYNC_CRYPT
  6557. && keyShareEntry->lastRet == 0 /* don't enter here if WC_PENDING_E */
  6558. #endif
  6559. ) {
  6560. #ifdef WOLFSSL_DEBUG_TLS
  6561. WOLFSSL_MSG("Peer DH Key");
  6562. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6563. #endif
  6564. ssl->options.dhKeySz = (word16)pSz;
  6565. /* Derive secret from private key and peer's public key. */
  6566. ret = DhAgree(ssl, dhKey,
  6567. (const byte*)keyShareEntry->privKey, keyShareEntry->keyLen, /* our private */
  6568. keyShareEntry->ke, keyShareEntry->keLen, /* peer's public key */
  6569. ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz, /* secret */
  6570. NULL, 0
  6571. );
  6572. #ifdef WOLFSSL_ASYNC_CRYPT
  6573. if (ret == WC_PENDING_E) {
  6574. return ret;
  6575. }
  6576. #endif
  6577. }
  6578. /* RFC 8446 Section 7.4.1:
  6579. * ... left-padded with zeros up to the size of the prime. ...
  6580. */
  6581. if (ret == 0 && (word32)ssl->options.dhKeySz > ssl->arrays->preMasterSz) {
  6582. word32 diff = (word32)ssl->options.dhKeySz - ssl->arrays->preMasterSz;
  6583. XMEMMOVE(ssl->arrays->preMasterSecret + diff,
  6584. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
  6585. XMEMSET(ssl->arrays->preMasterSecret, 0, diff);
  6586. ssl->arrays->preMasterSz = ssl->options.dhKeySz;
  6587. }
  6588. /* done with key share, release resources */
  6589. if (dhKey)
  6590. wc_FreeDhKey(dhKey);
  6591. if (keyShareEntry->key) {
  6592. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_DH);
  6593. keyShareEntry->key = NULL;
  6594. }
  6595. if (keyShareEntry->privKey != NULL) {
  6596. XFREE(keyShareEntry->privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6597. keyShareEntry->privKey = NULL;
  6598. }
  6599. if (keyShareEntry->pubKey != NULL) {
  6600. XFREE(keyShareEntry->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6601. keyShareEntry->pubKey = NULL;
  6602. }
  6603. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6604. keyShareEntry->ke = NULL;
  6605. #else
  6606. (void)ssl;
  6607. (void)keyShareEntry;
  6608. ret = PEER_KEY_ERROR;
  6609. WOLFSSL_ERROR_VERBOSE(ret);
  6610. #endif
  6611. return ret;
  6612. }
  6613. /* Process the X25519 key share extension on the client side.
  6614. *
  6615. * ssl The SSL/TLS object.
  6616. * keyShareEntry The key share entry object to use to calculate shared secret.
  6617. * returns 0 on success and other values indicate failure.
  6618. */
  6619. static int TLSX_KeyShare_ProcessX25519(WOLFSSL* ssl,
  6620. KeyShareEntry* keyShareEntry)
  6621. {
  6622. int ret;
  6623. #ifdef HAVE_CURVE25519
  6624. curve25519_key* key = (curve25519_key*)keyShareEntry->key;
  6625. curve25519_key* peerX25519Key;
  6626. #ifdef HAVE_ECC
  6627. if (ssl->peerEccKey != NULL) {
  6628. wc_ecc_free(ssl->peerEccKey);
  6629. ssl->peerEccKey = NULL;
  6630. ssl->peerEccKeyPresent = 0;
  6631. }
  6632. #endif
  6633. peerX25519Key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
  6634. DYNAMIC_TYPE_TLSX);
  6635. if (peerX25519Key == NULL) {
  6636. WOLFSSL_MSG("PeerEccKey Memory error");
  6637. return MEMORY_ERROR;
  6638. }
  6639. ret = wc_curve25519_init(peerX25519Key);
  6640. if (ret != 0) {
  6641. XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6642. return ret;
  6643. }
  6644. #ifdef WOLFSSL_DEBUG_TLS
  6645. WOLFSSL_MSG("Peer Curve25519 Key");
  6646. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6647. #endif
  6648. if (wc_curve25519_check_public(keyShareEntry->ke, keyShareEntry->keLen,
  6649. EC25519_LITTLE_ENDIAN) != 0) {
  6650. ret = ECC_PEERKEY_ERROR;
  6651. WOLFSSL_ERROR_VERBOSE(ret);
  6652. }
  6653. if (ret == 0) {
  6654. if (wc_curve25519_import_public_ex(keyShareEntry->ke,
  6655. keyShareEntry->keLen, peerX25519Key,
  6656. EC25519_LITTLE_ENDIAN) != 0) {
  6657. ret = ECC_PEERKEY_ERROR;
  6658. WOLFSSL_ERROR_VERBOSE(ret);
  6659. }
  6660. }
  6661. if (ret == 0) {
  6662. ssl->ecdhCurveOID = ECC_X25519_OID;
  6663. ret = wc_curve25519_shared_secret_ex(key, peerX25519Key,
  6664. ssl->arrays->preMasterSecret,
  6665. &ssl->arrays->preMasterSz,
  6666. EC25519_LITTLE_ENDIAN);
  6667. }
  6668. wc_curve25519_free(peerX25519Key);
  6669. XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6670. wc_curve25519_free((curve25519_key*)keyShareEntry->key);
  6671. if (keyShareEntry->key != NULL) {
  6672. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6673. keyShareEntry->key = NULL;
  6674. }
  6675. #else
  6676. (void)ssl;
  6677. (void)keyShareEntry;
  6678. ret = PEER_KEY_ERROR;
  6679. WOLFSSL_ERROR_VERBOSE(ret);
  6680. #endif /* HAVE_CURVE25519 */
  6681. return ret;
  6682. }
  6683. /* Process the X448 key share extension on the client side.
  6684. *
  6685. * ssl The SSL/TLS object.
  6686. * keyShareEntry The key share entry object to use to calculate shared secret.
  6687. * returns 0 on success and other values indicate failure.
  6688. */
  6689. static int TLSX_KeyShare_ProcessX448(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6690. {
  6691. int ret;
  6692. #ifdef HAVE_CURVE448
  6693. curve448_key* key = (curve448_key*)keyShareEntry->key;
  6694. curve448_key* peerX448Key;
  6695. #ifdef HAVE_ECC
  6696. if (ssl->peerEccKey != NULL) {
  6697. wc_ecc_free(ssl->peerEccKey);
  6698. ssl->peerEccKey = NULL;
  6699. ssl->peerEccKeyPresent = 0;
  6700. }
  6701. #endif
  6702. peerX448Key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
  6703. DYNAMIC_TYPE_TLSX);
  6704. if (peerX448Key == NULL) {
  6705. WOLFSSL_MSG("PeerEccKey Memory error");
  6706. return MEMORY_ERROR;
  6707. }
  6708. ret = wc_curve448_init(peerX448Key);
  6709. if (ret != 0) {
  6710. XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6711. return ret;
  6712. }
  6713. #ifdef WOLFSSL_DEBUG_TLS
  6714. WOLFSSL_MSG("Peer Curve448 Key");
  6715. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6716. #endif
  6717. if (wc_curve448_check_public(keyShareEntry->ke, keyShareEntry->keLen,
  6718. EC448_LITTLE_ENDIAN) != 0) {
  6719. ret = ECC_PEERKEY_ERROR;
  6720. WOLFSSL_ERROR_VERBOSE(ret);
  6721. }
  6722. if (ret == 0) {
  6723. if (wc_curve448_import_public_ex(keyShareEntry->ke,
  6724. keyShareEntry->keLen, peerX448Key,
  6725. EC448_LITTLE_ENDIAN) != 0) {
  6726. ret = ECC_PEERKEY_ERROR;
  6727. WOLFSSL_ERROR_VERBOSE(ret);
  6728. }
  6729. }
  6730. if (ret == 0) {
  6731. ssl->ecdhCurveOID = ECC_X448_OID;
  6732. ret = wc_curve448_shared_secret_ex(key, peerX448Key,
  6733. ssl->arrays->preMasterSecret,
  6734. &ssl->arrays->preMasterSz,
  6735. EC448_LITTLE_ENDIAN);
  6736. }
  6737. wc_curve448_free(peerX448Key);
  6738. XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6739. wc_curve448_free((curve448_key*)keyShareEntry->key);
  6740. if (keyShareEntry->key != NULL) {
  6741. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6742. keyShareEntry->key = NULL;
  6743. }
  6744. #else
  6745. (void)ssl;
  6746. (void)keyShareEntry;
  6747. ret = PEER_KEY_ERROR;
  6748. WOLFSSL_ERROR_VERBOSE(ret);
  6749. #endif /* HAVE_CURVE448 */
  6750. return ret;
  6751. }
  6752. /* Process the ECC key share extension on the client side.
  6753. *
  6754. * ssl The SSL/TLS object.
  6755. * keyShareEntry The key share entry object to use to calculate shared secret.
  6756. * returns 0 on success and other values indicate failure.
  6757. */
  6758. static int TLSX_KeyShare_ProcessEcc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6759. {
  6760. int ret = 0;
  6761. #ifdef HAVE_ECC
  6762. int curveId = ECC_CURVE_INVALID;
  6763. ecc_key* eccKey = (ecc_key*)keyShareEntry->key;
  6764. /* find supported curve */
  6765. switch (keyShareEntry->group) {
  6766. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  6767. #ifndef NO_ECC_SECP
  6768. case WOLFSSL_ECC_SECP256R1:
  6769. curveId = ECC_SECP256R1;
  6770. break;
  6771. #endif /* !NO_ECC_SECP */
  6772. #endif
  6773. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  6774. #ifndef NO_ECC_SECP
  6775. case WOLFSSL_ECC_SECP384R1:
  6776. curveId = ECC_SECP384R1;
  6777. break;
  6778. #endif /* !NO_ECC_SECP */
  6779. #endif
  6780. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  6781. #ifndef NO_ECC_SECP
  6782. case WOLFSSL_ECC_SECP521R1:
  6783. curveId = ECC_SECP521R1;
  6784. break;
  6785. #endif /* !NO_ECC_SECP */
  6786. #endif
  6787. #if defined(HAVE_X448) && ECC_MIN_KEY_SZ <= 448
  6788. case WOLFSSL_ECC_X448:
  6789. curveId = ECC_X448;
  6790. break;
  6791. #endif
  6792. default:
  6793. /* unsupported curve */
  6794. WOLFSSL_ERROR_VERBOSE(ECC_PEERKEY_ERROR);
  6795. return ECC_PEERKEY_ERROR;
  6796. }
  6797. #ifdef WOLFSSL_ASYNC_CRYPT
  6798. if (keyShareEntry->lastRet == 0) /* don't enter here if WC_PENDING_E */
  6799. #endif
  6800. {
  6801. #ifdef WOLFSSL_DEBUG_TLS
  6802. WOLFSSL_MSG("Peer ECC Key");
  6803. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6804. #endif
  6805. if (ssl->peerEccKey != NULL) {
  6806. wc_ecc_free(ssl->peerEccKey);
  6807. XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
  6808. ssl->peerEccKeyPresent = 0;
  6809. }
  6810. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER >= 115)
  6811. ret = tsip_Tls13GenSharedSecret(ssl, keyShareEntry);
  6812. if (ret != CRYPTOCB_UNAVAILABLE) {
  6813. return ret;
  6814. }
  6815. ret = 0;
  6816. #endif
  6817. ssl->peerEccKey = (ecc_key*)XMALLOC(sizeof(ecc_key), ssl->heap,
  6818. DYNAMIC_TYPE_ECC);
  6819. if (ssl->peerEccKey == NULL) {
  6820. WOLFSSL_MSG("PeerEccKey Memory error");
  6821. ret = MEMORY_ERROR;
  6822. }
  6823. if (ret == 0) {
  6824. ret = wc_ecc_init_ex(ssl->peerEccKey, ssl->heap, ssl->devId);
  6825. }
  6826. /* Point is validated by import function. */
  6827. if (ret == 0) {
  6828. ret = wc_ecc_import_x963_ex(keyShareEntry->ke, keyShareEntry->keLen,
  6829. ssl->peerEccKey, curveId);
  6830. if (ret != 0) {
  6831. ret = ECC_PEERKEY_ERROR;
  6832. WOLFSSL_ERROR_VERBOSE(ret);
  6833. }
  6834. }
  6835. if (ret == 0) {
  6836. ssl->ecdhCurveOID = ssl->peerEccKey->dp->oidSum;
  6837. ssl->peerEccKeyPresent = 1;
  6838. }
  6839. }
  6840. if (ret == 0 && eccKey == NULL)
  6841. ret = BAD_FUNC_ARG;
  6842. if (ret == 0) {
  6843. ret = EccSharedSecret(ssl, eccKey, ssl->peerEccKey,
  6844. keyShareEntry->ke, &keyShareEntry->keLen,
  6845. ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz,
  6846. ssl->options.side
  6847. );
  6848. #ifdef WOLFSSL_ASYNC_CRYPT
  6849. if (ret == WC_PENDING_E)
  6850. return ret;
  6851. #endif
  6852. }
  6853. /* done with key share, release resources */
  6854. if (ssl->peerEccKey != NULL
  6855. #ifdef HAVE_PK_CALLBACKS
  6856. && ssl->ctx->EccSharedSecretCb == NULL
  6857. #endif
  6858. ) {
  6859. wc_ecc_free(ssl->peerEccKey);
  6860. XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
  6861. ssl->peerEccKey = NULL;
  6862. ssl->peerEccKeyPresent = 0;
  6863. }
  6864. if (keyShareEntry->key) {
  6865. wc_ecc_free((ecc_key*)keyShareEntry->key);
  6866. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_ECC);
  6867. keyShareEntry->key = NULL;
  6868. }
  6869. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6870. keyShareEntry->ke = NULL;
  6871. #else
  6872. (void)ssl;
  6873. (void)keyShareEntry;
  6874. ret = PEER_KEY_ERROR;
  6875. WOLFSSL_ERROR_VERBOSE(ret);
  6876. #endif /* HAVE_ECC */
  6877. return ret;
  6878. }
  6879. #ifdef HAVE_PQC
  6880. #ifdef WOLFSSL_WC_KYBER
  6881. /* Process the Kyber key share extension on the client side.
  6882. *
  6883. * ssl The SSL/TLS object.
  6884. * keyShareEntry The key share entry object to use to calculate shared secret.
  6885. * returns 0 on success and other values indicate failure.
  6886. */
  6887. static int TLSX_KeyShare_ProcessPqc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6888. {
  6889. int ret = 0;
  6890. int type;
  6891. KyberKey kem[1];
  6892. byte* sharedSecret = NULL;
  6893. word32 sharedSecretLen = 0;
  6894. int oqs_group = 0;
  6895. int ecc_group = 0;
  6896. ecc_key eccpubkey;
  6897. word32 outlen = 0;
  6898. word32 privSz = 0;
  6899. word32 ctSz = 0;
  6900. word32 ssSz = 0;
  6901. if (keyShareEntry->ke == NULL) {
  6902. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  6903. return BAD_FUNC_ARG;
  6904. }
  6905. if (ssl->options.side == WOLFSSL_SERVER_END) {
  6906. /* I am the server, the shared secret has already been generated and
  6907. * is in keyShareEntry->ke; copy it to the pre-master secret
  6908. * pre-allocated buffer. */
  6909. if (keyShareEntry->keLen > ENCRYPT_LEN) {
  6910. WOLFSSL_MSG("shared secret is too long.");
  6911. return LENGTH_ERROR;
  6912. }
  6913. XMEMCPY(ssl->arrays->preMasterSecret, keyShareEntry->ke,
  6914. keyShareEntry->keLen);
  6915. ssl->arrays->preMasterSz = keyShareEntry->keLen;
  6916. XFREE(keyShareEntry->ke, sl->heap, DYNAMIC_TYPE_SECRET)
  6917. keyShareEntry->ke = NULL;
  6918. keyShareEntry->keLen = 0;
  6919. return 0;
  6920. }
  6921. /* I am the client, the ciphertext is in keyShareEntry->ke */
  6922. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  6923. ret = kyber_id2type(oqs_group, &type);
  6924. if (ret != 0) {
  6925. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  6926. ret = BAD_FUNC_ARG;
  6927. }
  6928. if (ret == 0) {
  6929. ret = wc_KyberKey_Init(type, kem, ssl->heap, INVALID_DEVID);
  6930. if (ret != 0) {
  6931. WOLFSSL_MSG("Error creating Kyber KEM");
  6932. }
  6933. }
  6934. if (ret == 0) {
  6935. ret = wc_KyberKey_SharedSecretSize(kem, &ssSz);
  6936. }
  6937. if (ret == 0) {
  6938. sharedSecretLen = ssSz;
  6939. switch (ecc_group) {
  6940. case WOLFSSL_ECC_SECP256R1:
  6941. sharedSecretLen += 32;
  6942. outlen = 32;
  6943. break;
  6944. case WOLFSSL_ECC_SECP384R1:
  6945. sharedSecretLen += 48;
  6946. outlen = 48;
  6947. break;
  6948. case WOLFSSL_ECC_SECP521R1:
  6949. sharedSecretLen += 66;
  6950. outlen = 66;
  6951. break;
  6952. default:
  6953. break;
  6954. }
  6955. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  6956. if (ret != 0) {
  6957. WOLFSSL_MSG("Memory allocation error.");
  6958. ret = MEMORY_E;
  6959. }
  6960. }
  6961. if (ret == 0) {
  6962. sharedSecret = (byte*)XMALLOC(sharedSecretLen, ssl->heap,
  6963. DYNAMIC_TYPE_TLSX);
  6964. if (sharedSecret == NULL) {
  6965. WOLFSSL_MSG("Memory allocation error.");
  6966. ret = MEMORY_E;
  6967. }
  6968. }
  6969. if (ret == 0) {
  6970. ret = wc_KyberKey_CipherTextSize(kem, &ctSz);
  6971. }
  6972. if (ret == 0) {
  6973. ret = wc_KyberKey_PrivateKeySize(kem, &privSz);
  6974. }
  6975. if (ret == 0) {
  6976. ret = wc_KyberKey_DecodePrivateKey(kem, keyShareEntry->privKey, privSz);
  6977. }
  6978. if (ret == 0) {
  6979. ret = wc_KyberKey_Decapsulate(kem, sharedSecret + outlen,
  6980. keyShareEntry->ke + keyShareEntry->keLen - ctSz, ctSz);
  6981. if (ret != 0) {
  6982. WOLFSSL_MSG("wc_KyberKey decapsulation failure.");
  6983. ret = BAD_FUNC_ARG;
  6984. }
  6985. }
  6986. if (ecc_group != 0) {
  6987. if (ret == 0) {
  6988. /* Point is validated by import function. */
  6989. ret = wc_ecc_import_x963(keyShareEntry->ke,
  6990. keyShareEntry->keLen - ctSz,
  6991. &eccpubkey);
  6992. if (ret != 0) {
  6993. WOLFSSL_MSG("ECC Public key import error.");
  6994. }
  6995. }
  6996. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  6997. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  6998. !defined(HAVE_SELFTEST)
  6999. if (ret == 0) {
  7000. ret = wc_ecc_set_rng(keyShareEntry->key, ssl->rng);
  7001. if (ret != 0) {
  7002. WOLFSSL_MSG("Failure to set the ECC private key RNG.");
  7003. }
  7004. }
  7005. #endif
  7006. if (ret == 0) {
  7007. PRIVATE_KEY_UNLOCK();
  7008. ret = wc_ecc_shared_secret(keyShareEntry->key, &eccpubkey,
  7009. sharedSecret, &outlen);
  7010. PRIVATE_KEY_LOCK();
  7011. if (outlen != sharedSecretLen - ssSz) {
  7012. WOLFSSL_MSG("ECC shared secret derivation error.");
  7013. ret = BAD_FUNC_ARG;
  7014. }
  7015. }
  7016. }
  7017. if ((ret == 0) && (sharedSecretLen > ENCRYPT_LEN)) {
  7018. WOLFSSL_MSG("shared secret is too long.");
  7019. ret = LENGTH_ERROR;
  7020. }
  7021. if (ret == 0) {
  7022. /* Copy the shared secret to the pre-master secret pre-allocated
  7023. * buffer. */
  7024. XMEMCPY(ssl->arrays->preMasterSecret, sharedSecret, sharedSecretLen);
  7025. ssl->arrays->preMasterSz = (word32) sharedSecretLen;
  7026. }
  7027. if (sharedSecret != NULL) {
  7028. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
  7029. }
  7030. wc_ecc_free(&eccpubkey);
  7031. wc_KyberKey_Free(kem);
  7032. return ret;
  7033. }
  7034. #elif defined(HAVE_LIBOQS)
  7035. /* Process the liboqs key share extension on the client side.
  7036. *
  7037. * ssl The SSL/TLS object.
  7038. * keyShareEntry The key share entry object to use to calculate shared secret.
  7039. * returns 0 on success and other values indicate failure.
  7040. */
  7041. static int TLSX_KeyShare_ProcessPqc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  7042. {
  7043. int ret = 0;
  7044. const char* algName = NULL;
  7045. OQS_KEM* kem = NULL;
  7046. byte* sharedSecret = NULL;
  7047. word32 sharedSecretLen = 0;
  7048. int oqs_group = 0;
  7049. int ecc_group = 0;
  7050. ecc_key eccpubkey;
  7051. word32 outlen = 0;
  7052. if (keyShareEntry->ke == NULL) {
  7053. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  7054. return BAD_FUNC_ARG;
  7055. }
  7056. if (ssl->options.side == WOLFSSL_SERVER_END) {
  7057. /* I am the server, the shared secret has already been generated and
  7058. * is in keyShareEntry->ke; copy it to the pre-master secret
  7059. * pre-allocated buffer. */
  7060. if (keyShareEntry->keLen > ENCRYPT_LEN) {
  7061. WOLFSSL_MSG("shared secret is too long.");
  7062. WOLFSSL_ERROR_VERBOSE(LENGTH_ERROR);
  7063. return LENGTH_ERROR;
  7064. }
  7065. XMEMCPY(ssl->arrays->preMasterSecret, keyShareEntry->ke, keyShareEntry->keLen);
  7066. ssl->arrays->preMasterSz = keyShareEntry->keLen;
  7067. XFREE(keyShareEntry->ke, sl->heap, DYNAMIC_TYPE_SECRET)
  7068. keyShareEntry->ke = NULL;
  7069. keyShareEntry->keLen = 0;
  7070. return 0;
  7071. }
  7072. /* I am the client, the ciphertext is in keyShareEntry->ke */
  7073. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  7074. algName = OQS_ID2name(oqs_group);
  7075. if (algName == NULL) {
  7076. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  7077. WOLFSSL_ERROR_VERBOSE(BAD_FUNC_ARG);
  7078. return BAD_FUNC_ARG;
  7079. }
  7080. kem = OQS_KEM_new(algName);
  7081. if (kem == NULL) {
  7082. WOLFSSL_MSG("Error creating OQS KEM, ensure algorithm support"
  7083. "was enabled in liboqs.");
  7084. return MEMORY_E;
  7085. }
  7086. sharedSecretLen = (word32)kem->length_shared_secret;
  7087. switch (ecc_group) {
  7088. case WOLFSSL_ECC_SECP256R1:
  7089. sharedSecretLen += 32;
  7090. outlen = 32;
  7091. break;
  7092. case WOLFSSL_ECC_SECP384R1:
  7093. sharedSecretLen += 48;
  7094. outlen = 48;
  7095. break;
  7096. case WOLFSSL_ECC_SECP521R1:
  7097. sharedSecretLen += 66;
  7098. outlen = 66;
  7099. break;
  7100. default:
  7101. break;
  7102. }
  7103. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  7104. if (ret != 0) {
  7105. WOLFSSL_MSG("Memory allocation error.");
  7106. return MEMORY_E;
  7107. }
  7108. sharedSecret = (byte*)XMALLOC(sharedSecretLen, ssl->heap,
  7109. DYNAMIC_TYPE_TLSX);
  7110. if (sharedSecret == NULL) {
  7111. WOLFSSL_MSG("Memory allocation error.");
  7112. ret = MEMORY_E;
  7113. }
  7114. if (ret == 0 && OQS_KEM_decaps(kem, sharedSecret + outlen,
  7115. keyShareEntry->ke + keyShareEntry->keLen -
  7116. kem->length_ciphertext,
  7117. keyShareEntry->privKey) != OQS_SUCCESS) {
  7118. WOLFSSL_MSG("Liboqs decapsulation failure.");
  7119. ret = BAD_FUNC_ARG;
  7120. WOLFSSL_ERROR_VERBOSE(ret);
  7121. }
  7122. if (ecc_group != 0) {
  7123. if (ret == 0) {
  7124. /* Point is validated by import function. */
  7125. ret = wc_ecc_import_x963(keyShareEntry->ke,
  7126. keyShareEntry->keLen -
  7127. (word32)kem->length_ciphertext,
  7128. &eccpubkey);
  7129. if (ret != 0) {
  7130. WOLFSSL_ERROR_VERBOSE(ret);
  7131. WOLFSSL_MSG("ECC Public key import error.");
  7132. }
  7133. }
  7134. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  7135. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  7136. !defined(HAVE_SELFTEST)
  7137. if (ret == 0) {
  7138. ret = wc_ecc_set_rng(keyShareEntry->key, ssl->rng);
  7139. if (ret != 0) {
  7140. WOLFSSL_MSG("Failure to set the ECC private key RNG.");
  7141. }
  7142. }
  7143. #endif
  7144. if (ret == 0) {
  7145. PRIVATE_KEY_UNLOCK();
  7146. ret = wc_ecc_shared_secret(keyShareEntry->key, &eccpubkey, sharedSecret, &outlen);
  7147. PRIVATE_KEY_LOCK();
  7148. if (outlen != sharedSecretLen - kem->length_shared_secret) {
  7149. WOLFSSL_MSG("ECC shared secret derivation error.");
  7150. ret = BAD_FUNC_ARG;
  7151. WOLFSSL_ERROR_VERBOSE(ret);
  7152. }
  7153. }
  7154. }
  7155. if (sharedSecretLen > ENCRYPT_LEN) {
  7156. WOLFSSL_MSG("shared secret is too long.");
  7157. ret = LENGTH_ERROR;
  7158. WOLFSSL_ERROR_VERBOSE(ret);
  7159. }
  7160. if (ret == 0) {
  7161. /* Copy the shared secret to the pre-master secret pre-allocated
  7162. * buffer. */
  7163. XMEMCPY(ssl->arrays->preMasterSecret, sharedSecret, sharedSecretLen);
  7164. ssl->arrays->preMasterSz = (word32) sharedSecretLen;
  7165. }
  7166. if (sharedSecret != NULL) {
  7167. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
  7168. }
  7169. wc_ecc_free(&eccpubkey);
  7170. OQS_KEM_free(kem);
  7171. return ret;
  7172. }
  7173. #elif defined(HAVE_PQM4)
  7174. static int TLSX_KeyShare_ProcessPqc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  7175. {
  7176. int ret = 0;
  7177. byte* sharedSecret = NULL;
  7178. word32 sharedSecretLen = 0;
  7179. int oqs_group = 0;
  7180. int ecc_group = 0;
  7181. ecc_key eccpubkey;
  7182. word32 outlen = 0;
  7183. if (keyShareEntry->ke == NULL) {
  7184. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  7185. return BAD_FUNC_ARG;
  7186. }
  7187. if (ssl->options.side == WOLFSSL_SERVER_END) {
  7188. /* I am the server, the shared secret has already been generated and
  7189. * is in keyShareEntry->ke; copy it to the pre-master secret
  7190. * pre-allocated buffer. */
  7191. if (keyShareEntry->keLen > ENCRYPT_LEN) {
  7192. WOLFSSL_MSG("shared secret is too long.");
  7193. WOLFSSL_ERROR_VERBOSE(LENGTH_ERROR);
  7194. return LENGTH_ERROR;
  7195. }
  7196. XMEMCPY(ssl->arrays->preMasterSecret, keyShareEntry->ke, keyShareEntry->keLen);
  7197. ssl->arrays->preMasterSz = keyShareEntry->keLen;
  7198. XFREE(keyShareEntry->ke, sl->heap, DYNAMIC_TYPE_SECRET);
  7199. keyShareEntry->ke = NULL;
  7200. keyShareEntry->keLen = 0;
  7201. return 0;
  7202. }
  7203. /* I am the client, the ciphertext is in keyShareEntry->ke */
  7204. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  7205. sharedSecretLen = (word32)PQM4_SHARED_SECRET_LENGTH;
  7206. switch (ecc_group) {
  7207. case WOLFSSL_ECC_SECP256R1:
  7208. sharedSecretLen += 32;
  7209. outlen = 32;
  7210. break;
  7211. case WOLFSSL_ECC_SECP384R1:
  7212. sharedSecretLen += 48;
  7213. outlen = 48;
  7214. break;
  7215. case WOLFSSL_ECC_SECP521R1:
  7216. sharedSecretLen += 66;
  7217. outlen = 66;
  7218. break;
  7219. default:
  7220. break;
  7221. }
  7222. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  7223. if (ret != 0) {
  7224. WOLFSSL_MSG("Memory allocation error.");
  7225. return MEMORY_E;
  7226. }
  7227. sharedSecret = (byte*)XMALLOC(sharedSecretLen, ssl->heap,
  7228. DYNAMIC_TYPE_TLSX);
  7229. if (sharedSecret == NULL) {
  7230. WOLFSSL_MSG("Memory allocation error.");
  7231. ret = MEMORY_E;
  7232. }
  7233. if (ret == 0 && crypto_kem_dec(sharedSecret + outlen,
  7234. keyShareEntry->ke + keyShareEntry->keLen -
  7235. PQM4_CIPHERTEXT_LENGTH,
  7236. keyShareEntry->privKey) != 0) {
  7237. WOLFSSL_MSG("PQM4 decapsulation failure.");
  7238. ret = BAD_FUNC_ARG;
  7239. } else {
  7240. WOLFSSL_MSG("PQM4 decapsulation SUCCESS!!!!!");
  7241. }
  7242. if (ecc_group != 0) {
  7243. if (ret == 0) {
  7244. /* Point is validated by import function. */
  7245. ret = wc_ecc_import_x963(keyShareEntry->ke,
  7246. keyShareEntry->keLen -
  7247. (word32)PQM4_CIPHERTEXT_LENGTH,
  7248. &eccpubkey);
  7249. if (ret != 0) {
  7250. WOLFSSL_MSG("ECC Public key import error.");
  7251. }
  7252. }
  7253. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  7254. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  7255. !defined(HAVE_SELFTEST)
  7256. if (ret == 0) {
  7257. ret = wc_ecc_set_rng(keyShareEntry->key, ssl->rng);
  7258. if (ret != 0) {
  7259. WOLFSSL_MSG("Failure to set the ECC private key RNG.");
  7260. }
  7261. }
  7262. #endif
  7263. if (ret == 0) {
  7264. PRIVATE_KEY_UNLOCK();
  7265. ret = wc_ecc_shared_secret(keyShareEntry->key, &eccpubkey, sharedSecret, &outlen);
  7266. PRIVATE_KEY_LOCK();
  7267. if (outlen != sharedSecretLen - PQM4_SHARED_SECRET_LENGTH) {
  7268. WOLFSSL_MSG("ECC shared secret derivation error.");
  7269. ret = BAD_FUNC_ARG;
  7270. }
  7271. }
  7272. }
  7273. if (sharedSecretLen > ENCRYPT_LEN) {
  7274. WOLFSSL_MSG("shared secret is too long.\n");
  7275. ret = LENGTH_ERROR;
  7276. }
  7277. if (ret == 0) {
  7278. /* Copy the shared secret to the pre-master secret pre-allocated
  7279. * buffer. */
  7280. XMEMCPY(ssl->arrays->preMasterSecret, sharedSecret, sharedSecretLen);
  7281. ssl->arrays->preMasterSz = (word32) sharedSecretLen;
  7282. }
  7283. if (sharedSecret != NULL) {
  7284. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
  7285. }
  7286. wc_ecc_free(&eccpubkey);
  7287. return ret;
  7288. }
  7289. #endif /* HAVE_PQM4 */
  7290. #endif /* HAVE_PQC */
  7291. /* Process the key share extension on the client side.
  7292. *
  7293. * ssl The SSL/TLS object.
  7294. * keyShareEntry The key share entry object to use to calculate shared secret.
  7295. * returns 0 on success and other values indicate failure.
  7296. */
  7297. static int TLSX_KeyShare_Process(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  7298. {
  7299. int ret;
  7300. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  7301. ssl->session->namedGroup = (byte)keyShareEntry->group;
  7302. #endif
  7303. /* reset the pre master secret size */
  7304. if (ssl->arrays->preMasterSz == 0)
  7305. ssl->arrays->preMasterSz = ENCRYPT_LEN;
  7306. /* Use Key Share Data from server. */
  7307. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(keyShareEntry->group))
  7308. ret = TLSX_KeyShare_ProcessDh(ssl, keyShareEntry);
  7309. else if (keyShareEntry->group == WOLFSSL_ECC_X25519)
  7310. ret = TLSX_KeyShare_ProcessX25519(ssl, keyShareEntry);
  7311. else if (keyShareEntry->group == WOLFSSL_ECC_X448)
  7312. ret = TLSX_KeyShare_ProcessX448(ssl, keyShareEntry);
  7313. #ifdef HAVE_PQC
  7314. else if (WOLFSSL_NAMED_GROUP_IS_PQC(keyShareEntry->group))
  7315. ret = TLSX_KeyShare_ProcessPqc(ssl, keyShareEntry);
  7316. #endif
  7317. else
  7318. ret = TLSX_KeyShare_ProcessEcc(ssl, keyShareEntry);
  7319. #ifdef WOLFSSL_DEBUG_TLS
  7320. if (ret == 0) {
  7321. WOLFSSL_MSG("KE Secret");
  7322. WOLFSSL_BUFFER(ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
  7323. }
  7324. #endif
  7325. #ifdef WOLFSSL_ASYNC_CRYPT
  7326. keyShareEntry->lastRet = ret;
  7327. #endif
  7328. return ret;
  7329. }
  7330. /* Parse an entry of the KeyShare extension.
  7331. *
  7332. * ssl The SSL/TLS object.
  7333. * input The extension data.
  7334. * length The length of the extension data.
  7335. * kse The new key share entry object.
  7336. * returns a positive number to indicate amount of data parsed and a negative
  7337. * number on error.
  7338. */
  7339. static int TLSX_KeyShareEntry_Parse(WOLFSSL* ssl, const byte* input,
  7340. word16 length, KeyShareEntry **kse)
  7341. {
  7342. int ret;
  7343. word16 group;
  7344. word16 keLen;
  7345. int offset = 0;
  7346. byte* ke;
  7347. if (length < OPAQUE16_LEN + OPAQUE16_LEN)
  7348. return BUFFER_ERROR;
  7349. /* Named group */
  7350. ato16(&input[offset], &group);
  7351. offset += OPAQUE16_LEN;
  7352. /* Key exchange data - public key. */
  7353. ato16(&input[offset], &keLen);
  7354. offset += OPAQUE16_LEN;
  7355. if (keLen == 0)
  7356. return INVALID_PARAMETER;
  7357. if (keLen > length - offset)
  7358. return BUFFER_ERROR;
  7359. #ifdef HAVE_PQC
  7360. if (WOLFSSL_NAMED_GROUP_IS_PQC(group) &&
  7361. ssl->options.side == WOLFSSL_SERVER_END) {
  7362. /* For KEMs, the public key is not stored. Casting away const because
  7363. * we know for KEMs, it will be read-only.*/
  7364. ke = (byte *)&input[offset];
  7365. } else
  7366. #endif
  7367. {
  7368. /* Store a copy in the key share object. */
  7369. ke = (byte*)XMALLOC(keLen, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7370. if (ke == NULL)
  7371. return MEMORY_E;
  7372. XMEMCPY(ke, &input[offset], keLen);
  7373. }
  7374. /* Populate a key share object in the extension. */
  7375. ret = TLSX_KeyShare_Use(ssl, group, keLen, ke, kse);
  7376. if (ret != 0) {
  7377. if (ke != &input[offset]) {
  7378. XFREE(ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7379. }
  7380. return ret;
  7381. }
  7382. /* Total length of the parsed data. */
  7383. return offset + keLen;
  7384. }
  7385. /* Searches the groups sent for the specified named group.
  7386. *
  7387. * ssl SSL/TLS object.
  7388. * name Group name to match.
  7389. * returns 1 when the extension has the group name and 0 otherwise.
  7390. */
  7391. static int TLSX_KeyShare_Find(WOLFSSL* ssl, word16 group)
  7392. {
  7393. TLSX* extension;
  7394. KeyShareEntry* list;
  7395. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7396. if (extension == NULL) {
  7397. extension = TLSX_Find(ssl->ctx->extensions, TLSX_KEY_SHARE);
  7398. if (extension == NULL)
  7399. return 0;
  7400. }
  7401. list = (KeyShareEntry*)extension->data;
  7402. while (list != NULL) {
  7403. if (list->group == group)
  7404. return 1;
  7405. list = list->next;
  7406. }
  7407. return 0;
  7408. }
  7409. /* Searches the supported groups extension for the specified named group.
  7410. *
  7411. * ssl The SSL/TLS object.
  7412. * name The group name to match.
  7413. * returns 1 when the extension has the group name and 0 otherwise.
  7414. */
  7415. static int TLSX_SupportedGroups_Find(WOLFSSL* ssl, word16 name)
  7416. {
  7417. #ifdef HAVE_SUPPORTED_CURVES
  7418. TLSX* extension;
  7419. SupportedCurve* curve = NULL;
  7420. if ((extension = TLSX_Find(ssl->extensions,
  7421. TLSX_SUPPORTED_GROUPS)) == NULL) {
  7422. if ((extension = TLSX_Find(ssl->ctx->extensions,
  7423. TLSX_SUPPORTED_GROUPS)) == NULL) {
  7424. return 0;
  7425. }
  7426. }
  7427. for (curve = (SupportedCurve*)extension->data; curve; curve = curve->next) {
  7428. if (curve->name == name)
  7429. return 1;
  7430. }
  7431. #endif
  7432. (void)ssl;
  7433. (void)name;
  7434. return 0;
  7435. }
  7436. /* Parse the KeyShare extension.
  7437. * Different formats in different messages.
  7438. *
  7439. * ssl The SSL/TLS object.
  7440. * input The extension data.
  7441. * length The length of the extension data.
  7442. * msgType The type of the message this extension is being parsed from.
  7443. * returns 0 on success and other values indicate failure.
  7444. */
  7445. static int TLSX_KeyShare_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  7446. byte msgType)
  7447. {
  7448. int ret;
  7449. KeyShareEntry *keyShareEntry = NULL;
  7450. word16 group;
  7451. if (msgType == client_hello) {
  7452. int offset = 0;
  7453. word16 len;
  7454. TLSX* extension;
  7455. /* Add a KeyShare extension if it doesn't exist. */
  7456. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7457. if (extension == NULL) {
  7458. /* Push new KeyShare extension. */
  7459. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7460. if (ret != 0)
  7461. return ret;
  7462. }
  7463. if (length < OPAQUE16_LEN)
  7464. return BUFFER_ERROR;
  7465. /* ClientHello contains zero or more key share entries. */
  7466. ato16(input, &len);
  7467. if (len != length - OPAQUE16_LEN)
  7468. return BUFFER_ERROR;
  7469. offset += OPAQUE16_LEN;
  7470. while (offset < (int)length) {
  7471. ret = TLSX_KeyShareEntry_Parse(ssl, &input[offset],
  7472. length - (word16)offset,
  7473. &keyShareEntry);
  7474. if (ret < 0)
  7475. return ret;
  7476. offset += ret;
  7477. }
  7478. ret = 0;
  7479. }
  7480. else if (msgType == server_hello) {
  7481. int len;
  7482. if (length < OPAQUE16_LEN)
  7483. return BUFFER_ERROR;
  7484. /* The data is the named group the server wants to use. */
  7485. ato16(input, &group);
  7486. /* Check the selected group was supported by ClientHello extensions. */
  7487. if (!TLSX_SupportedGroups_Find(ssl, group)) {
  7488. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7489. return BAD_KEY_SHARE_DATA;
  7490. }
  7491. /* Check if the group was sent. */
  7492. if (!TLSX_KeyShare_Find(ssl, group)) {
  7493. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7494. return BAD_KEY_SHARE_DATA;
  7495. }
  7496. /* ServerHello contains one key share entry. */
  7497. len = TLSX_KeyShareEntry_Parse(ssl, input, length, &keyShareEntry);
  7498. if (len != (int)length)
  7499. return BUFFER_ERROR;
  7500. /* Not in list sent if there isn't a private key. */
  7501. if (keyShareEntry == NULL || (keyShareEntry->key == NULL
  7502. #if !defined(NO_DH) || defined(HAVE_PQC)
  7503. && keyShareEntry->privKey == NULL
  7504. #endif
  7505. )) {
  7506. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7507. return BAD_KEY_SHARE_DATA;
  7508. }
  7509. /* Process the entry to calculate the secret. */
  7510. ret = TLSX_KeyShare_Process(ssl, keyShareEntry);
  7511. if (ret == 0)
  7512. ssl->session->namedGroup = ssl->namedGroup = group;
  7513. }
  7514. else if (msgType == hello_retry_request) {
  7515. if (length != OPAQUE16_LEN)
  7516. return BUFFER_ERROR;
  7517. /* The data is the named group the server wants to use. */
  7518. ato16(input, &group);
  7519. #ifdef WOLFSSL_ASYNC_CRYPT
  7520. /* only perform find and clear TLSX if not returning from async */
  7521. if (ssl->error != WC_PENDING_E)
  7522. #endif
  7523. {
  7524. /* Check the selected group was supported by ClientHello extensions. */
  7525. if (!TLSX_SupportedGroups_Find(ssl, group)) {
  7526. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7527. return BAD_KEY_SHARE_DATA;
  7528. }
  7529. /* Check if the group was sent. */
  7530. if (TLSX_KeyShare_Find(ssl, group)) {
  7531. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7532. return BAD_KEY_SHARE_DATA;
  7533. }
  7534. /* Clear out unusable key shares. */
  7535. ret = TLSX_KeyShare_Empty(ssl);
  7536. if (ret != 0)
  7537. return ret;
  7538. }
  7539. #ifdef HAVE_PQC
  7540. /* For post-quantum groups, do this in TLSX_PopulateExtensions(). */
  7541. if (!WOLFSSL_NAMED_GROUP_IS_PQC(group))
  7542. #endif
  7543. ret = TLSX_KeyShare_Use(ssl, group, 0, NULL, NULL);
  7544. }
  7545. else {
  7546. /* Not a message type that is allowed to have this extension. */
  7547. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  7548. return SANITY_MSG_E;
  7549. }
  7550. return ret;
  7551. }
  7552. /* Create a new key share entry and put it into the list.
  7553. *
  7554. * list The linked list of key share entries.
  7555. * group The named group.
  7556. * heap The memory to allocate with.
  7557. * keyShareEntry The new key share entry object.
  7558. * returns 0 on success and other values indicate failure.
  7559. */
  7560. static int TLSX_KeyShare_New(KeyShareEntry** list, int group, void *heap,
  7561. KeyShareEntry** keyShareEntry)
  7562. {
  7563. KeyShareEntry* kse;
  7564. KeyShareEntry** next;
  7565. kse = (KeyShareEntry*)XMALLOC(sizeof(KeyShareEntry), heap,
  7566. DYNAMIC_TYPE_TLSX);
  7567. if (kse == NULL)
  7568. return MEMORY_E;
  7569. XMEMSET(kse, 0, sizeof(*kse));
  7570. kse->group = (word16)group;
  7571. /* Add it to the back and maintain the links. */
  7572. while (*list != NULL) {
  7573. /* Assign to temporary to work around compiler bug found by customer. */
  7574. next = &((*list)->next);
  7575. list = next;
  7576. }
  7577. *list = kse;
  7578. *keyShareEntry = kse;
  7579. (void)heap;
  7580. return 0;
  7581. }
  7582. #ifdef HAVE_PQC
  7583. #ifdef WOLFSSL_WC_KYBER
  7584. static int server_generate_pqc_ciphertext(WOLFSSL* ssl,
  7585. KeyShareEntry* keyShareEntry, byte* data, word16 len)
  7586. {
  7587. /* I am the server. The data parameter is the client's public key. I need
  7588. * to generate the public information (AKA ciphertext) and shared secret
  7589. * here. Note the "public information" is equivalent to a the public key in
  7590. * key exchange parlance. That's why it is being assigned to pubKey.
  7591. */
  7592. int type;
  7593. KyberKey kem[1];
  7594. byte* sharedSecret = NULL;
  7595. byte* ciphertext = NULL;
  7596. int ret = 0;
  7597. int oqs_group = 0;
  7598. int ecc_group = 0;
  7599. KeyShareEntry *ecc_kse = NULL;
  7600. ecc_key eccpubkey;
  7601. word32 outlen = 0;
  7602. word32 pubSz = 0;
  7603. word32 ctSz = 0;
  7604. word32 ssSz = 0;
  7605. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  7606. ret = kyber_id2type(oqs_group, &type);
  7607. if (ret != 0) {
  7608. WOLFSSL_MSG("Invalid Kyber algorithm specified.");
  7609. ret = BAD_FUNC_ARG;
  7610. }
  7611. if (ret == 0) {
  7612. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  7613. if (ret != 0) {
  7614. WOLFSSL_MSG("Could not do ECC public key initialization.");
  7615. ret = MEMORY_E;
  7616. }
  7617. }
  7618. if (ret == 0) {
  7619. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  7620. DYNAMIC_TYPE_TLSX);
  7621. if (ecc_kse == NULL) {
  7622. WOLFSSL_MSG("ecc_kse memory allocation failure");
  7623. ret = MEMORY_ERROR;
  7624. }
  7625. }
  7626. if (ret == 0) {
  7627. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  7628. }
  7629. if (ret == 0 && ecc_group != 0) {
  7630. ecc_kse->group = ecc_group;
  7631. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  7632. if (ret != 0) {
  7633. /* No message, TLSX_KeyShare_GenEccKey() will do it. */
  7634. return ret;
  7635. }
  7636. ret = 0;
  7637. }
  7638. if (ret == 0) {
  7639. ret = wc_KyberKey_Init(type, kem, ssl->heap, INVALID_DEVID);
  7640. if (ret == 0) {
  7641. WOLFSSL_MSG("Error creating Kyber KEM");
  7642. }
  7643. }
  7644. if (ret == 0) {
  7645. ret = wc_KyberKey_PublicKeySize(kem, &pubSz);
  7646. }
  7647. if (ret == 0) {
  7648. ret = wc_KyberKey_CipherTextSize(kem, &ctSz);
  7649. }
  7650. if (ret == 0) {
  7651. ret = wc_KyberKey_SharedSecretSize(kem, &ssSz);
  7652. }
  7653. if (ret == 0 && len != pubSz + ecc_kse->pubKeyLen) {
  7654. WOLFSSL_MSG("Invalid public key.");
  7655. ret = BAD_FUNC_ARG;
  7656. }
  7657. if (ret == 0) {
  7658. sharedSecret = (byte*)XMALLOC(ecc_kse->keyLen + ssSz, ssl->heap,
  7659. DYNAMIC_TYPE_TLSX);
  7660. ciphertext = (byte*)XMALLOC(ecc_kse->pubKeyLen + ctSz, ssl->heap,
  7661. DYNAMIC_TYPE_TLSX);
  7662. if (sharedSecret == NULL || ciphertext == NULL) {
  7663. WOLFSSL_MSG("Ciphertext/shared secret memory allocation failure.");
  7664. ret = MEMORY_E;
  7665. }
  7666. }
  7667. if (ecc_group != 0) {
  7668. if (ret == 0) {
  7669. /* Point is validated by import function. */
  7670. ret = wc_ecc_import_x963(data, len - pubSz, &eccpubkey);
  7671. if (ret != 0) {
  7672. WOLFSSL_MSG("Bad ECC public key.");
  7673. }
  7674. }
  7675. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  7676. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  7677. !defined(HAVE_SELFTEST)
  7678. if (ret == 0) {
  7679. ret = wc_ecc_set_rng(ecc_kse->key, ssl->rng);
  7680. }
  7681. #endif
  7682. if (ret == 0) {
  7683. outlen = ecc_kse->keyLen;
  7684. PRIVATE_KEY_UNLOCK();
  7685. ret = wc_ecc_shared_secret(ecc_kse->key, &eccpubkey,
  7686. sharedSecret,
  7687. &outlen);
  7688. PRIVATE_KEY_LOCK();
  7689. if (outlen != ecc_kse->keyLen) {
  7690. WOLFSSL_MSG("Data length mismatch.");
  7691. ret = BAD_FUNC_ARG;
  7692. }
  7693. }
  7694. }
  7695. if (ret == 0) {
  7696. ret = wc_KyberKey_DecodePublicKey(kem, data + ecc_kse->pubKeyLen,
  7697. pubSz);
  7698. }
  7699. if (ret == 0) {
  7700. ret = wc_KyberKey_Encapsulate(kem, ciphertext + ecc_kse->pubKeyLen,
  7701. sharedSecret + outlen, ssl->rng);
  7702. if (ret != 0) {
  7703. WOLFSSL_MSG("wc_KyberKey encapsulation failure.");
  7704. }
  7705. }
  7706. if (ret == 0) {
  7707. if (keyShareEntry->ke != NULL) {
  7708. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7709. }
  7710. keyShareEntry->ke = sharedSecret;
  7711. keyShareEntry->keLen = outlen + ssSz;
  7712. sharedSecret = NULL;
  7713. XMEMCPY(ciphertext, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  7714. keyShareEntry->pubKey = ciphertext;
  7715. keyShareEntry->pubKeyLen = (word32)(ecc_kse->pubKeyLen + ctSz);
  7716. ciphertext = NULL;
  7717. }
  7718. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  7719. if (sharedSecret != NULL)
  7720. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_TLSX);
  7721. if (ciphertext != NULL)
  7722. XFREE(ciphertext, ssl->heap, DYNAMIC_TYPE_TLSX);
  7723. wc_ecc_free(&eccpubkey);
  7724. wc_KyberKey_Free(kem);
  7725. return ret;
  7726. }
  7727. #elif defined(HAVE_LIBOQS)
  7728. static int server_generate_pqc_ciphertext(WOLFSSL* ssl,
  7729. KeyShareEntry* keyShareEntry,
  7730. byte* data, word16 len)
  7731. {
  7732. /* I am the server. The data parameter is the client's public key. I need
  7733. * to generate the public information (AKA ciphertext) and shared secret
  7734. * here. Note the "public information" is equivalent to a the public key in
  7735. * key exchange parlance. That's why it is being assigned to pubKey.
  7736. */
  7737. const char* algName = NULL;
  7738. OQS_KEM* kem = NULL;
  7739. byte* sharedSecret = NULL;
  7740. byte* ciphertext = NULL;
  7741. int ret = 0;
  7742. int oqs_group = 0;
  7743. int ecc_group = 0;
  7744. KeyShareEntry *ecc_kse = NULL;
  7745. ecc_key eccpubkey;
  7746. word32 outlen = 0;
  7747. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  7748. algName = OQS_ID2name(oqs_group);
  7749. if (algName == NULL) {
  7750. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  7751. return BAD_FUNC_ARG;
  7752. }
  7753. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  7754. if (ret != 0) {
  7755. WOLFSSL_MSG("Could not do ECC public key initialization.");
  7756. return MEMORY_E;
  7757. }
  7758. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap, DYNAMIC_TYPE_TLSX);
  7759. if (ecc_kse == NULL) {
  7760. WOLFSSL_MSG("ecc_kse memory allocation failure");
  7761. ret = MEMORY_ERROR;
  7762. }
  7763. if (ret == 0) {
  7764. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  7765. }
  7766. if (ret == 0 && ecc_group != 0) {
  7767. ecc_kse->group = ecc_group;
  7768. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  7769. if (ret != 0) {
  7770. /* No message, TLSX_KeyShare_GenEccKey() will do it. */
  7771. return ret;
  7772. }
  7773. ret = 0;
  7774. }
  7775. if (ret == 0) {
  7776. kem = OQS_KEM_new(algName);
  7777. if (kem == NULL) {
  7778. WOLFSSL_MSG("Error creating OQS KEM, ensure algorithm support "
  7779. "was enabled in liboqs.");
  7780. ret = MEMORY_E;
  7781. }
  7782. }
  7783. if (ret == 0 && len != kem->length_public_key + ecc_kse->pubKeyLen) {
  7784. WOLFSSL_MSG("Invalid public key.");
  7785. WOLFSSL_ERROR_VERBOSE(BAD_FUNC_ARG);
  7786. ret = BAD_FUNC_ARG;
  7787. }
  7788. if (ret == 0) {
  7789. sharedSecret = (byte*)XMALLOC(ecc_kse->keyLen +
  7790. kem->length_shared_secret,
  7791. ssl->heap, DYNAMIC_TYPE_TLSX);
  7792. ciphertext = (byte*)XMALLOC(ecc_kse->pubKeyLen + kem->length_ciphertext,
  7793. ssl->heap, DYNAMIC_TYPE_TLSX);
  7794. if (sharedSecret == NULL || ciphertext == NULL) {
  7795. WOLFSSL_MSG("Ciphertext/shared secret memory allocation failure.");
  7796. ret = MEMORY_E;
  7797. }
  7798. }
  7799. if (ecc_group != 0) {
  7800. if (ret == 0) {
  7801. /* Point is validated by import function. */
  7802. ret = wc_ecc_import_x963(data, len - (word32)kem->length_public_key,
  7803. &eccpubkey);
  7804. if (ret != 0) {
  7805. WOLFSSL_MSG("Bad ECC public key.");
  7806. }
  7807. }
  7808. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  7809. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  7810. !defined(HAVE_SELFTEST)
  7811. if (ret == 0) {
  7812. ret = wc_ecc_set_rng(ecc_kse->key, ssl->rng);
  7813. }
  7814. #endif
  7815. if (ret == 0) {
  7816. outlen = ecc_kse->keyLen;
  7817. PRIVATE_KEY_UNLOCK();
  7818. ret = wc_ecc_shared_secret(ecc_kse->key, &eccpubkey,
  7819. sharedSecret,
  7820. &outlen);
  7821. PRIVATE_KEY_LOCK();
  7822. if (outlen != ecc_kse->keyLen) {
  7823. WOLFSSL_MSG("Data length mismatch.");
  7824. ret = BAD_FUNC_ARG;
  7825. }
  7826. }
  7827. }
  7828. if (ret == 0 &&
  7829. OQS_KEM_encaps(kem, ciphertext + ecc_kse->pubKeyLen,
  7830. sharedSecret + outlen,
  7831. data + ecc_kse->pubKeyLen) != OQS_SUCCESS) {
  7832. WOLFSSL_MSG("OQS Encapsulation failure.");
  7833. ret = BAD_FUNC_ARG;
  7834. }
  7835. if (ret == 0) {
  7836. if (keyShareEntry->ke != NULL) {
  7837. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7838. }
  7839. keyShareEntry->ke = sharedSecret;
  7840. keyShareEntry->keLen = outlen + (word32)kem->length_shared_secret;
  7841. sharedSecret = NULL;
  7842. XMEMCPY(ciphertext, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  7843. keyShareEntry->pubKey = ciphertext;
  7844. keyShareEntry->pubKeyLen = (word32)(ecc_kse->pubKeyLen +
  7845. kem->length_ciphertext);
  7846. ciphertext = NULL;
  7847. /* Set namedGroup so wolfSSL_get_curve_name() can function properly on
  7848. * the server side. */
  7849. ssl->namedGroup = keyShareEntry->group;
  7850. }
  7851. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  7852. if (sharedSecret != NULL)
  7853. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_TLSX);
  7854. if (ciphertext != NULL)
  7855. XFREE(ciphertext, ssl->heap, DYNAMIC_TYPE_TLSX);
  7856. wc_ecc_free(&eccpubkey);
  7857. OQS_KEM_free(kem);
  7858. return ret;
  7859. }
  7860. #elif defined(HAVE_PQM4)
  7861. static int server_generate_pqc_ciphertext(WOLFSSL* ssl,
  7862. KeyShareEntry* keyShareEntry,
  7863. byte* data, word16 len)
  7864. {
  7865. /* I am the server. The data parameter is the client's public key. I need
  7866. * to generate the public information (AKA ciphertext) and shared secret
  7867. * here. Note the "public information" is equivalent to a the public key in
  7868. * key exchange parlance. That's why it is being assigned to pubKey.
  7869. */
  7870. byte* sharedSecret = NULL;
  7871. byte* ciphertext = NULL;
  7872. int ret = 0;
  7873. int oqs_group = 0;
  7874. int ecc_group = 0;
  7875. KeyShareEntry *ecc_kse = NULL;
  7876. ecc_key eccpubkey;
  7877. word32 outlen = 0;
  7878. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  7879. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  7880. if (ret != 0) {
  7881. WOLFSSL_MSG("Could not do ECC public key initialization.");
  7882. return MEMORY_E;
  7883. }
  7884. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap, DYNAMIC_TYPE_TLSX);
  7885. if (ecc_kse == NULL) {
  7886. WOLFSSL_MSG("ecc_kse memory allocation failure");
  7887. ret = MEMORY_ERROR;
  7888. }
  7889. if (ret == 0) {
  7890. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  7891. }
  7892. if (ret == 0 && ecc_group != 0) {
  7893. ecc_kse->group = ecc_group;
  7894. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  7895. if (ret != 0) {
  7896. /* No message, TLSX_KeyShare_GenEccKey() will do it. */
  7897. return ret;
  7898. }
  7899. ret = 0;
  7900. }
  7901. if (ret == 0 && len != PQM4_PUBLIC_KEY_LENGTH + ecc_kse->pubKeyLen) {
  7902. WOLFSSL_MSG("Invalid public key.");
  7903. ret = BAD_FUNC_ARG;
  7904. }
  7905. if (ret == 0) {
  7906. sharedSecret = (byte*)XMALLOC(ecc_kse->keyLen + PQM4_SHARED_SECRET_LENGTH,
  7907. ssl->heap, DYNAMIC_TYPE_TLSX);
  7908. ciphertext = (byte*)XMALLOC(ecc_kse->pubKeyLen + PQM4_CIPHERTEXT_LENGTH,
  7909. ssl->heap, DYNAMIC_TYPE_TLSX);
  7910. if (sharedSecret == NULL || ciphertext == NULL) {
  7911. WOLFSSL_MSG("Ciphertext/shared secret memory allocation failure.");
  7912. ret = MEMORY_E;
  7913. }
  7914. }
  7915. if (ecc_group != 0) {
  7916. if (ret == 0) {
  7917. /* Point is validated by import function. */
  7918. ret = wc_ecc_import_x963(data, len - PQM4_PUBLIC_KEY_LENGTH,
  7919. &eccpubkey);
  7920. if (ret != 0) {
  7921. WOLFSSL_MSG("Bad ECC public key.");
  7922. }
  7923. }
  7924. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  7925. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  7926. !defined(HAVE_SELFTEST)
  7927. if (ret == 0) {
  7928. ret = wc_ecc_set_rng(ecc_kse->key, ssl->rng);
  7929. }
  7930. #endif
  7931. if (ret == 0) {
  7932. outlen = ecc_kse->keyLen;
  7933. PRIVATE_KEY_UNLOCK();
  7934. ret = wc_ecc_shared_secret(ecc_kse->key, &eccpubkey,
  7935. sharedSecret,
  7936. &outlen);
  7937. PRIVATE_KEY_LOCK();
  7938. if (outlen != ecc_kse->keyLen) {
  7939. WOLFSSL_MSG("Data length mismatch.");
  7940. ret = BAD_FUNC_ARG;
  7941. }
  7942. }
  7943. }
  7944. if (ret == 0 &&
  7945. crypto_kem_enc(ciphertext + ecc_kse->pubKeyLen,
  7946. sharedSecret + outlen,
  7947. data + ecc_kse->pubKeyLen) != 0) {
  7948. WOLFSSL_MSG("PQM4 Encapsulation failure.");
  7949. ret = BAD_FUNC_ARG;
  7950. }
  7951. if (ret == 0) {
  7952. if (keyShareEntry->ke != NULL) {
  7953. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7954. }
  7955. keyShareEntry->ke = sharedSecret;
  7956. keyShareEntry->keLen = outlen + (word32)PQM4_SHARED_SECRET_LENGTH;
  7957. sharedSecret = NULL;
  7958. XMEMCPY(ciphertext, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  7959. keyShareEntry->pubKey = ciphertext;
  7960. keyShareEntry->pubKeyLen = (word32)(ecc_kse->pubKeyLen +
  7961. PQM4_CIPHERTEXT_LENGTH);
  7962. ciphertext = NULL;
  7963. /* Set namedGroup so wolfSSL_get_curve_name() can function properly on
  7964. * the server side. */
  7965. ssl->namedGroup = keyShareEntry->group;
  7966. }
  7967. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  7968. if (sharedSecret != NULL)
  7969. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_TLSX);
  7970. if (ciphertext != NULL)
  7971. XFREE(ciphertext, ssl->heap, DYNAMIC_TYPE_TLSX);
  7972. wc_ecc_free(&eccpubkey);
  7973. return ret;
  7974. }
  7975. #endif /* HAVE_PQM4 */
  7976. #endif /* HAVE_PQC */
  7977. /* Use the data to create a new key share object in the extensions.
  7978. *
  7979. * ssl The SSL/TLS object.
  7980. * group The named group.
  7981. * len The length of the public key data.
  7982. * data The public key data.
  7983. * kse The new key share entry object.
  7984. * returns 0 on success and other values indicate failure.
  7985. */
  7986. int TLSX_KeyShare_Use(WOLFSSL* ssl, word16 group, word16 len, byte* data,
  7987. KeyShareEntry **kse)
  7988. {
  7989. int ret = 0;
  7990. TLSX* extension;
  7991. KeyShareEntry* keyShareEntry = NULL;
  7992. /* Find the KeyShare extension if it exists. */
  7993. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7994. if (extension == NULL) {
  7995. /* Push new KeyShare extension. */
  7996. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7997. if (ret != 0)
  7998. return ret;
  7999. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  8000. if (extension == NULL)
  8001. return MEMORY_E;
  8002. }
  8003. extension->resp = 0;
  8004. /* Try to find the key share entry with this group. */
  8005. keyShareEntry = (KeyShareEntry*)extension->data;
  8006. while (keyShareEntry != NULL) {
  8007. if (keyShareEntry->group == group)
  8008. break;
  8009. keyShareEntry = keyShareEntry->next;
  8010. }
  8011. /* Create a new key share entry if not found. */
  8012. if (keyShareEntry == NULL) {
  8013. ret = TLSX_KeyShare_New((KeyShareEntry**)&extension->data, group,
  8014. ssl->heap, &keyShareEntry);
  8015. if (ret != 0)
  8016. return ret;
  8017. }
  8018. #ifdef HAVE_PQC
  8019. if (WOLFSSL_NAMED_GROUP_IS_PQC(group) &&
  8020. ssl->options.side == WOLFSSL_SERVER_END) {
  8021. ret = server_generate_pqc_ciphertext(ssl, keyShareEntry, data,
  8022. len);
  8023. if (ret != 0)
  8024. return ret;
  8025. }
  8026. else
  8027. #endif
  8028. if (data != NULL) {
  8029. if (keyShareEntry->ke != NULL) {
  8030. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  8031. }
  8032. keyShareEntry->ke = data;
  8033. keyShareEntry->keLen = len;
  8034. }
  8035. else {
  8036. /* Generate a key pair. */
  8037. ret = TLSX_KeyShare_GenKey(ssl, keyShareEntry);
  8038. if (ret != 0)
  8039. return ret;
  8040. }
  8041. if (kse != NULL)
  8042. *kse = keyShareEntry;
  8043. return 0;
  8044. }
  8045. /* Set an empty Key Share extension.
  8046. *
  8047. * ssl The SSL/TLS object.
  8048. * returns 0 on success and other values indicate failure.
  8049. */
  8050. int TLSX_KeyShare_Empty(WOLFSSL* ssl)
  8051. {
  8052. int ret = 0;
  8053. TLSX* extension;
  8054. /* Find the KeyShare extension if it exists. */
  8055. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  8056. if (extension == NULL) {
  8057. /* Push new KeyShare extension. */
  8058. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  8059. }
  8060. else if (extension->data != NULL) {
  8061. TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
  8062. extension->data = NULL;
  8063. }
  8064. return ret;
  8065. }
  8066. /* Returns whether this group is supported.
  8067. *
  8068. * namedGroup The named group to check.
  8069. * returns 1 when supported or 0 otherwise.
  8070. */
  8071. static int TLSX_KeyShare_IsSupported(int namedGroup)
  8072. {
  8073. switch (namedGroup) {
  8074. #ifdef HAVE_FFDHE_2048
  8075. case WOLFSSL_FFDHE_2048:
  8076. break;
  8077. #endif
  8078. #ifdef HAVE_FFDHE_3072
  8079. case WOLFSSL_FFDHE_3072:
  8080. break;
  8081. #endif
  8082. #ifdef HAVE_FFDHE_4096
  8083. case WOLFSSL_FFDHE_4096:
  8084. break;
  8085. #endif
  8086. #ifdef HAVE_FFDHE_6144
  8087. case WOLFSSL_FFDHE_6144:
  8088. break;
  8089. #endif
  8090. #ifdef HAVE_FFDHE_8192
  8091. case WOLFSSL_FFDHE_8192:
  8092. break;
  8093. #endif
  8094. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  8095. #ifdef HAVE_ECC_KOBLITZ
  8096. case WOLFSSL_ECC_SECP256K1:
  8097. break;
  8098. #endif
  8099. #ifndef NO_ECC_SECP
  8100. case WOLFSSL_ECC_SECP256R1:
  8101. break;
  8102. #endif /* !NO_ECC_SECP */
  8103. #ifdef HAVE_ECC_BRAINPOOL
  8104. case WOLFSSL_ECC_BRAINPOOLP256R1:
  8105. break;
  8106. #endif
  8107. #endif
  8108. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  8109. case WOLFSSL_ECC_X25519:
  8110. break;
  8111. #endif
  8112. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  8113. case WOLFSSL_ECC_X448:
  8114. break;
  8115. #endif
  8116. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  8117. #ifndef NO_ECC_SECP
  8118. case WOLFSSL_ECC_SECP384R1:
  8119. break;
  8120. #endif /* !NO_ECC_SECP */
  8121. #ifdef HAVE_ECC_BRAINPOOL
  8122. case WOLFSSL_ECC_BRAINPOOLP384R1:
  8123. break;
  8124. #endif
  8125. #endif
  8126. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  8127. #ifndef NO_ECC_SECP
  8128. case WOLFSSL_ECC_SECP521R1:
  8129. break;
  8130. #endif /* !NO_ECC_SECP */
  8131. #endif
  8132. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  8133. #ifdef HAVE_ECC_KOBLITZ
  8134. case WOLFSSL_ECC_SECP160K1:
  8135. break;
  8136. #endif
  8137. #ifndef NO_ECC_SECP
  8138. case WOLFSSL_ECC_SECP160R1:
  8139. break;
  8140. #endif
  8141. #ifdef HAVE_ECC_SECPR2
  8142. case WOLFSSL_ECC_SECP160R2:
  8143. break;
  8144. #endif
  8145. #endif
  8146. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  8147. #ifdef HAVE_ECC_KOBLITZ
  8148. case WOLFSSL_ECC_SECP192K1:
  8149. break;
  8150. #endif
  8151. #ifndef NO_ECC_SECP
  8152. case WOLFSSL_ECC_SECP192R1:
  8153. break;
  8154. #endif
  8155. #endif
  8156. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  8157. #ifdef HAVE_ECC_KOBLITZ
  8158. case WOLFSSL_ECC_SECP224K1:
  8159. break;
  8160. #endif
  8161. #ifndef NO_ECC_SECP
  8162. case WOLFSSL_ECC_SECP224R1:
  8163. break;
  8164. #endif
  8165. #endif
  8166. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  8167. #ifdef HAVE_ECC_BRAINPOOL
  8168. case WOLFSSL_ECC_BRAINPOOLP512R1:
  8169. break;
  8170. #endif
  8171. #endif
  8172. #ifdef HAVE_PQC
  8173. #ifdef WOLFSSL_WC_KYBER
  8174. #ifdef WOLFSSL_KYBER512
  8175. case WOLFSSL_KYBER_LEVEL1:
  8176. #endif
  8177. #ifdef WOLFSSL_KYBER768
  8178. case WOLFSSL_KYBER_LEVEL3:
  8179. #endif
  8180. #ifdef WOLFSSL_KYBER1024
  8181. case WOLFSSL_KYBER_LEVEL5:
  8182. #endif
  8183. break;
  8184. #elif defined(HAVE_LIBOQS)
  8185. case WOLFSSL_KYBER_LEVEL1:
  8186. case WOLFSSL_KYBER_LEVEL3:
  8187. case WOLFSSL_KYBER_LEVEL5:
  8188. case WOLFSSL_NTRU_HPS_LEVEL1:
  8189. case WOLFSSL_NTRU_HPS_LEVEL3:
  8190. case WOLFSSL_NTRU_HPS_LEVEL5:
  8191. case WOLFSSL_NTRU_HRSS_LEVEL3:
  8192. case WOLFSSL_SABER_LEVEL1:
  8193. case WOLFSSL_SABER_LEVEL3:
  8194. case WOLFSSL_SABER_LEVEL5:
  8195. case WOLFSSL_KYBER_90S_LEVEL1:
  8196. case WOLFSSL_KYBER_90S_LEVEL3:
  8197. case WOLFSSL_KYBER_90S_LEVEL5:
  8198. case WOLFSSL_P256_NTRU_HPS_LEVEL1:
  8199. case WOLFSSL_P384_NTRU_HPS_LEVEL3:
  8200. case WOLFSSL_P521_NTRU_HPS_LEVEL5:
  8201. case WOLFSSL_P384_NTRU_HRSS_LEVEL3:
  8202. case WOLFSSL_P256_SABER_LEVEL1:
  8203. case WOLFSSL_P384_SABER_LEVEL3:
  8204. case WOLFSSL_P521_SABER_LEVEL5:
  8205. case WOLFSSL_P256_KYBER_LEVEL1:
  8206. case WOLFSSL_P384_KYBER_LEVEL3:
  8207. case WOLFSSL_P521_KYBER_LEVEL5:
  8208. case WOLFSSL_P256_KYBER_90S_LEVEL1:
  8209. case WOLFSSL_P384_KYBER_90S_LEVEL3:
  8210. case WOLFSSL_P521_KYBER_90S_LEVEL5:
  8211. findEccPqc(NULL, &namedGroup, namedGroup);
  8212. if (! OQS_KEM_alg_is_enabled(OQS_ID2name(namedGroup))) {
  8213. return 0;
  8214. }
  8215. break;
  8216. #elif defined(HAVE_PQM4)
  8217. case WOLFSSL_KYBER_LEVEL1:
  8218. break;
  8219. #endif
  8220. #endif /* HAVE_PQC */
  8221. default:
  8222. return 0;
  8223. }
  8224. return 1;
  8225. }
  8226. /* Examines the application specified group ranking and returns the rank of the
  8227. * group.
  8228. * If no group ranking set then all groups are rank 0 (highest).
  8229. *
  8230. * ssl The SSL/TLS object.
  8231. * group The group to check ranking for.
  8232. * returns ranking from 0 to MAX_GROUP_COUNT-1 or -1 when group not in list.
  8233. */
  8234. static int TLSX_KeyShare_GroupRank(WOLFSSL* ssl, int group)
  8235. {
  8236. byte i;
  8237. if (ssl->numGroups == 0) {
  8238. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  8239. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  8240. #ifndef NO_ECC_SECP
  8241. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP256R1;
  8242. #endif
  8243. #endif
  8244. #endif
  8245. #ifndef HAVE_FIPS
  8246. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  8247. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_X25519;
  8248. #endif
  8249. #endif
  8250. #ifndef HAVE_FIPS
  8251. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  8252. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_X448;
  8253. #endif
  8254. #endif
  8255. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  8256. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  8257. #ifndef NO_ECC_SECP
  8258. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP384R1;
  8259. #endif
  8260. #endif
  8261. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  8262. #ifndef NO_ECC_SECP
  8263. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP521R1;
  8264. #endif
  8265. #endif
  8266. #endif
  8267. /* Add FFDHE supported groups. */
  8268. #ifdef HAVE_FFDHE_2048
  8269. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_2048;
  8270. #endif
  8271. #ifdef HAVE_FFDHE_3072
  8272. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_3072;
  8273. #endif
  8274. #ifdef HAVE_FFDHE_4096
  8275. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_4096;
  8276. #endif
  8277. #ifdef HAVE_FFDHE_6144
  8278. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_6144;
  8279. #endif
  8280. #ifdef HAVE_FFDHE_8192
  8281. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_8192;
  8282. #endif
  8283. #ifdef HAVE_PQC
  8284. /* For the liboqs groups we need to do a runtime check because
  8285. * liboqs could be compiled to make an algorithm unavailable.
  8286. */
  8287. #ifdef WOLFSSL_WC_KYBER
  8288. #ifdef WOLFSSL_KYBER512
  8289. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL1))
  8290. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL1;
  8291. #endif
  8292. #ifdef WOLFSSL_KYBER768
  8293. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL3))
  8294. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL3;
  8295. #endif
  8296. #ifdef WOLFSSL_KYBER1024
  8297. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL5))
  8298. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL5;
  8299. #endif
  8300. #elif defined(HAVE_LIBOQS)
  8301. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL1))
  8302. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL1;
  8303. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL3))
  8304. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL3;
  8305. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL5))
  8306. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL5;
  8307. if (TLSX_KeyShare_IsSupported(WOLFSSL_NTRU_HPS_LEVEL1))
  8308. ssl->group[ssl->numGroups++] = WOLFSSL_NTRU_HPS_LEVEL1;
  8309. if (TLSX_KeyShare_IsSupported(WOLFSSL_NTRU_HPS_LEVEL3))
  8310. ssl->group[ssl->numGroups++] = WOLFSSL_NTRU_HPS_LEVEL3;
  8311. if (TLSX_KeyShare_IsSupported(WOLFSSL_NTRU_HPS_LEVEL5))
  8312. ssl->group[ssl->numGroups++] = WOLFSSL_NTRU_HPS_LEVEL5;
  8313. if (TLSX_KeyShare_IsSupported(WOLFSSL_NTRU_HRSS_LEVEL3))
  8314. ssl->group[ssl->numGroups++] = WOLFSSL_NTRU_HRSS_LEVEL3;
  8315. if (TLSX_KeyShare_IsSupported(WOLFSSL_SABER_LEVEL1))
  8316. ssl->group[ssl->numGroups++] = WOLFSSL_SABER_LEVEL1;
  8317. if (TLSX_KeyShare_IsSupported(WOLFSSL_SABER_LEVEL3))
  8318. ssl->group[ssl->numGroups++] = WOLFSSL_SABER_LEVEL3;
  8319. if (TLSX_KeyShare_IsSupported(WOLFSSL_SABER_LEVEL5))
  8320. ssl->group[ssl->numGroups++] = WOLFSSL_SABER_LEVEL5;
  8321. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_90S_LEVEL1))
  8322. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_90S_LEVEL1;
  8323. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_90S_LEVEL3))
  8324. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_90S_LEVEL3;
  8325. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_90S_LEVEL5))
  8326. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_90S_LEVEL5;
  8327. if (TLSX_KeyShare_IsSupported(WOLFSSL_P256_NTRU_HPS_LEVEL1))
  8328. ssl->group[ssl->numGroups++] = WOLFSSL_P256_NTRU_HPS_LEVEL1;
  8329. if (TLSX_KeyShare_IsSupported(WOLFSSL_P384_NTRU_HPS_LEVEL3))
  8330. ssl->group[ssl->numGroups++] = WOLFSSL_P384_NTRU_HPS_LEVEL3;
  8331. if (TLSX_KeyShare_IsSupported(WOLFSSL_P521_NTRU_HPS_LEVEL5))
  8332. ssl->group[ssl->numGroups++] = WOLFSSL_P521_NTRU_HPS_LEVEL5;
  8333. if (TLSX_KeyShare_IsSupported(WOLFSSL_P384_NTRU_HRSS_LEVEL3))
  8334. ssl->group[ssl->numGroups++] = WOLFSSL_P384_NTRU_HRSS_LEVEL3;
  8335. if (TLSX_KeyShare_IsSupported(WOLFSSL_P256_SABER_LEVEL1))
  8336. ssl->group[ssl->numGroups++] = WOLFSSL_P256_SABER_LEVEL1;
  8337. if (TLSX_KeyShare_IsSupported(WOLFSSL_P384_SABER_LEVEL3))
  8338. ssl->group[ssl->numGroups++] = WOLFSSL_P384_SABER_LEVEL3;
  8339. if (TLSX_KeyShare_IsSupported(WOLFSSL_P521_SABER_LEVEL5))
  8340. ssl->group[ssl->numGroups++] = WOLFSSL_P521_SABER_LEVEL5;
  8341. if (TLSX_KeyShare_IsSupported(WOLFSSL_P256_KYBER_LEVEL1))
  8342. ssl->group[ssl->numGroups++] = WOLFSSL_P256_KYBER_LEVEL1;
  8343. if (TLSX_KeyShare_IsSupported(WOLFSSL_P384_KYBER_LEVEL3))
  8344. ssl->group[ssl->numGroups++] = WOLFSSL_P384_KYBER_LEVEL3;
  8345. if (TLSX_KeyShare_IsSupported(WOLFSSL_P521_KYBER_LEVEL5))
  8346. ssl->group[ssl->numGroups++] = WOLFSSL_P521_KYBER_LEVEL5;
  8347. if (TLSX_KeyShare_IsSupported(WOLFSSL_P256_KYBER_90S_LEVEL1))
  8348. ssl->group[ssl->numGroups++] = WOLFSSL_P256_KYBER_90S_LEVEL1;
  8349. if (TLSX_KeyShare_IsSupported(WOLFSSL_P384_KYBER_90S_LEVEL3))
  8350. ssl->group[ssl->numGroups++] = WOLFSSL_P384_KYBER_90S_LEVEL3;
  8351. if (TLSX_KeyShare_IsSupported(WOLFSSL_P521_KYBER_90S_LEVEL5))
  8352. ssl->group[ssl->numGroups++] = WOLFSSL_P521_KYBER_90S_LEVEL5;
  8353. #elif defined(HAVE_PQM4)
  8354. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL1))
  8355. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL1;
  8356. #endif /* HAVE_LIBOQS */
  8357. #endif /* HAVE_PQC */
  8358. }
  8359. for (i = 0; i < ssl->numGroups; i++)
  8360. if (ssl->group[i] == (word16)group)
  8361. return i;
  8362. return -1;
  8363. }
  8364. /* Set a key share that is supported by the client into extensions.
  8365. *
  8366. * ssl The SSL/TLS object.
  8367. * returns BAD_KEY_SHARE_DATA if no supported group has a key share,
  8368. * 0 if a supported group has a key share and other values indicate an error.
  8369. */
  8370. static int TLSX_KeyShare_SetSupported(WOLFSSL* ssl)
  8371. {
  8372. int ret;
  8373. #ifdef HAVE_SUPPORTED_CURVES
  8374. TLSX* extension;
  8375. SupportedCurve* curve = NULL;
  8376. SupportedCurve* preferredCurve = NULL;
  8377. int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
  8378. int rank;
  8379. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  8380. if (extension != NULL)
  8381. curve = (SupportedCurve*)extension->data;
  8382. /* Use server's preference order. */
  8383. for (; curve != NULL; curve = curve->next) {
  8384. if (!TLSX_KeyShare_IsSupported(curve->name))
  8385. continue;
  8386. if (wolfSSL_curve_is_disabled(ssl, curve->name))
  8387. continue;
  8388. rank = TLSX_KeyShare_GroupRank(ssl, curve->name);
  8389. if (rank == -1)
  8390. continue;
  8391. if (rank < preferredRank) {
  8392. preferredCurve = curve;
  8393. preferredRank = rank;
  8394. }
  8395. }
  8396. curve = preferredCurve;
  8397. if (curve == NULL) {
  8398. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  8399. return BAD_KEY_SHARE_DATA;
  8400. }
  8401. /* Delete the old key share data list. */
  8402. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  8403. if (extension != NULL) {
  8404. KeyShareEntry* kse = (KeyShareEntry*)extension->data;
  8405. #ifdef WOLFSSL_ASYNC_CRYPT
  8406. /* for async don't free, call `TLSX_KeyShare_Use` again */
  8407. if (kse && kse->lastRet != WC_PENDING_E)
  8408. #endif
  8409. {
  8410. TLSX_KeyShare_FreeAll(kse, ssl->heap);
  8411. extension->data = NULL;
  8412. }
  8413. }
  8414. /* Add in the chosen group. */
  8415. ret = TLSX_KeyShare_Use(ssl, curve->name, 0, NULL, NULL);
  8416. if (ret != 0 && ret != WC_PENDING_E)
  8417. return ret;
  8418. /* Set extension to be in response. */
  8419. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  8420. extension->resp = 1;
  8421. #else
  8422. (void)ssl;
  8423. WOLFSSL_ERROR_VERBOSE(NOT_COMPILED_IN);
  8424. ret = NOT_COMPILED_IN;
  8425. #endif
  8426. return ret;
  8427. }
  8428. /* Ensure there is a key pair that can be used for key exchange.
  8429. *
  8430. * ssl The SSL/TLS object.
  8431. * doHelloRetry If set to non-zero will do hello_retry
  8432. * returns 0 on success and other values indicate failure.
  8433. */
  8434. int TLSX_KeyShare_Establish(WOLFSSL *ssl, int* doHelloRetry)
  8435. {
  8436. int ret;
  8437. TLSX* extension;
  8438. KeyShareEntry* clientKSE = NULL;
  8439. KeyShareEntry* serverKSE;
  8440. KeyShareEntry* list = NULL;
  8441. KeyShareEntry* preferredKSE = NULL;
  8442. int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
  8443. int rank;
  8444. /* Find the KeyShare extension if it exists. */
  8445. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  8446. if (extension != NULL)
  8447. list = (KeyShareEntry*)extension->data;
  8448. if (extension && extension->resp == 1) {
  8449. ret = 0;
  8450. #ifdef WOLFSSL_ASYNC_CRYPT
  8451. /* in async case make sure key generation is finalized */
  8452. serverKSE = (KeyShareEntry*)extension->data;
  8453. if (serverKSE->lastRet == WC_PENDING_E) {
  8454. if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE)
  8455. *doHelloRetry = 1;
  8456. ret = TLSX_KeyShare_GenKey(ssl, serverKSE);
  8457. }
  8458. #endif
  8459. return ret;
  8460. }
  8461. /* Use server's preference order. */
  8462. for (clientKSE = list; clientKSE != NULL; clientKSE = clientKSE->next) {
  8463. if (clientKSE->ke == NULL)
  8464. continue;
  8465. /* Check consistency now - extensions in any order. */
  8466. if (!TLSX_SupportedGroups_Find(ssl, clientKSE->group))
  8467. continue;
  8468. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(clientKSE->group)) {
  8469. /* Check max value supported. */
  8470. if (clientKSE->group > WOLFSSL_ECC_MAX) {
  8471. #ifdef HAVE_PQC
  8472. if (!WOLFSSL_NAMED_GROUP_IS_PQC(clientKSE->group))
  8473. #endif
  8474. continue;
  8475. }
  8476. if (wolfSSL_curve_is_disabled(ssl, clientKSE->group))
  8477. continue;
  8478. }
  8479. if (!TLSX_KeyShare_IsSupported(clientKSE->group))
  8480. continue;
  8481. rank = TLSX_KeyShare_GroupRank(ssl, clientKSE->group);
  8482. if (rank == -1)
  8483. continue;
  8484. if (rank < preferredRank) {
  8485. preferredKSE = clientKSE;
  8486. preferredRank = rank;
  8487. }
  8488. }
  8489. clientKSE = preferredKSE;
  8490. /* No supported group found - send HelloRetryRequest. */
  8491. if (clientKSE == NULL) {
  8492. /* Set KEY_SHARE_ERROR to indicate HelloRetryRequest required. */
  8493. *doHelloRetry = 1;
  8494. return TLSX_KeyShare_SetSupported(ssl);
  8495. }
  8496. list = NULL;
  8497. /* Generate a new key pair except in the case of OQS KEM because we
  8498. * are going to encapsulate and that does not require us to generate a
  8499. * key pair.
  8500. */
  8501. ret = TLSX_KeyShare_New(&list, clientKSE->group, ssl->heap, &serverKSE);
  8502. if (ret != 0)
  8503. return ret;
  8504. if (clientKSE->key == NULL) {
  8505. #ifdef HAVE_PQC
  8506. if (WOLFSSL_NAMED_GROUP_IS_PQC(clientKSE->group)) {
  8507. /* Going to need the public key (AKA ciphertext). */
  8508. serverKSE->pubKey = clientKSE->pubKey;
  8509. clientKSE->pubKey = NULL;
  8510. serverKSE->pubKeyLen = clientKSE->pubKeyLen;
  8511. clientKSE->pubKeyLen = 0;
  8512. }
  8513. else
  8514. #endif
  8515. {
  8516. ret = TLSX_KeyShare_GenKey(ssl, serverKSE);
  8517. }
  8518. /* for async do setup of serverKSE below, but return WC_PENDING_E */
  8519. if (ret != 0
  8520. #ifdef WOLFSSL_ASYNC_CRYPT
  8521. && ret != WC_PENDING_E
  8522. #endif
  8523. ) {
  8524. return ret;
  8525. }
  8526. }
  8527. else {
  8528. /* transfer buffers to serverKSE */
  8529. serverKSE->key = clientKSE->key;
  8530. clientKSE->key = NULL;
  8531. serverKSE->keyLen = clientKSE->keyLen;
  8532. serverKSE->pubKey = clientKSE->pubKey;
  8533. clientKSE->pubKey = NULL;
  8534. serverKSE->pubKeyLen = clientKSE->pubKeyLen;
  8535. #ifndef NO_DH
  8536. serverKSE->privKey = clientKSE->privKey;
  8537. clientKSE->privKey = NULL;
  8538. #endif
  8539. }
  8540. serverKSE->ke = clientKSE->ke;
  8541. serverKSE->keLen = clientKSE->keLen;
  8542. clientKSE->ke = NULL;
  8543. clientKSE->keLen = 0;
  8544. TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
  8545. extension->data = (void *)serverKSE;
  8546. extension->resp = 1;
  8547. return ret;
  8548. }
  8549. /* Derive the shared secret of the key exchange.
  8550. *
  8551. * ssl The SSL/TLS object.
  8552. * returns 0 on success and other values indicate failure.
  8553. */
  8554. int TLSX_KeyShare_DeriveSecret(WOLFSSL *ssl)
  8555. {
  8556. int ret;
  8557. TLSX* extension;
  8558. KeyShareEntry* list = NULL;
  8559. #ifdef WOLFSSL_ASYNC_CRYPT
  8560. ret = wolfSSL_AsyncPop(ssl, NULL);
  8561. /* Check for error */
  8562. if (ret != WC_NOT_PENDING_E && ret < 0) {
  8563. return ret;
  8564. }
  8565. #endif
  8566. /* Find the KeyShare extension if it exists. */
  8567. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  8568. if (extension != NULL)
  8569. list = (KeyShareEntry*)extension->data;
  8570. if (list == NULL)
  8571. return KEY_SHARE_ERROR;
  8572. /* Calculate secret. */
  8573. ret = TLSX_KeyShare_Process(ssl, list);
  8574. return ret;
  8575. }
  8576. #define KS_FREE_ALL TLSX_KeyShare_FreeAll
  8577. #define KS_GET_SIZE TLSX_KeyShare_GetSize
  8578. #define KS_WRITE TLSX_KeyShare_Write
  8579. #define KS_PARSE TLSX_KeyShare_Parse
  8580. #else
  8581. #define KS_FREE_ALL(a, b)
  8582. #define KS_GET_SIZE(a, b) 0
  8583. #define KS_WRITE(a, b, c) 0
  8584. #define KS_PARSE(a, b, c, d) 0
  8585. #endif /* WOLFSSL_TLS13 */
  8586. /******************************************************************************/
  8587. /* Pre-Shared Key */
  8588. /******************************************************************************/
  8589. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  8590. /* Free the pre-shared key dynamic data.
  8591. *
  8592. * list The linked list of key share entry objects.
  8593. * heap The heap used for allocation.
  8594. */
  8595. static void TLSX_PreSharedKey_FreeAll(PreSharedKey* list, void* heap)
  8596. {
  8597. PreSharedKey* current;
  8598. while ((current = list) != NULL) {
  8599. list = current->next;
  8600. XFREE(current->identity, heap, DYNAMIC_TYPE_TLSX);
  8601. XFREE(current, heap, DYNAMIC_TYPE_TLSX);
  8602. }
  8603. (void)heap;
  8604. }
  8605. /* Get the size of the encoded pre shared key extension.
  8606. *
  8607. * list The linked list of pre-shared key extensions.
  8608. * msgType The type of the message this extension is being written into.
  8609. * returns the number of bytes of the encoded pre-shared key extension or
  8610. * SANITY_MSG_E to indicate invalid message type.
  8611. */
  8612. static int TLSX_PreSharedKey_GetSize(PreSharedKey* list, byte msgType,
  8613. word16* pSz)
  8614. {
  8615. if (msgType == client_hello) {
  8616. /* Length of identities + Length of binders. */
  8617. word16 len = OPAQUE16_LEN + OPAQUE16_LEN;
  8618. while (list != NULL) {
  8619. /* Each entry has: identity, ticket age and binder. */
  8620. len += OPAQUE16_LEN + list->identityLen + OPAQUE32_LEN +
  8621. OPAQUE8_LEN + (word16)list->binderLen;
  8622. list = list->next;
  8623. }
  8624. *pSz += len;
  8625. return 0;
  8626. }
  8627. if (msgType == server_hello) {
  8628. *pSz += OPAQUE16_LEN;
  8629. return 0;
  8630. }
  8631. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8632. return SANITY_MSG_E;
  8633. }
  8634. /* The number of bytes to be written for the binders.
  8635. *
  8636. * list The linked list of pre-shared key extensions.
  8637. * msgType The type of the message this extension is being written into.
  8638. * returns the number of bytes of the encoded pre-shared key extension or
  8639. * SANITY_MSG_E to indicate invalid message type.
  8640. */
  8641. int TLSX_PreSharedKey_GetSizeBinders(PreSharedKey* list, byte msgType,
  8642. word16* pSz)
  8643. {
  8644. word16 len;
  8645. if (msgType != client_hello) {
  8646. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8647. return SANITY_MSG_E;
  8648. }
  8649. /* Length of all binders. */
  8650. len = OPAQUE16_LEN;
  8651. while (list != NULL) {
  8652. len += OPAQUE8_LEN + (word16)list->binderLen;
  8653. list = list->next;
  8654. }
  8655. *pSz = len;
  8656. return 0;
  8657. }
  8658. /* Writes the pre-shared key extension into the output buffer - binders only.
  8659. * Assumes that the the output buffer is big enough to hold data.
  8660. *
  8661. * list The linked list of key share entries.
  8662. * output The buffer to write into.
  8663. * msgType The type of the message this extension is being written into.
  8664. * returns the number of bytes written into the buffer.
  8665. */
  8666. int TLSX_PreSharedKey_WriteBinders(PreSharedKey* list, byte* output,
  8667. byte msgType, word16* pSz)
  8668. {
  8669. PreSharedKey* current = list;
  8670. word16 idx = 0;
  8671. word16 lenIdx;
  8672. word16 len;
  8673. if (msgType != client_hello) {
  8674. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8675. return SANITY_MSG_E;
  8676. }
  8677. /* Skip length of all binders. */
  8678. lenIdx = idx;
  8679. idx += OPAQUE16_LEN;
  8680. while (current != NULL) {
  8681. /* Binder data length. */
  8682. output[idx++] = (byte)current->binderLen;
  8683. /* Binder data. */
  8684. XMEMCPY(output + idx, current->binder, current->binderLen);
  8685. idx += (word16)current->binderLen;
  8686. current = current->next;
  8687. }
  8688. /* Length of the binders. */
  8689. len = idx - lenIdx - OPAQUE16_LEN;
  8690. c16toa(len, output + lenIdx);
  8691. *pSz = idx;
  8692. return 0;
  8693. }
  8694. /* Writes the pre-shared key extension into the output buffer.
  8695. * Assumes that the the output buffer is big enough to hold data.
  8696. *
  8697. * list The linked list of key share entries.
  8698. * output The buffer to write into.
  8699. * msgType The type of the message this extension is being written into.
  8700. * returns the number of bytes written into the buffer.
  8701. */
  8702. static int TLSX_PreSharedKey_Write(PreSharedKey* list, byte* output,
  8703. byte msgType, word16* pSz)
  8704. {
  8705. if (msgType == client_hello) {
  8706. PreSharedKey* current = list;
  8707. word16 idx = 0;
  8708. word16 lenIdx;
  8709. word16 len;
  8710. int ret;
  8711. /* Write identites only. Binders after HMACing over this. */
  8712. lenIdx = idx;
  8713. idx += OPAQUE16_LEN;
  8714. while (current != NULL) {
  8715. /* Identity length */
  8716. c16toa(current->identityLen, output + idx);
  8717. idx += OPAQUE16_LEN;
  8718. /* Identity data */
  8719. XMEMCPY(output + idx, current->identity, current->identityLen);
  8720. idx += current->identityLen;
  8721. /* Obfuscated ticket age. */
  8722. c32toa(current->ticketAge, output + idx);
  8723. idx += OPAQUE32_LEN;
  8724. current = current->next;
  8725. }
  8726. /* Length of the identites. */
  8727. len = idx - lenIdx - OPAQUE16_LEN;
  8728. c16toa(len, output + lenIdx);
  8729. /* Don't include binders here.
  8730. * The binders are based on the hash of all the ClientHello data up to
  8731. * and include the identities written above.
  8732. */
  8733. ret = TLSX_PreSharedKey_GetSizeBinders(list, msgType, &len);
  8734. if (ret < 0)
  8735. return ret;
  8736. *pSz += idx + len;
  8737. }
  8738. else if (msgType == server_hello) {
  8739. word16 i;
  8740. /* Find the index of the chosen identity. */
  8741. for (i=0; list != NULL && !list->chosen; i++)
  8742. list = list->next;
  8743. if (list == NULL) {
  8744. WOLFSSL_ERROR_VERBOSE(BUILD_MSG_ERROR);
  8745. return BUILD_MSG_ERROR;
  8746. }
  8747. /* The index of the identity chosen by the server from the list supplied
  8748. * by the client.
  8749. */
  8750. c16toa(i, output);
  8751. *pSz += OPAQUE16_LEN;
  8752. }
  8753. else {
  8754. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8755. return SANITY_MSG_E;
  8756. }
  8757. return 0;
  8758. }
  8759. /* Parse the pre-shared key extension.
  8760. * Different formats in different messages.
  8761. *
  8762. * ssl The SSL/TLS object.
  8763. * input The extension data.
  8764. * length The length of the extension data.
  8765. * msgType The type of the message this extension is being parsed from.
  8766. * returns 0 on success and other values indicate failure.
  8767. */
  8768. static int TLSX_PreSharedKey_Parse(WOLFSSL* ssl, const byte* input,
  8769. word16 length, byte msgType)
  8770. {
  8771. TLSX* extension;
  8772. PreSharedKey* list;
  8773. if (msgType == client_hello) {
  8774. int ret;
  8775. word16 len;
  8776. word16 idx = 0;
  8777. TLSX_Remove(&ssl->extensions, TLSX_PRE_SHARED_KEY, ssl->heap);
  8778. /* Length of identities and of binders. */
  8779. if ((int)(length - idx) < OPAQUE16_LEN + OPAQUE16_LEN)
  8780. return BUFFER_E;
  8781. /* Length of identities. */
  8782. ato16(input + idx, &len);
  8783. idx += OPAQUE16_LEN;
  8784. if (len < MIN_PSK_ID_LEN || length - idx < len)
  8785. return BUFFER_E;
  8786. /* Create a pre-shared key object for each identity. */
  8787. while (len > 0) {
  8788. const byte* identity;
  8789. word16 identityLen;
  8790. word32 age;
  8791. if (len < OPAQUE16_LEN)
  8792. return BUFFER_E;
  8793. /* Length of identity. */
  8794. ato16(input + idx, &identityLen);
  8795. idx += OPAQUE16_LEN;
  8796. if (len < OPAQUE16_LEN + identityLen + OPAQUE32_LEN ||
  8797. identityLen > MAX_PSK_ID_LEN)
  8798. return BUFFER_E;
  8799. /* Cache identity pointer. */
  8800. identity = input + idx;
  8801. idx += identityLen;
  8802. /* Ticket age. */
  8803. ato32(input + idx, &age);
  8804. idx += OPAQUE32_LEN;
  8805. ret = TLSX_PreSharedKey_Use(ssl, identity, identityLen, age, no_mac,
  8806. 0, 0, 1, NULL);
  8807. if (ret != 0)
  8808. return ret;
  8809. /* Done with this identity. */
  8810. len -= OPAQUE16_LEN + identityLen + OPAQUE32_LEN;
  8811. }
  8812. /* Find the list of identities sent to server. */
  8813. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8814. if (extension == NULL)
  8815. return PSK_KEY_ERROR;
  8816. list = (PreSharedKey*)extension->data;
  8817. /* Length of binders. */
  8818. if (idx + OPAQUE16_LEN > length)
  8819. return BUFFER_E;
  8820. ato16(input + idx, &len);
  8821. idx += OPAQUE16_LEN;
  8822. if (len < MIN_PSK_BINDERS_LEN || length - idx < len)
  8823. return BUFFER_E;
  8824. /* Set binder for each identity. */
  8825. while (list != NULL && len > 0) {
  8826. /* Length of binder */
  8827. list->binderLen = input[idx++];
  8828. if (list->binderLen < WC_SHA256_DIGEST_SIZE ||
  8829. list->binderLen > WC_MAX_DIGEST_SIZE)
  8830. return BUFFER_E;
  8831. if (len < OPAQUE8_LEN + list->binderLen)
  8832. return BUFFER_E;
  8833. /* Copy binder into static buffer. */
  8834. XMEMCPY(list->binder, input + idx, list->binderLen);
  8835. idx += (word16)list->binderLen;
  8836. /* Done with binder entry. */
  8837. len -= OPAQUE8_LEN + (word16)list->binderLen;
  8838. /* Next identity. */
  8839. list = list->next;
  8840. }
  8841. if (list != NULL || len != 0)
  8842. return BUFFER_E;
  8843. return 0;
  8844. }
  8845. if (msgType == server_hello) {
  8846. word16 idx;
  8847. /* Index of identity chosen by server. */
  8848. if (length != OPAQUE16_LEN)
  8849. return BUFFER_E;
  8850. ato16(input, &idx);
  8851. #ifdef WOLFSSL_EARLY_DATA
  8852. ssl->options.pskIdIndex = idx + 1;
  8853. #endif
  8854. /* Find the list of identities sent to server. */
  8855. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8856. if (extension == NULL)
  8857. return PSK_KEY_ERROR;
  8858. list = (PreSharedKey*)extension->data;
  8859. /* Mark the identity as chosen. */
  8860. for (; list != NULL && idx > 0; idx--)
  8861. list = list->next;
  8862. if (list == NULL) {
  8863. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8864. return PSK_KEY_ERROR;
  8865. }
  8866. list->chosen = 1;
  8867. #ifdef HAVE_SESSION_TICKET
  8868. if (list->resumption) {
  8869. /* Check that the session's details are the same as the server's. */
  8870. if (ssl->options.cipherSuite0 != ssl->session->cipherSuite0 ||
  8871. ssl->options.cipherSuite != ssl->session->cipherSuite ||
  8872. ssl->session->version.major != ssl->ctx->method->version.major ||
  8873. ssl->session->version.minor != ssl->ctx->method->version.minor) {
  8874. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8875. return PSK_KEY_ERROR;
  8876. }
  8877. }
  8878. #endif
  8879. return 0;
  8880. }
  8881. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8882. return SANITY_MSG_E;
  8883. }
  8884. /* Create a new pre-shared key and put it into the list.
  8885. *
  8886. * list The linked list of pre-shared key.
  8887. * identity The identity.
  8888. * len The length of the identity data.
  8889. * heap The memory to allocate with.
  8890. * preSharedKey The new pre-shared key object.
  8891. * returns 0 on success and other values indicate failure.
  8892. */
  8893. static int TLSX_PreSharedKey_New(PreSharedKey** list, const byte* identity,
  8894. word16 len, void *heap,
  8895. PreSharedKey** preSharedKey)
  8896. {
  8897. PreSharedKey* psk;
  8898. PreSharedKey** next;
  8899. psk = (PreSharedKey*)XMALLOC(sizeof(PreSharedKey), heap, DYNAMIC_TYPE_TLSX);
  8900. if (psk == NULL)
  8901. return MEMORY_E;
  8902. XMEMSET(psk, 0, sizeof(*psk));
  8903. /* Make a copy of the identity data. */
  8904. psk->identity = (byte*)XMALLOC(len, heap, DYNAMIC_TYPE_TLSX);
  8905. if (psk->identity == NULL) {
  8906. XFREE(psk, heap, DYNAMIC_TYPE_TLSX);
  8907. return MEMORY_E;
  8908. }
  8909. XMEMCPY(psk->identity, identity, len);
  8910. psk->identityLen = len;
  8911. /* Add it to the end and maintain the links. */
  8912. while (*list != NULL) {
  8913. /* Assign to temporary to work around compiler bug found by customer. */
  8914. next = &((*list)->next);
  8915. list = next;
  8916. }
  8917. *list = psk;
  8918. *preSharedKey = psk;
  8919. (void)heap;
  8920. return 0;
  8921. }
  8922. static WC_INLINE byte GetHmacLength(int hmac)
  8923. {
  8924. switch (hmac) {
  8925. #ifndef NO_SHA256
  8926. case sha256_mac:
  8927. return WC_SHA256_DIGEST_SIZE;
  8928. #endif
  8929. #ifdef WOLFSSL_SHA384
  8930. case sha384_mac:
  8931. return WC_SHA384_DIGEST_SIZE;
  8932. #endif
  8933. #ifdef WOLFSSL_SHA512
  8934. case sha512_mac:
  8935. return WC_SHA512_DIGEST_SIZE;
  8936. #endif
  8937. }
  8938. return 0;
  8939. }
  8940. /* Use the data to create a new pre-shared key object in the extensions.
  8941. *
  8942. * ssl The SSL/TLS object.
  8943. * identity The identity.
  8944. * len The length of the identity data.
  8945. * age The age of the identity.
  8946. * hmac The HMAC algorithm.
  8947. * ciphersuite0 The first byte of the ciphersuite to use.
  8948. * ciphersuite The second byte of the ciphersuite to use.
  8949. * resumption The PSK is for resumption of a session.
  8950. * preSharedKey The new pre-shared key object.
  8951. * returns 0 on success and other values indicate failure.
  8952. */
  8953. int TLSX_PreSharedKey_Use(WOLFSSL* ssl, const byte* identity, word16 len,
  8954. word32 age, byte hmac, byte cipherSuite0,
  8955. byte cipherSuite, byte resumption,
  8956. PreSharedKey **preSharedKey)
  8957. {
  8958. int ret = 0;
  8959. TLSX* extension;
  8960. PreSharedKey* psk = NULL;
  8961. /* Find the pre-shared key extension if it exists. */
  8962. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8963. if (extension == NULL) {
  8964. /* Push new pre-shared key extension. */
  8965. ret = TLSX_Push(&ssl->extensions, TLSX_PRE_SHARED_KEY, NULL, ssl->heap);
  8966. if (ret != 0)
  8967. return ret;
  8968. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8969. if (extension == NULL)
  8970. return MEMORY_E;
  8971. }
  8972. /* Try to find the pre-shared key with this identity. */
  8973. psk = (PreSharedKey*)extension->data;
  8974. while (psk != NULL) {
  8975. if ((psk->identityLen == len) &&
  8976. (XMEMCMP(psk->identity, identity, len) == 0)) {
  8977. break;
  8978. }
  8979. psk = psk->next;
  8980. }
  8981. /* Create a new pre-shared key object if not found. */
  8982. if (psk == NULL) {
  8983. ret = TLSX_PreSharedKey_New((PreSharedKey**)&extension->data, identity,
  8984. len, ssl->heap, &psk);
  8985. if (ret != 0)
  8986. return ret;
  8987. }
  8988. /* Update/set age and HMAC algorithm. */
  8989. psk->ticketAge = age;
  8990. psk->hmac = hmac;
  8991. psk->cipherSuite0 = cipherSuite0;
  8992. psk->cipherSuite = cipherSuite;
  8993. psk->resumption = resumption;
  8994. psk->binderLen = GetHmacLength(psk->hmac);
  8995. if (preSharedKey != NULL)
  8996. *preSharedKey = psk;
  8997. return 0;
  8998. }
  8999. #define PSK_FREE_ALL TLSX_PreSharedKey_FreeAll
  9000. #define PSK_GET_SIZE TLSX_PreSharedKey_GetSize
  9001. #define PSK_WRITE TLSX_PreSharedKey_Write
  9002. #define PSK_PARSE TLSX_PreSharedKey_Parse
  9003. #else
  9004. #define PSK_FREE_ALL(a, b)
  9005. #define PSK_GET_SIZE(a, b, c) 0
  9006. #define PSK_WRITE(a, b, c, d) 0
  9007. #define PSK_PARSE(a, b, c, d) 0
  9008. #endif
  9009. /******************************************************************************/
  9010. /* PSK Key Exchange Modes */
  9011. /******************************************************************************/
  9012. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  9013. /* Get the size of the encoded PSK KE modes extension.
  9014. * Only in ClientHello.
  9015. *
  9016. * modes The PSK KE mode bit string.
  9017. * msgType The type of the message this extension is being written into.
  9018. * returns the number of bytes of the encoded PSK KE mode extension.
  9019. */
  9020. static int TLSX_PskKeModes_GetSize(byte modes, byte msgType, word16* pSz)
  9021. {
  9022. if (msgType == client_hello) {
  9023. /* Format: Len | Modes* */
  9024. word16 len = OPAQUE8_LEN;
  9025. /* Check whether each possible mode is to be written. */
  9026. if (modes & (1 << PSK_KE))
  9027. len += OPAQUE8_LEN;
  9028. if (modes & (1 << PSK_DHE_KE))
  9029. len += OPAQUE8_LEN;
  9030. *pSz += len;
  9031. return 0;
  9032. }
  9033. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9034. return SANITY_MSG_E;
  9035. }
  9036. /* Writes the PSK KE modes extension into the output buffer.
  9037. * Assumes that the the output buffer is big enough to hold data.
  9038. * Only in ClientHello.
  9039. *
  9040. * modes The PSK KE mode bit string.
  9041. * output The buffer to write into.
  9042. * msgType The type of the message this extension is being written into.
  9043. * returns the number of bytes written into the buffer.
  9044. */
  9045. static int TLSX_PskKeModes_Write(byte modes, byte* output, byte msgType,
  9046. word16* pSz)
  9047. {
  9048. if (msgType == client_hello) {
  9049. /* Format: Len | Modes* */
  9050. word16 idx = OPAQUE8_LEN;
  9051. /* Write out each possible mode. */
  9052. if (modes & (1 << PSK_KE))
  9053. output[idx++] = PSK_KE;
  9054. if (modes & (1 << PSK_DHE_KE))
  9055. output[idx++] = PSK_DHE_KE;
  9056. /* Write out length of mode list. */
  9057. output[0] = (byte)(idx - OPAQUE8_LEN);
  9058. *pSz += idx;
  9059. return 0;
  9060. }
  9061. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9062. return SANITY_MSG_E;
  9063. }
  9064. /* Parse the PSK KE modes extension.
  9065. * Only in ClientHello.
  9066. *
  9067. * ssl The SSL/TLS object.
  9068. * input The extension data.
  9069. * length The length of the extension data.
  9070. * msgType The type of the message this extension is being parsed from.
  9071. * returns 0 on success and other values indicate failure.
  9072. */
  9073. static int TLSX_PskKeModes_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  9074. byte msgType)
  9075. {
  9076. int ret;
  9077. if (msgType == client_hello) {
  9078. /* Format: Len | Modes* */
  9079. int idx = 0;
  9080. word16 len;
  9081. byte modes = 0;
  9082. /* Ensure length byte exists. */
  9083. if (length < OPAQUE8_LEN)
  9084. return BUFFER_E;
  9085. /* Get length of mode list and ensure that is the only data. */
  9086. len = input[0];
  9087. if (length - OPAQUE8_LEN != len)
  9088. return BUFFER_E;
  9089. idx = OPAQUE8_LEN;
  9090. /* Set a bit for each recognized modes. */
  9091. while (len > 0) {
  9092. /* Ignore unrecognized modes. */
  9093. if (input[idx] <= PSK_DHE_KE)
  9094. modes |= 1 << input[idx];
  9095. idx++;
  9096. len--;
  9097. }
  9098. ret = TLSX_PskKeModes_Use(ssl, modes);
  9099. if (ret != 0)
  9100. return ret;
  9101. return 0;
  9102. }
  9103. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9104. return SANITY_MSG_E;
  9105. }
  9106. /* Use the data to create a new PSK Key Exchange Modes object in the extensions.
  9107. *
  9108. * ssl The SSL/TLS object.
  9109. * modes The PSK key exchange modes.
  9110. * returns 0 on success and other values indicate failure.
  9111. */
  9112. int TLSX_PskKeModes_Use(WOLFSSL* ssl, byte modes)
  9113. {
  9114. int ret = 0;
  9115. TLSX* extension;
  9116. /* Find the PSK key exchange modes extension if it exists. */
  9117. extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
  9118. if (extension == NULL) {
  9119. /* Push new PSK key exchange modes extension. */
  9120. ret = TLSX_Push(&ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES, NULL,
  9121. ssl->heap);
  9122. if (ret != 0)
  9123. return ret;
  9124. extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
  9125. if (extension == NULL)
  9126. return MEMORY_E;
  9127. }
  9128. extension->val = modes;
  9129. return 0;
  9130. }
  9131. #define PKM_GET_SIZE TLSX_PskKeModes_GetSize
  9132. #define PKM_WRITE TLSX_PskKeModes_Write
  9133. #define PKM_PARSE TLSX_PskKeModes_Parse
  9134. #else
  9135. #define PKM_GET_SIZE(a, b, c) 0
  9136. #define PKM_WRITE(a, b, c, d) 0
  9137. #define PKM_PARSE(a, b, c, d) 0
  9138. #endif
  9139. /******************************************************************************/
  9140. /* Post-Handshake Authentication */
  9141. /******************************************************************************/
  9142. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  9143. /* Get the size of the encoded Post-Handshake Authentication extension.
  9144. * Only in ClientHello.
  9145. *
  9146. * msgType The type of the message this extension is being written into.
  9147. * returns the number of bytes of the encoded Post-Handshake Authentication
  9148. * extension.
  9149. */
  9150. static int TLSX_PostHandAuth_GetSize(byte msgType, word16* pSz)
  9151. {
  9152. if (msgType == client_hello) {
  9153. *pSz += 0;
  9154. return 0;
  9155. }
  9156. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9157. return SANITY_MSG_E;
  9158. }
  9159. /* Writes the Post-Handshake Authentication extension into the output buffer.
  9160. * Assumes that the the output buffer is big enough to hold data.
  9161. * Only in ClientHello.
  9162. *
  9163. * output The buffer to write into.
  9164. * msgType The type of the message this extension is being written into.
  9165. * returns the number of bytes written into the buffer.
  9166. */
  9167. static int TLSX_PostHandAuth_Write(byte* output, byte msgType, word16* pSz)
  9168. {
  9169. (void)output;
  9170. if (msgType == client_hello) {
  9171. *pSz += 0;
  9172. return 0;
  9173. }
  9174. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9175. return SANITY_MSG_E;
  9176. }
  9177. /* Parse the Post-Handshake Authentication extension.
  9178. * Only in ClientHello.
  9179. *
  9180. * ssl The SSL/TLS object.
  9181. * input The extension data.
  9182. * length The length of the extension data.
  9183. * msgType The type of the message this extension is being parsed from.
  9184. * returns 0 on success and other values indicate failure.
  9185. */
  9186. static int TLSX_PostHandAuth_Parse(WOLFSSL* ssl, const byte* input,
  9187. word16 length, byte msgType)
  9188. {
  9189. (void)input;
  9190. if (msgType == client_hello) {
  9191. /* Ensure extension is empty. */
  9192. if (length != 0)
  9193. return BUFFER_E;
  9194. ssl->options.postHandshakeAuth = 1;
  9195. return 0;
  9196. }
  9197. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9198. return SANITY_MSG_E;
  9199. }
  9200. /* Create a new Post-handshake authentication object in the extensions.
  9201. *
  9202. * ssl The SSL/TLS object.
  9203. * returns 0 on success and other values indicate failure.
  9204. */
  9205. static int TLSX_PostHandAuth_Use(WOLFSSL* ssl)
  9206. {
  9207. int ret = 0;
  9208. TLSX* extension;
  9209. /* Find the PSK key exchange modes extension if it exists. */
  9210. extension = TLSX_Find(ssl->extensions, TLSX_POST_HANDSHAKE_AUTH);
  9211. if (extension == NULL) {
  9212. /* Push new Post-handshake Authentication extension. */
  9213. ret = TLSX_Push(&ssl->extensions, TLSX_POST_HANDSHAKE_AUTH, NULL,
  9214. ssl->heap);
  9215. if (ret != 0)
  9216. return ret;
  9217. }
  9218. return 0;
  9219. }
  9220. #define PHA_GET_SIZE TLSX_PostHandAuth_GetSize
  9221. #define PHA_WRITE TLSX_PostHandAuth_Write
  9222. #define PHA_PARSE TLSX_PostHandAuth_Parse
  9223. #else
  9224. #define PHA_GET_SIZE(a, b) 0
  9225. #define PHA_WRITE(a, b, c) 0
  9226. #define PHA_PARSE(a, b, c, d) 0
  9227. #endif
  9228. /******************************************************************************/
  9229. /* Early Data Indication */
  9230. /******************************************************************************/
  9231. #ifdef WOLFSSL_EARLY_DATA
  9232. /* Get the size of the encoded Early Data Indication extension.
  9233. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  9234. *
  9235. * msgType The type of the message this extension is being written into.
  9236. * returns the number of bytes of the encoded Early Data Indication extension.
  9237. */
  9238. static int TLSX_EarlyData_GetSize(byte msgType, word16* pSz)
  9239. {
  9240. int ret = 0;
  9241. if (msgType == client_hello || msgType == encrypted_extensions)
  9242. *pSz += 0;
  9243. else if (msgType == session_ticket)
  9244. *pSz += OPAQUE32_LEN;
  9245. else {
  9246. ret = SANITY_MSG_E;
  9247. WOLFSSL_ERROR_VERBOSE(ret);
  9248. }
  9249. return ret;
  9250. }
  9251. /* Writes the Early Data Indicator extension into the output buffer.
  9252. * Assumes that the the output buffer is big enough to hold data.
  9253. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  9254. *
  9255. * maxSz The maximum early data size.
  9256. * output The buffer to write into.
  9257. * msgType The type of the message this extension is being written into.
  9258. * returns the number of bytes written into the buffer.
  9259. */
  9260. static int TLSX_EarlyData_Write(word32 maxSz, byte* output, byte msgType,
  9261. word16* pSz)
  9262. {
  9263. if (msgType == client_hello || msgType == encrypted_extensions)
  9264. return 0;
  9265. else if (msgType == session_ticket) {
  9266. c32toa(maxSz, output);
  9267. *pSz += OPAQUE32_LEN;
  9268. return 0;
  9269. }
  9270. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9271. return SANITY_MSG_E;
  9272. }
  9273. /* Parse the Early Data Indicator extension.
  9274. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  9275. *
  9276. * ssl The SSL/TLS object.
  9277. * input The extension data.
  9278. * length The length of the extension data.
  9279. * msgType The type of the message this extension is being parsed from.
  9280. * returns 0 on success and other values indicate failure.
  9281. */
  9282. static int TLSX_EarlyData_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  9283. byte msgType)
  9284. {
  9285. if (msgType == client_hello) {
  9286. if (length != 0)
  9287. return BUFFER_E;
  9288. if (ssl->earlyData == expecting_early_data) {
  9289. if (ssl->options.maxEarlyDataSz != 0)
  9290. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_ACCEPTED;
  9291. else
  9292. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_REJECTED;
  9293. return TLSX_EarlyData_Use(ssl, 0, 0);
  9294. }
  9295. ssl->earlyData = early_data_ext;
  9296. return 0;
  9297. }
  9298. if (msgType == encrypted_extensions) {
  9299. if (length != 0)
  9300. return BUFFER_E;
  9301. /* Ensure the index of PSK identity chosen by server is 0.
  9302. * Index is plus one to handle 'not set' value of 0.
  9303. */
  9304. if (ssl->options.pskIdIndex != 1) {
  9305. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  9306. return PSK_KEY_ERROR;
  9307. }
  9308. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  9309. /* the extension from server comes in */
  9310. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_ACCEPTED;
  9311. }
  9312. return TLSX_EarlyData_Use(ssl, 1, 1);
  9313. }
  9314. if (msgType == session_ticket) {
  9315. word32 maxSz;
  9316. if (length != OPAQUE32_LEN)
  9317. return BUFFER_E;
  9318. ato32(input, &maxSz);
  9319. ssl->session->maxEarlyDataSz = maxSz;
  9320. return 0;
  9321. }
  9322. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9323. return SANITY_MSG_E;
  9324. }
  9325. /* Use the data to create a new Early Data object in the extensions.
  9326. *
  9327. * ssl The SSL/TLS object.
  9328. * maxSz The maximum early data size.
  9329. * is_response if this extension is part of a response
  9330. * returns 0 on success and other values indicate failure.
  9331. */
  9332. int TLSX_EarlyData_Use(WOLFSSL* ssl, word32 maxSz, int is_response)
  9333. {
  9334. int ret = 0;
  9335. TLSX* extension;
  9336. /* Find the early data extension if it exists. */
  9337. extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
  9338. if (extension == NULL) {
  9339. /* Push new early data extension. */
  9340. ret = TLSX_Push(&ssl->extensions, TLSX_EARLY_DATA, NULL, ssl->heap);
  9341. if (ret != 0)
  9342. return ret;
  9343. extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
  9344. if (extension == NULL)
  9345. return MEMORY_E;
  9346. }
  9347. extension->resp = is_response;
  9348. extension->val = maxSz;
  9349. return 0;
  9350. }
  9351. #define EDI_GET_SIZE TLSX_EarlyData_GetSize
  9352. #define EDI_WRITE TLSX_EarlyData_Write
  9353. #define EDI_PARSE TLSX_EarlyData_Parse
  9354. #else
  9355. #define EDI_GET_SIZE(a, b) 0
  9356. #define EDI_WRITE(a, b, c, d) 0
  9357. #define EDI_PARSE(a, b, c, d) 0
  9358. #endif
  9359. /******************************************************************************/
  9360. /* QUIC transport parameter extension */
  9361. /******************************************************************************/
  9362. #ifdef WOLFSSL_QUIC
  9363. static word16 TLSX_QuicTP_GetSize(TLSX* extension)
  9364. {
  9365. const QuicTransportParam *tp = (QuicTransportParam*)extension->data;
  9366. return tp ? tp->len : 0;
  9367. }
  9368. int TLSX_QuicTP_Use(WOLFSSL* ssl, TLSX_Type ext_type, int is_response)
  9369. {
  9370. int ret = 0;
  9371. TLSX* extension;
  9372. WOLFSSL_ENTER("TLSX_QuicTP_Use");
  9373. if (ssl->quic.transport_local == NULL) {
  9374. /* RFC9000, ch 7.3: "An endpoint MUST treat the absence of [...]
  9375. * from either endpoint [...] as a connection error of type
  9376. * TRANSPORT_PARAMETER_ERROR."
  9377. */
  9378. ret = QUIC_TP_MISSING_E;
  9379. goto cleanup;
  9380. }
  9381. extension = TLSX_Find(ssl->extensions, ext_type);
  9382. if (extension == NULL) {
  9383. ret = TLSX_Push(&ssl->extensions, ext_type, NULL, ssl->heap);
  9384. if (ret != 0)
  9385. goto cleanup;
  9386. extension = TLSX_Find(ssl->extensions, ext_type);
  9387. if (extension == NULL) {
  9388. ret = MEMORY_E;
  9389. goto cleanup;
  9390. }
  9391. }
  9392. if (extension->data) {
  9393. QuicTransportParam_free((QuicTransportParam*)extension->data, ssl->heap);
  9394. extension->data = NULL;
  9395. }
  9396. extension->resp = is_response;
  9397. extension->data = (void*)QuicTransportParam_dup(ssl->quic.transport_local, ssl->heap);
  9398. if (!extension->data) {
  9399. ret = MEMORY_E;
  9400. goto cleanup;
  9401. }
  9402. cleanup:
  9403. WOLFSSL_LEAVE("TLSX_QuicTP_Use", ret);
  9404. return ret;
  9405. }
  9406. static word16 TLSX_QuicTP_Write(QuicTransportParam *tp, byte* output)
  9407. {
  9408. word16 len = 0;
  9409. WOLFSSL_ENTER("TLSX_QuicTP_Write");
  9410. if (tp && tp->len) {
  9411. XMEMCPY(output, tp->data, tp->len);
  9412. len = tp->len;
  9413. }
  9414. WOLFSSL_LEAVE("TLSX_QuicTP_Write", len);
  9415. return len;
  9416. }
  9417. static int TLSX_QuicTP_Parse(WOLFSSL *ssl, const byte *input, size_t len, int ext_type, int msgType)
  9418. {
  9419. const QuicTransportParam *tp, **ptp;
  9420. (void)msgType;
  9421. tp = QuicTransportParam_new(input, len, ssl->heap);
  9422. if (!tp) {
  9423. return MEMORY_E;
  9424. }
  9425. ptp = (ext_type == TLSX_KEY_QUIC_TP_PARAMS_DRAFT) ?
  9426. &ssl->quic.transport_peer_draft : &ssl->quic.transport_peer;
  9427. if (*ptp) {
  9428. QTP_FREE(*ptp, ssl->heap);
  9429. }
  9430. *ptp = tp;
  9431. return 0;
  9432. }
  9433. #define QTP_GET_SIZE TLSX_QuicTP_GetSize
  9434. #define QTP_USE TLSX_QuicTP_Use
  9435. #define QTP_WRITE TLSX_QuicTP_Write
  9436. #define QTP_PARSE TLSX_QuicTP_Parse
  9437. #endif /* WOLFSSL_QUIC */
  9438. #if defined(WOLFSSL_DTLS_CID)
  9439. #define CID_GET_SIZE TLSX_ConnectionID_GetSize
  9440. #define CID_WRITE TLSX_ConnectionID_Write
  9441. #define CID_PARSE TLSX_ConnectionID_Parse
  9442. #define CID_FREE TLSX_ConnectionID_Free
  9443. #else
  9444. #define CID_GET_SIZE(a) 0
  9445. #define CID_WRITE(a, b) 0
  9446. #define CID_PARSE(a, b, c, d) 0
  9447. #define CID_FREE(a, b) 0
  9448. #endif /* defined(WOLFSSL_DTLS_CID) */
  9449. /******************************************************************************/
  9450. /* TLS Extensions Framework */
  9451. /******************************************************************************/
  9452. /** Finds an extension in the provided list. */
  9453. TLSX* TLSX_Find(TLSX* list, TLSX_Type type)
  9454. {
  9455. TLSX* extension = list;
  9456. while (extension && extension->type != type)
  9457. extension = extension->next;
  9458. return extension;
  9459. }
  9460. /** Remove an extension. */
  9461. void TLSX_Remove(TLSX** list, TLSX_Type type, void* heap)
  9462. {
  9463. TLSX* extension = *list;
  9464. TLSX** next = list;
  9465. while (extension && extension->type != type) {
  9466. next = &extension->next;
  9467. extension = extension->next;
  9468. }
  9469. if (extension) {
  9470. *next = extension->next;
  9471. extension->next = NULL;
  9472. TLSX_FreeAll(extension, heap);
  9473. }
  9474. }
  9475. /** Releases all extensions in the provided list. */
  9476. void TLSX_FreeAll(TLSX* list, void* heap)
  9477. {
  9478. TLSX* extension;
  9479. while ((extension = list)) {
  9480. list = extension->next;
  9481. switch (extension->type) {
  9482. #ifdef HAVE_SNI
  9483. case TLSX_SERVER_NAME:
  9484. SNI_FREE_ALL((SNI*)extension->data, heap);
  9485. break;
  9486. #endif
  9487. case TLSX_TRUSTED_CA_KEYS:
  9488. TCA_FREE_ALL((TCA*)extension->data, heap);
  9489. break;
  9490. case TLSX_MAX_FRAGMENT_LENGTH:
  9491. MFL_FREE_ALL(extension->data, heap);
  9492. break;
  9493. case TLSX_EXTENDED_MASTER_SECRET:
  9494. case TLSX_TRUNCATED_HMAC:
  9495. /* Nothing to do. */
  9496. break;
  9497. case TLSX_SUPPORTED_GROUPS:
  9498. EC_FREE_ALL((SupportedCurve*)extension->data, heap);
  9499. break;
  9500. case TLSX_EC_POINT_FORMATS:
  9501. PF_FREE_ALL((PointFormat*)extension->data, heap);
  9502. break;
  9503. case TLSX_STATUS_REQUEST:
  9504. CSR_FREE_ALL((CertificateStatusRequest*)extension->data, heap);
  9505. break;
  9506. case TLSX_STATUS_REQUEST_V2:
  9507. CSR2_FREE_ALL((CertificateStatusRequestItemV2*)extension->data,
  9508. heap);
  9509. break;
  9510. case TLSX_RENEGOTIATION_INFO:
  9511. SCR_FREE_ALL(extension->data, heap);
  9512. break;
  9513. case TLSX_SESSION_TICKET:
  9514. WOLF_STK_FREE(extension->data, heap);
  9515. break;
  9516. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9517. ALPN_FREE_ALL((ALPN*)extension->data, heap);
  9518. break;
  9519. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9520. case TLSX_SIGNATURE_ALGORITHMS:
  9521. break;
  9522. #endif
  9523. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9524. case TLSX_ENCRYPT_THEN_MAC:
  9525. break;
  9526. #endif
  9527. #ifdef WOLFSSL_TLS13
  9528. case TLSX_SUPPORTED_VERSIONS:
  9529. break;
  9530. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9531. case TLSX_COOKIE:
  9532. CKE_FREE_ALL((Cookie*)extension->data, heap);
  9533. break;
  9534. #endif
  9535. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9536. case TLSX_PRE_SHARED_KEY:
  9537. PSK_FREE_ALL((PreSharedKey*)extension->data, heap);
  9538. break;
  9539. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9540. break;
  9541. #endif
  9542. #ifdef WOLFSSL_EARLY_DATA
  9543. case TLSX_EARLY_DATA:
  9544. break;
  9545. #endif
  9546. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9547. case TLSX_POST_HANDSHAKE_AUTH:
  9548. break;
  9549. #endif
  9550. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9551. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9552. break;
  9553. #endif
  9554. case TLSX_KEY_SHARE:
  9555. KS_FREE_ALL((KeyShareEntry*)extension->data, heap);
  9556. break;
  9557. #endif
  9558. #ifdef WOLFSSL_SRTP
  9559. case TLSX_USE_SRTP:
  9560. SRTP_FREE((TlsxSrtp*)extension->data, heap);
  9561. break;
  9562. #endif
  9563. #ifdef WOLFSSL_QUIC
  9564. case TLSX_KEY_QUIC_TP_PARAMS:
  9565. FALL_THROUGH;
  9566. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9567. QTP_FREE((QuicTransportParam*)extension->data, heap);
  9568. break;
  9569. #endif
  9570. #ifdef WOLFSSL_DTLS_CID
  9571. case TLSX_CONNECTION_ID:
  9572. CID_FREE((byte*)extension->data, heap);
  9573. break;
  9574. #endif /* WOLFSSL_DTLS_CID */
  9575. default:
  9576. break;
  9577. }
  9578. XFREE(extension, heap, DYNAMIC_TYPE_TLSX);
  9579. }
  9580. (void)heap;
  9581. }
  9582. /** Checks if the tls extensions are supported based on the protocol version. */
  9583. int TLSX_SupportExtensions(WOLFSSL* ssl) {
  9584. return ssl && (IsTLS(ssl) || ssl->version.major == DTLS_MAJOR);
  9585. }
  9586. /** Tells the buffered size of the extensions in a list. */
  9587. static int TLSX_GetSize(TLSX* list, byte* semaphore, byte msgType,
  9588. word16* pLength)
  9589. {
  9590. int ret = 0;
  9591. TLSX* extension;
  9592. word16 length = 0;
  9593. byte isRequest = (msgType == client_hello ||
  9594. msgType == certificate_request);
  9595. while ((extension = list)) {
  9596. list = extension->next;
  9597. /* only extensions marked as response are sent back to the client. */
  9598. if (!isRequest && !extension->resp)
  9599. continue; /* skip! */
  9600. /* ssl level extensions are expected to override ctx level ones. */
  9601. if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
  9602. continue; /* skip! */
  9603. /* extension type + extension data length. */
  9604. length += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
  9605. switch (extension->type) {
  9606. #ifdef HAVE_SNI
  9607. case TLSX_SERVER_NAME:
  9608. /* SNI only sends the name on the request. */
  9609. if (isRequest)
  9610. length += SNI_GET_SIZE((SNI*)extension->data);
  9611. break;
  9612. #endif
  9613. case TLSX_TRUSTED_CA_KEYS:
  9614. /* TCA only sends the list on the request. */
  9615. if (isRequest)
  9616. length += TCA_GET_SIZE((TCA*)extension->data);
  9617. break;
  9618. case TLSX_MAX_FRAGMENT_LENGTH:
  9619. length += MFL_GET_SIZE(extension->data);
  9620. break;
  9621. case TLSX_EXTENDED_MASTER_SECRET:
  9622. case TLSX_TRUNCATED_HMAC:
  9623. /* always empty. */
  9624. break;
  9625. case TLSX_SUPPORTED_GROUPS:
  9626. length += EC_GET_SIZE((SupportedCurve*)extension->data);
  9627. break;
  9628. case TLSX_EC_POINT_FORMATS:
  9629. length += PF_GET_SIZE((PointFormat*)extension->data);
  9630. break;
  9631. case TLSX_STATUS_REQUEST:
  9632. length += CSR_GET_SIZE(
  9633. (CertificateStatusRequest*)extension->data, isRequest);
  9634. break;
  9635. case TLSX_STATUS_REQUEST_V2:
  9636. length += CSR2_GET_SIZE(
  9637. (CertificateStatusRequestItemV2*)extension->data,
  9638. isRequest);
  9639. break;
  9640. case TLSX_RENEGOTIATION_INFO:
  9641. length += SCR_GET_SIZE((SecureRenegotiation*)extension->data,
  9642. isRequest);
  9643. break;
  9644. case TLSX_SESSION_TICKET:
  9645. length += WOLF_STK_GET_SIZE((SessionTicket*)extension->data,
  9646. isRequest);
  9647. break;
  9648. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9649. length += ALPN_GET_SIZE((ALPN*)extension->data);
  9650. break;
  9651. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9652. case TLSX_SIGNATURE_ALGORITHMS:
  9653. length += SA_GET_SIZE(extension->data);
  9654. break;
  9655. #endif
  9656. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9657. case TLSX_ENCRYPT_THEN_MAC:
  9658. ret = ETM_GET_SIZE(msgType, &length);
  9659. break;
  9660. #endif /* HAVE_ENCRYPT_THEN_MAC */
  9661. #ifdef WOLFSSL_TLS13
  9662. case TLSX_SUPPORTED_VERSIONS:
  9663. ret = SV_GET_SIZE(extension->data, msgType, &length);
  9664. break;
  9665. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9666. case TLSX_COOKIE:
  9667. ret = CKE_GET_SIZE((Cookie*)extension->data, msgType, &length);
  9668. break;
  9669. #endif
  9670. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9671. case TLSX_PRE_SHARED_KEY:
  9672. ret = PSK_GET_SIZE((PreSharedKey*)extension->data, msgType,
  9673. &length);
  9674. break;
  9675. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9676. ret = PKM_GET_SIZE((byte)extension->val, msgType, &length);
  9677. break;
  9678. #endif
  9679. #ifdef WOLFSSL_EARLY_DATA
  9680. case TLSX_EARLY_DATA:
  9681. ret = EDI_GET_SIZE(msgType, &length);
  9682. break;
  9683. #endif
  9684. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9685. case TLSX_POST_HANDSHAKE_AUTH:
  9686. ret = PHA_GET_SIZE(msgType, &length);
  9687. break;
  9688. #endif
  9689. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9690. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9691. length += SAC_GET_SIZE(extension->data);
  9692. break;
  9693. #endif
  9694. case TLSX_KEY_SHARE:
  9695. length += KS_GET_SIZE((KeyShareEntry*)extension->data, msgType);
  9696. break;
  9697. #endif
  9698. #ifdef WOLFSSL_SRTP
  9699. case TLSX_USE_SRTP:
  9700. length += SRTP_GET_SIZE((TlsxSrtp*)extension->data);
  9701. break;
  9702. #endif
  9703. #ifdef WOLFSSL_QUIC
  9704. case TLSX_KEY_QUIC_TP_PARAMS:
  9705. FALL_THROUGH; /* followed by */
  9706. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9707. length += QTP_GET_SIZE(extension);
  9708. break;
  9709. #endif
  9710. #ifdef WOLFSSL_DTLS_CID
  9711. case TLSX_CONNECTION_ID:
  9712. length += CID_GET_SIZE((byte*)extension->data);
  9713. break;
  9714. #endif /* WOLFSSL_DTLS_CID */
  9715. default:
  9716. break;
  9717. }
  9718. /* marks the extension as processed so ctx level */
  9719. /* extensions don't overlap with ssl level ones. */
  9720. TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
  9721. }
  9722. *pLength += length;
  9723. return ret;
  9724. }
  9725. /** Writes the extensions of a list in a buffer. */
  9726. static int TLSX_Write(TLSX* list, byte* output, byte* semaphore,
  9727. byte msgType, word16* pOffset)
  9728. {
  9729. int ret = 0;
  9730. TLSX* extension;
  9731. word16 offset = 0;
  9732. word16 length_offset = 0;
  9733. byte isRequest = (msgType == client_hello ||
  9734. msgType == certificate_request);
  9735. while ((extension = list)) {
  9736. list = extension->next;
  9737. /* only extensions marked as response are written in a response. */
  9738. if (!isRequest && !extension->resp)
  9739. continue; /* skip! */
  9740. /* ssl level extensions are expected to override ctx level ones. */
  9741. if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
  9742. continue; /* skip! */
  9743. /* writes extension type. */
  9744. c16toa(extension->type, output + offset);
  9745. offset += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
  9746. length_offset = offset;
  9747. /* extension data should be written internally. */
  9748. switch (extension->type) {
  9749. #ifdef HAVE_SNI
  9750. case TLSX_SERVER_NAME:
  9751. if (isRequest) {
  9752. WOLFSSL_MSG("SNI extension to write");
  9753. offset += SNI_WRITE((SNI*)extension->data, output + offset);
  9754. }
  9755. break;
  9756. #endif
  9757. case TLSX_TRUSTED_CA_KEYS:
  9758. WOLFSSL_MSG("Trusted CA Indication extension to write");
  9759. if (isRequest) {
  9760. offset += TCA_WRITE((TCA*)extension->data, output + offset);
  9761. }
  9762. break;
  9763. case TLSX_MAX_FRAGMENT_LENGTH:
  9764. WOLFSSL_MSG("Max Fragment Length extension to write");
  9765. offset += MFL_WRITE((byte*)extension->data, output + offset);
  9766. break;
  9767. case TLSX_EXTENDED_MASTER_SECRET:
  9768. WOLFSSL_MSG("Extended Master Secret");
  9769. /* always empty. */
  9770. break;
  9771. case TLSX_TRUNCATED_HMAC:
  9772. WOLFSSL_MSG("Truncated HMAC extension to write");
  9773. /* always empty. */
  9774. break;
  9775. case TLSX_SUPPORTED_GROUPS:
  9776. WOLFSSL_MSG("Supported Groups extension to write");
  9777. offset += EC_WRITE((SupportedCurve*)extension->data,
  9778. output + offset);
  9779. break;
  9780. case TLSX_EC_POINT_FORMATS:
  9781. WOLFSSL_MSG("Point Formats extension to write");
  9782. offset += PF_WRITE((PointFormat*)extension->data,
  9783. output + offset);
  9784. break;
  9785. case TLSX_STATUS_REQUEST:
  9786. WOLFSSL_MSG("Certificate Status Request extension to write");
  9787. offset += CSR_WRITE((CertificateStatusRequest*)extension->data,
  9788. output + offset, isRequest);
  9789. break;
  9790. case TLSX_STATUS_REQUEST_V2:
  9791. WOLFSSL_MSG("Certificate Status Request v2 extension to write");
  9792. offset += CSR2_WRITE(
  9793. (CertificateStatusRequestItemV2*)extension->data,
  9794. output + offset, isRequest);
  9795. break;
  9796. case TLSX_RENEGOTIATION_INFO:
  9797. WOLFSSL_MSG("Secure Renegotiation extension to write");
  9798. offset += SCR_WRITE((SecureRenegotiation*)extension->data,
  9799. output + offset, isRequest);
  9800. break;
  9801. case TLSX_SESSION_TICKET:
  9802. WOLFSSL_MSG("Session Ticket extension to write");
  9803. offset += WOLF_STK_WRITE((SessionTicket*)extension->data,
  9804. output + offset, isRequest);
  9805. break;
  9806. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9807. WOLFSSL_MSG("ALPN extension to write");
  9808. offset += ALPN_WRITE((ALPN*)extension->data, output + offset);
  9809. break;
  9810. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9811. case TLSX_SIGNATURE_ALGORITHMS:
  9812. WOLFSSL_MSG("Signature Algorithms extension to write");
  9813. offset += SA_WRITE(extension->data, output + offset);
  9814. break;
  9815. #endif
  9816. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9817. case TLSX_ENCRYPT_THEN_MAC:
  9818. WOLFSSL_MSG("Encrypt-Then-Mac extension to write");
  9819. ret = ETM_WRITE(extension->data, output, msgType, &offset);
  9820. break;
  9821. #endif /* HAVE_ENCRYPT_THEN_MAC */
  9822. #ifdef WOLFSSL_TLS13
  9823. case TLSX_SUPPORTED_VERSIONS:
  9824. WOLFSSL_MSG("Supported Versions extension to write");
  9825. ret = SV_WRITE(extension->data, output + offset, msgType, &offset);
  9826. break;
  9827. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9828. case TLSX_COOKIE:
  9829. WOLFSSL_MSG("Cookie extension to write");
  9830. ret = CKE_WRITE((Cookie*)extension->data, output + offset,
  9831. msgType, &offset);
  9832. break;
  9833. #endif
  9834. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9835. case TLSX_PRE_SHARED_KEY:
  9836. WOLFSSL_MSG("Pre-Shared Key extension to write");
  9837. ret = PSK_WRITE((PreSharedKey*)extension->data, output + offset,
  9838. msgType, &offset);
  9839. break;
  9840. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9841. WOLFSSL_MSG("PSK Key Exchange Modes extension to write");
  9842. ret = PKM_WRITE((byte)extension->val, output + offset, msgType,
  9843. &offset);
  9844. break;
  9845. #endif
  9846. #ifdef WOLFSSL_EARLY_DATA
  9847. case TLSX_EARLY_DATA:
  9848. WOLFSSL_MSG("Early Data extension to write");
  9849. ret = EDI_WRITE(extension->val, output + offset, msgType,
  9850. &offset);
  9851. break;
  9852. #endif
  9853. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9854. case TLSX_POST_HANDSHAKE_AUTH:
  9855. WOLFSSL_MSG("Post-Handshake Authentication extension to write");
  9856. ret = PHA_WRITE(output + offset, msgType, &offset);
  9857. break;
  9858. #endif
  9859. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9860. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9861. WOLFSSL_MSG("Signature Algorithms extension to write");
  9862. offset += SAC_WRITE(extension->data, output + offset);
  9863. break;
  9864. #endif
  9865. case TLSX_KEY_SHARE:
  9866. WOLFSSL_MSG("Key Share extension to write");
  9867. offset += KS_WRITE((KeyShareEntry*)extension->data,
  9868. output + offset, msgType);
  9869. break;
  9870. #endif
  9871. #ifdef WOLFSSL_SRTP
  9872. case TLSX_USE_SRTP:
  9873. offset += SRTP_WRITE((TlsxSrtp*)extension->data, output+offset);
  9874. break;
  9875. #endif
  9876. #ifdef WOLFSSL_QUIC
  9877. case TLSX_KEY_QUIC_TP_PARAMS:
  9878. FALL_THROUGH;
  9879. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9880. WOLFSSL_MSG("QUIC transport parameter to write");
  9881. offset += QTP_WRITE((QuicTransportParam*)extension->data,
  9882. output + offset);
  9883. break;
  9884. #endif
  9885. #ifdef WOLFSSL_DTLS_CID
  9886. case TLSX_CONNECTION_ID:
  9887. offset += CID_WRITE((byte*)extension->data, output+offset);
  9888. break;
  9889. #endif /* WOLFSSL_DTLS_CID */
  9890. default:
  9891. break;
  9892. }
  9893. /* writes extension data length. */
  9894. c16toa(offset - length_offset, output + length_offset - OPAQUE16_LEN);
  9895. /* marks the extension as processed so ctx level */
  9896. /* extensions don't overlap with ssl level ones. */
  9897. TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
  9898. }
  9899. *pOffset += offset;
  9900. return ret;
  9901. }
  9902. #ifdef HAVE_SUPPORTED_CURVES
  9903. /* Populates the default supported groups / curves */
  9904. static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions)
  9905. {
  9906. int ret = WOLFSSL_SUCCESS;
  9907. #ifdef WOLFSSL_TLS13
  9908. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9909. if (ssl->options.resuming && ssl->session->namedGroup != 0) {
  9910. return TLSX_UseSupportedCurve(extensions, ssl->session->namedGroup,
  9911. ssl->heap);
  9912. }
  9913. #endif
  9914. if (ssl->numGroups != 0) {
  9915. int i;
  9916. for (i = 0; i < ssl->numGroups; i++) {
  9917. ret = TLSX_UseSupportedCurve(extensions, ssl->group[i], ssl->heap);
  9918. if (ret != WOLFSSL_SUCCESS)
  9919. return ret;
  9920. }
  9921. return WOLFSSL_SUCCESS;
  9922. }
  9923. #endif /* WOLFSSL_TLS13 */
  9924. #if defined(HAVE_ECC)
  9925. /* list in order by strength, since not all servers choose by strength */
  9926. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  9927. #ifndef NO_ECC_SECP
  9928. ret = TLSX_UseSupportedCurve(extensions,
  9929. WOLFSSL_ECC_SECP521R1, ssl->heap);
  9930. if (ret != WOLFSSL_SUCCESS) return ret;
  9931. #endif
  9932. #endif
  9933. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  9934. #ifdef HAVE_ECC_BRAINPOOL
  9935. ret = TLSX_UseSupportedCurve(extensions,
  9936. WOLFSSL_ECC_BRAINPOOLP512R1, ssl->heap);
  9937. if (ret != WOLFSSL_SUCCESS) return ret;
  9938. #endif
  9939. #endif
  9940. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  9941. #ifndef NO_ECC_SECP
  9942. ret = TLSX_UseSupportedCurve(extensions,
  9943. WOLFSSL_ECC_SECP384R1, ssl->heap);
  9944. if (ret != WOLFSSL_SUCCESS) return ret;
  9945. #endif
  9946. #ifdef HAVE_ECC_BRAINPOOL
  9947. ret = TLSX_UseSupportedCurve(extensions,
  9948. WOLFSSL_ECC_BRAINPOOLP384R1, ssl->heap);
  9949. if (ret != WOLFSSL_SUCCESS) return ret;
  9950. #endif
  9951. #endif
  9952. #endif /* HAVE_ECC */
  9953. #ifndef HAVE_FIPS
  9954. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  9955. ret = TLSX_UseSupportedCurve(extensions,
  9956. WOLFSSL_ECC_X448, ssl->heap);
  9957. if (ret != WOLFSSL_SUCCESS) return ret;
  9958. #endif
  9959. #endif /* HAVE_FIPS */
  9960. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  9961. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  9962. #ifndef NO_ECC_SECP
  9963. ret = TLSX_UseSupportedCurve(extensions,
  9964. WOLFSSL_ECC_SECP256R1, ssl->heap);
  9965. if (ret != WOLFSSL_SUCCESS) return ret;
  9966. #endif
  9967. #ifdef HAVE_ECC_KOBLITZ
  9968. ret = TLSX_UseSupportedCurve(extensions,
  9969. WOLFSSL_ECC_SECP256K1, ssl->heap);
  9970. if (ret != WOLFSSL_SUCCESS) return ret;
  9971. #endif
  9972. #ifdef HAVE_ECC_BRAINPOOL
  9973. ret = TLSX_UseSupportedCurve(extensions,
  9974. WOLFSSL_ECC_BRAINPOOLP256R1, ssl->heap);
  9975. if (ret != WOLFSSL_SUCCESS) return ret;
  9976. #endif
  9977. #endif
  9978. #endif /* HAVE_ECC */
  9979. #ifndef HAVE_FIPS
  9980. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  9981. ret = TLSX_UseSupportedCurve(extensions,
  9982. WOLFSSL_ECC_X25519, ssl->heap);
  9983. if (ret != WOLFSSL_SUCCESS) return ret;
  9984. #endif
  9985. #endif /* HAVE_FIPS */
  9986. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  9987. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  9988. #ifndef NO_ECC_SECP
  9989. ret = TLSX_UseSupportedCurve(extensions,
  9990. WOLFSSL_ECC_SECP224R1, ssl->heap);
  9991. if (ret != WOLFSSL_SUCCESS) return ret;
  9992. #endif
  9993. #ifdef HAVE_ECC_KOBLITZ
  9994. ret = TLSX_UseSupportedCurve(extensions,
  9995. WOLFSSL_ECC_SECP224K1, ssl->heap);
  9996. if (ret != WOLFSSL_SUCCESS) return ret;
  9997. #endif
  9998. #endif
  9999. #ifndef HAVE_FIPS
  10000. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  10001. #ifndef NO_ECC_SECP
  10002. ret = TLSX_UseSupportedCurve(extensions,
  10003. WOLFSSL_ECC_SECP192R1, ssl->heap);
  10004. if (ret != WOLFSSL_SUCCESS) return ret;
  10005. #endif
  10006. #ifdef HAVE_ECC_KOBLITZ
  10007. ret = TLSX_UseSupportedCurve(extensions,
  10008. WOLFSSL_ECC_SECP192K1, ssl->heap);
  10009. if (ret != WOLFSSL_SUCCESS) return ret;
  10010. #endif
  10011. #endif
  10012. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  10013. #ifndef NO_ECC_SECP
  10014. ret = TLSX_UseSupportedCurve(extensions,
  10015. WOLFSSL_ECC_SECP160R1, ssl->heap);
  10016. if (ret != WOLFSSL_SUCCESS) return ret;
  10017. #endif
  10018. #ifdef HAVE_ECC_SECPR2
  10019. ret = TLSX_UseSupportedCurve(extensions,
  10020. WOLFSSL_ECC_SECP160R2, ssl->heap);
  10021. if (ret != WOLFSSL_SUCCESS) return ret;
  10022. #endif
  10023. #ifdef HAVE_ECC_KOBLITZ
  10024. ret = TLSX_UseSupportedCurve(extensions,
  10025. WOLFSSL_ECC_SECP160K1, ssl->heap);
  10026. if (ret != WOLFSSL_SUCCESS) return ret;
  10027. #endif
  10028. #endif
  10029. #endif /* HAVE_FIPS */
  10030. #endif /* HAVE_ECC */
  10031. #ifndef NO_DH
  10032. /* Add FFDHE supported groups. */
  10033. #ifdef HAVE_FFDHE_8192
  10034. if (8192/8 >= ssl->options.minDhKeySz &&
  10035. 8192/8 <= ssl->options.maxDhKeySz) {
  10036. ret = TLSX_UseSupportedCurve(extensions,
  10037. WOLFSSL_FFDHE_8192, ssl->heap);
  10038. if (ret != WOLFSSL_SUCCESS)
  10039. return ret;
  10040. }
  10041. #endif
  10042. #ifdef HAVE_FFDHE_6144
  10043. if (6144/8 >= ssl->options.minDhKeySz &&
  10044. 6144/8 <= ssl->options.maxDhKeySz) {
  10045. ret = TLSX_UseSupportedCurve(extensions,
  10046. WOLFSSL_FFDHE_6144, ssl->heap);
  10047. if (ret != WOLFSSL_SUCCESS)
  10048. return ret;
  10049. }
  10050. #endif
  10051. #ifdef HAVE_FFDHE_4096
  10052. if (4096/8 >= ssl->options.minDhKeySz &&
  10053. 4096/8 <= ssl->options.maxDhKeySz) {
  10054. ret = TLSX_UseSupportedCurve(extensions,
  10055. WOLFSSL_FFDHE_4096, ssl->heap);
  10056. if (ret != WOLFSSL_SUCCESS)
  10057. return ret;
  10058. }
  10059. #endif
  10060. #ifdef HAVE_FFDHE_3072
  10061. if (3072/8 >= ssl->options.minDhKeySz &&
  10062. 3072/8 <= ssl->options.maxDhKeySz) {
  10063. ret = TLSX_UseSupportedCurve(extensions,
  10064. WOLFSSL_FFDHE_3072, ssl->heap);
  10065. if (ret != WOLFSSL_SUCCESS)
  10066. return ret;
  10067. }
  10068. #endif
  10069. #ifdef HAVE_FFDHE_2048
  10070. if (2048/8 >= ssl->options.minDhKeySz &&
  10071. 2048/8 <= ssl->options.maxDhKeySz) {
  10072. ret = TLSX_UseSupportedCurve(extensions,
  10073. WOLFSSL_FFDHE_2048, ssl->heap);
  10074. if (ret != WOLFSSL_SUCCESS)
  10075. return ret;
  10076. }
  10077. #endif
  10078. #endif
  10079. #ifdef HAVE_PQC
  10080. #ifdef WOLFSSL_WC_KYBER
  10081. #ifdef WOLFSSL_KYBER512
  10082. if (ret == WOLFSSL_SUCCESS)
  10083. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1,
  10084. ssl->heap);
  10085. #endif
  10086. #ifdef WOLFSSL_KYBER768
  10087. if (ret == WOLFSSL_SUCCESS)
  10088. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL3,
  10089. ssl->heap);
  10090. #endif
  10091. #ifdef WOLFSSL_KYBER768
  10092. if (ret == WOLFSSL_SUCCESS)
  10093. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL5,
  10094. ssl->heap);
  10095. #endif
  10096. #elif defined(HAVE_LIBOQS)
  10097. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1, ssl->heap);
  10098. if (ret == WOLFSSL_SUCCESS)
  10099. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL3,
  10100. ssl->heap);
  10101. if (ret == WOLFSSL_SUCCESS)
  10102. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL5,
  10103. ssl->heap);
  10104. if (ret == WOLFSSL_SUCCESS)
  10105. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_NTRU_HPS_LEVEL1,
  10106. ssl->heap);
  10107. if (ret == WOLFSSL_SUCCESS)
  10108. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_NTRU_HPS_LEVEL3,
  10109. ssl->heap);
  10110. if (ret == WOLFSSL_SUCCESS)
  10111. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_NTRU_HPS_LEVEL5,
  10112. ssl->heap);
  10113. if (ret == WOLFSSL_SUCCESS)
  10114. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_NTRU_HRSS_LEVEL3,
  10115. ssl->heap);
  10116. if (ret == WOLFSSL_SUCCESS)
  10117. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_SABER_LEVEL1,
  10118. ssl->heap);
  10119. if (ret == WOLFSSL_SUCCESS)
  10120. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_SABER_LEVEL3,
  10121. ssl->heap);
  10122. if (ret == WOLFSSL_SUCCESS)
  10123. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_SABER_LEVEL5,
  10124. ssl->heap);
  10125. if (ret == WOLFSSL_SUCCESS)
  10126. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_90S_LEVEL1,
  10127. ssl->heap);
  10128. if (ret == WOLFSSL_SUCCESS)
  10129. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_90S_LEVEL3,
  10130. ssl->heap);
  10131. if (ret == WOLFSSL_SUCCESS)
  10132. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_90S_LEVEL5,
  10133. ssl->heap);
  10134. if (ret == WOLFSSL_SUCCESS)
  10135. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P256_NTRU_HPS_LEVEL1,
  10136. ssl->heap);
  10137. if (ret == WOLFSSL_SUCCESS)
  10138. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_NTRU_HPS_LEVEL3,
  10139. ssl->heap);
  10140. if (ret == WOLFSSL_SUCCESS)
  10141. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P521_NTRU_HPS_LEVEL5,
  10142. ssl->heap);
  10143. if (ret == WOLFSSL_SUCCESS)
  10144. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_NTRU_HRSS_LEVEL3,
  10145. ssl->heap);
  10146. if (ret == WOLFSSL_SUCCESS)
  10147. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P256_SABER_LEVEL1,
  10148. ssl->heap);
  10149. if (ret == WOLFSSL_SUCCESS)
  10150. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_SABER_LEVEL3,
  10151. ssl->heap);
  10152. if (ret == WOLFSSL_SUCCESS)
  10153. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P521_SABER_LEVEL5,
  10154. ssl->heap);
  10155. if (ret == WOLFSSL_SUCCESS)
  10156. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P256_KYBER_LEVEL1,
  10157. ssl->heap);
  10158. if (ret == WOLFSSL_SUCCESS)
  10159. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_KYBER_LEVEL3,
  10160. ssl->heap);
  10161. if (ret == WOLFSSL_SUCCESS)
  10162. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P521_KYBER_LEVEL5,
  10163. ssl->heap);
  10164. if (ret == WOLFSSL_SUCCESS)
  10165. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P256_KYBER_90S_LEVEL1,
  10166. ssl->heap);
  10167. if (ret == WOLFSSL_SUCCESS)
  10168. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_KYBER_90S_LEVEL3,
  10169. ssl->heap);
  10170. if (ret == WOLFSSL_SUCCESS)
  10171. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P521_KYBER_90S_LEVEL5,
  10172. ssl->heap);
  10173. #elif defined(HAVE_PQM4)
  10174. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1, ssl->heap);
  10175. #endif /* HAVE_LIBOQS */
  10176. #endif /* HAVE_PQC */
  10177. (void)ssl;
  10178. (void)extensions;
  10179. return ret;
  10180. }
  10181. #endif /* HAVE_SUPPORTED_CURVES */
  10182. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  10183. static const word16 preferredGroup[] = {
  10184. #if defined(HAVE_ECC) && (!defined(NO_ECC256) || \
  10185. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 256
  10186. WOLFSSL_ECC_SECP256R1,
  10187. #endif
  10188. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  10189. WOLFSSL_ECC_X25519,
  10190. #endif
  10191. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  10192. WOLFSSL_ECC_X448,
  10193. #endif
  10194. #if defined(HAVE_ECC) && (!defined(NO_ECC384) || \
  10195. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 384
  10196. WOLFSSL_ECC_SECP384R1,
  10197. #endif
  10198. #if defined(HAVE_ECC) && (!defined(NO_ECC521) || \
  10199. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 521
  10200. WOLFSSL_ECC_SECP521R1,
  10201. #endif
  10202. #if defined(HAVE_FFDHE_2048)
  10203. WOLFSSL_FFDHE_2048,
  10204. #endif
  10205. #if defined(HAVE_FFDHE_3072)
  10206. WOLFSSL_FFDHE_3072,
  10207. #endif
  10208. #if defined(HAVE_FFDHE_4096)
  10209. WOLFSSL_FFDHE_4096,
  10210. #endif
  10211. #if defined(HAVE_FFDHE_6144)
  10212. WOLFSSL_FFDHE_6144,
  10213. #endif
  10214. #if defined(HAVE_FFDHE_8192)
  10215. WOLFSSL_FFDHE_8192,
  10216. #endif
  10217. WOLFSSL_NAMED_GROUP_INVALID
  10218. };
  10219. #endif /* WOLFSSL_TLS13 && HAVE_SUPPORTED_CURVES */
  10220. int TLSX_PopulateExtensions(WOLFSSL* ssl, byte isServer)
  10221. {
  10222. int ret = 0;
  10223. byte* public_key = NULL;
  10224. word16 public_key_len = 0;
  10225. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  10226. int usingPSK = 0;
  10227. #endif
  10228. #if defined(HAVE_SUPPORTED_CURVES) && defined(WOLFSSL_TLS13)
  10229. TLSX* extension = NULL;
  10230. word16 namedGroup = WOLFSSL_NAMED_GROUP_INVALID;
  10231. #endif
  10232. /* server will add extension depending on what is parsed from client */
  10233. if (!isServer) {
  10234. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  10235. if (!ssl->options.disallowEncThenMac) {
  10236. ret = TLSX_EncryptThenMac_Use(ssl);
  10237. if (ret != 0)
  10238. return ret;
  10239. }
  10240. #endif
  10241. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  10242. defined(HAVE_CURVE448)) && defined(HAVE_SUPPORTED_CURVES)
  10243. if (!ssl->options.userCurves && !ssl->ctx->userCurves) {
  10244. if (TLSX_Find(ssl->ctx->extensions,
  10245. TLSX_SUPPORTED_GROUPS) == NULL) {
  10246. ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
  10247. if (ret != WOLFSSL_SUCCESS)
  10248. return ret;
  10249. }
  10250. }
  10251. if ((!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade) &&
  10252. TLSX_Find(ssl->ctx->extensions, TLSX_EC_POINT_FORMATS) == NULL &&
  10253. TLSX_Find(ssl->extensions, TLSX_EC_POINT_FORMATS) == NULL) {
  10254. ret = TLSX_UsePointFormat(&ssl->extensions,
  10255. WOLFSSL_EC_PF_UNCOMPRESSED, ssl->heap);
  10256. if (ret != WOLFSSL_SUCCESS)
  10257. return ret;
  10258. }
  10259. #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
  10260. #ifdef WOLFSSL_SRTP
  10261. if (ssl->options.dtls && ssl->dtlsSrtpProfiles != 0) {
  10262. WOLFSSL_MSG("Adding DTLS SRTP extension");
  10263. if ((ret = TLSX_UseSRTP(&ssl->extensions, ssl->dtlsSrtpProfiles,
  10264. ssl->heap)) != 0) {
  10265. return ret;
  10266. }
  10267. }
  10268. #endif
  10269. } /* is not server */
  10270. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10271. WOLFSSL_MSG("Adding signature algorithms extension");
  10272. if ((ret = TLSX_SetSignatureAlgorithms(&ssl->extensions, ssl, ssl->heap))
  10273. != 0) {
  10274. return ret;
  10275. }
  10276. #else
  10277. ret = 0;
  10278. #endif
  10279. #ifdef WOLFSSL_TLS13
  10280. if (!isServer && IsAtLeastTLSv1_3(ssl->version)) {
  10281. /* Add mandatory TLS v1.3 extension: supported version */
  10282. WOLFSSL_MSG("Adding supported versions extension");
  10283. if ((ret = TLSX_SetSupportedVersions(&ssl->extensions, ssl,
  10284. ssl->heap)) != 0) {
  10285. return ret;
  10286. }
  10287. #if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && \
  10288. !defined(HAVE_CURVE448) && defined(HAVE_SUPPORTED_CURVES)
  10289. if (TLSX_Find(ssl->ctx->extensions, TLSX_SUPPORTED_GROUPS) == NULL) {
  10290. /* Put in DH groups for TLS 1.3 only. */
  10291. ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
  10292. if (ret != WOLFSSL_SUCCESS)
  10293. return ret;
  10294. /* ret value will be overwritten in !NO_PSK case */
  10295. #ifdef NO_PSK
  10296. ret = 0;
  10297. #endif
  10298. }
  10299. #endif /* !(HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
  10300. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10301. if (ssl->certHashSigAlgoSz > 0) {
  10302. WOLFSSL_MSG("Adding signature algorithms cert extension");
  10303. if ((ret = TLSX_SetSignatureAlgorithmsCert(&ssl->extensions,
  10304. ssl, ssl->heap)) != 0) {
  10305. return ret;
  10306. }
  10307. }
  10308. #endif
  10309. #if defined(HAVE_SUPPORTED_CURVES)
  10310. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  10311. if (extension == NULL) {
  10312. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10313. if (ssl->options.resuming && ssl->session->namedGroup != 0)
  10314. namedGroup = ssl->session->namedGroup;
  10315. else
  10316. #endif
  10317. if (ssl->numGroups > 0) {
  10318. int set = 0;
  10319. int i, j;
  10320. /* try to find the highest element in ssl->group[]
  10321. * that is contained in preferredGroup[].
  10322. */
  10323. namedGroup = preferredGroup[0];
  10324. for (i = 0; i < ssl->numGroups && !set; i++) {
  10325. for (j = 0; preferredGroup[j] != WOLFSSL_NAMED_GROUP_INVALID; j++) {
  10326. if (preferredGroup[j] == ssl->group[i]) {
  10327. namedGroup = ssl->group[i];
  10328. set = 1;
  10329. break;
  10330. }
  10331. }
  10332. }
  10333. }
  10334. else {
  10335. /* Choose the most preferred group. */
  10336. namedGroup = preferredGroup[0];
  10337. }
  10338. }
  10339. else {
  10340. KeyShareEntry* kse = (KeyShareEntry*)extension->data;
  10341. if (kse)
  10342. namedGroup = kse->group;
  10343. }
  10344. if (namedGroup > 0) {
  10345. #ifdef HAVE_PQC
  10346. /* For KEMs, the key share has already been generated. */
  10347. if (!WOLFSSL_NAMED_GROUP_IS_PQC(namedGroup))
  10348. #endif
  10349. ret = TLSX_KeyShare_Use(ssl, namedGroup, 0, NULL, NULL);
  10350. if (ret != 0)
  10351. return ret;
  10352. }
  10353. #endif /* HAVE_SUPPORTED_CURVES */
  10354. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10355. TLSX_Remove(&ssl->extensions, TLSX_PRE_SHARED_KEY, ssl->heap);
  10356. #endif
  10357. #if defined(HAVE_SESSION_TICKET)
  10358. if (ssl->options.resuming && ssl->session->ticketLen > 0) {
  10359. WOLFSSL_SESSION* sess = ssl->session;
  10360. #ifdef WOLFSSL_32BIT_MILLI_TIME
  10361. word32 now, milli;
  10362. #else
  10363. word64 now, milli;
  10364. #endif
  10365. if (sess->ticketLen > MAX_PSK_ID_LEN) {
  10366. WOLFSSL_MSG("Session ticket length for PSK ext is too large");
  10367. return BUFFER_ERROR;
  10368. }
  10369. /* Determine the MAC algorithm for the cipher suite used. */
  10370. ssl->options.cipherSuite0 = sess->cipherSuite0;
  10371. ssl->options.cipherSuite = sess->cipherSuite;
  10372. ret = SetCipherSpecs(ssl);
  10373. if (ret != 0)
  10374. return ret;
  10375. now = TimeNowInMilliseconds();
  10376. if (now == 0)
  10377. return GETTIME_ERROR;
  10378. #ifdef WOLFSSL_32BIT_MILLI_TIME
  10379. if (now < sess->ticketSeen)
  10380. milli = (0xFFFFFFFFU - sess->ticketSeen) + 1 + now;
  10381. else
  10382. milli = now - sess->ticketSeen;
  10383. milli += sess->ticketAdd;
  10384. /* Pre-shared key is mandatory extension for resumption. */
  10385. ret = TLSX_PreSharedKey_Use(ssl, sess->ticket, sess->ticketLen,
  10386. milli, ssl->specs.mac_algorithm, ssl->options.cipherSuite0,
  10387. ssl->options.cipherSuite, 1, NULL);
  10388. #else
  10389. milli = now - sess->ticketSeen + sess->ticketAdd;
  10390. /* Pre-shared key is mandatory extension for resumption. */
  10391. ret = TLSX_PreSharedKey_Use(ssl, sess->ticket, sess->ticketLen,
  10392. (word32)milli, ssl->specs.mac_algorithm,
  10393. ssl->options.cipherSuite0, ssl->options.cipherSuite, 1,
  10394. NULL);
  10395. #endif
  10396. if (ret != 0)
  10397. return ret;
  10398. usingPSK = 1;
  10399. }
  10400. #endif
  10401. #ifndef NO_PSK
  10402. #ifndef WOLFSSL_PSK_ONE_ID
  10403. if (ssl->options.client_psk_cs_cb != NULL) {
  10404. int i;
  10405. for (i = 0; i < ssl->suites->suiteSz; i += 2) {
  10406. byte cipherSuite0 = ssl->suites->suites[i + 0];
  10407. byte cipherSuite = ssl->suites->suites[i + 1];
  10408. unsigned int keySz;
  10409. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10410. int cnt = 0;
  10411. #endif
  10412. #ifdef HAVE_NULL_CIPHER
  10413. if (cipherSuite0 == ECC_BYTE ||
  10414. cipherSuite0 == ECDHE_PSK_BYTE) {
  10415. if (cipherSuite != TLS_SHA256_SHA256 &&
  10416. cipherSuite != TLS_SHA384_SHA384) {
  10417. continue;
  10418. }
  10419. }
  10420. else
  10421. #endif
  10422. if (cipherSuite0 != TLS13_BYTE)
  10423. continue;
  10424. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10425. do {
  10426. ssl->arrays->client_identity[0] = cnt;
  10427. #endif
  10428. ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0';
  10429. keySz = ssl->options.client_psk_cs_cb(
  10430. ssl, ssl->arrays->server_hint,
  10431. ssl->arrays->client_identity, MAX_PSK_ID_LEN,
  10432. ssl->arrays->psk_key, MAX_PSK_KEY_LEN,
  10433. GetCipherNameInternal(cipherSuite0, cipherSuite));
  10434. if (keySz > 0) {
  10435. ssl->arrays->psk_keySz = keySz;
  10436. ret = TLSX_PreSharedKey_Use(ssl,
  10437. (byte*)ssl->arrays->client_identity,
  10438. (word16)XSTRLEN(ssl->arrays->client_identity),
  10439. 0, SuiteMac(ssl->suites->suites + i),
  10440. cipherSuite0, cipherSuite, 0, NULL);
  10441. if (ret != 0)
  10442. return ret;
  10443. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10444. cnt++;
  10445. #endif
  10446. }
  10447. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10448. }
  10449. while (keySz > 0);
  10450. #endif
  10451. }
  10452. usingPSK = 1;
  10453. }
  10454. else
  10455. #endif
  10456. if (ssl->options.client_psk_cb != NULL ||
  10457. ssl->options.client_psk_tls13_cb != NULL) {
  10458. /* Default ciphersuite. */
  10459. byte cipherSuite0 = TLS13_BYTE;
  10460. byte cipherSuite = WOLFSSL_DEF_PSK_CIPHER;
  10461. int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE;
  10462. const char* cipherName = NULL;
  10463. if (ssl->options.client_psk_tls13_cb != NULL) {
  10464. ssl->arrays->psk_keySz = ssl->options.client_psk_tls13_cb(
  10465. ssl, ssl->arrays->server_hint,
  10466. ssl->arrays->client_identity, MAX_PSK_ID_LEN,
  10467. ssl->arrays->psk_key, MAX_PSK_KEY_LEN, &cipherName);
  10468. if (GetCipherSuiteFromName(cipherName, &cipherSuite0,
  10469. &cipherSuite, &cipherSuiteFlags) != 0) {
  10470. return PSK_KEY_ERROR;
  10471. }
  10472. }
  10473. else {
  10474. ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl,
  10475. ssl->arrays->server_hint, ssl->arrays->client_identity,
  10476. MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN);
  10477. }
  10478. #if defined(OPENSSL_EXTRA)
  10479. /* OpenSSL treats 0 as a PSK key length of 0
  10480. * and meaning no PSK available.
  10481. */
  10482. if (ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
  10483. return PSK_KEY_ERROR;
  10484. }
  10485. if (ssl->arrays->psk_keySz > 0) {
  10486. #else
  10487. if (ssl->arrays->psk_keySz == 0 ||
  10488. ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
  10489. return PSK_KEY_ERROR;
  10490. }
  10491. #endif
  10492. ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0';
  10493. ssl->options.cipherSuite0 = cipherSuite0;
  10494. ssl->options.cipherSuite = cipherSuite;
  10495. (void)cipherSuiteFlags;
  10496. ret = SetCipherSpecs(ssl);
  10497. if (ret != 0)
  10498. return ret;
  10499. ret = TLSX_PreSharedKey_Use(ssl,
  10500. (byte*)ssl->arrays->client_identity,
  10501. (word16)XSTRLEN(ssl->arrays->client_identity),
  10502. 0, ssl->specs.mac_algorithm,
  10503. cipherSuite0, cipherSuite, 0,
  10504. NULL);
  10505. if (ret != 0)
  10506. return ret;
  10507. usingPSK = 1;
  10508. #if defined(OPENSSL_EXTRA)
  10509. }
  10510. #endif
  10511. }
  10512. #endif /* !NO_PSK */
  10513. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10514. /* Some servers do not generate session tickets unless
  10515. * the extension is seen in a non-resume client hello.
  10516. * We used to send it only if we were otherwise using PSK.
  10517. * Now always send it. Define NO_TLSX_PSKKEM_PLAIN_ANNOUNCE
  10518. * to revert to the old behaviour. */
  10519. #ifdef NO_TLSX_PSKKEM_PLAIN_ANNOUNCE
  10520. if (usingPSK)
  10521. #endif
  10522. {
  10523. byte modes;
  10524. (void)usingPSK;
  10525. /* Pre-shared key modes: mandatory extension for resumption. */
  10526. modes = 1 << PSK_KE;
  10527. #if !defined(NO_DH) || defined(HAVE_ECC) || \
  10528. defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  10529. if (!ssl->options.noPskDheKe)
  10530. modes |= 1 << PSK_DHE_KE;
  10531. #endif
  10532. ret = TLSX_PskKeModes_Use(ssl, modes);
  10533. if (ret != 0)
  10534. return ret;
  10535. }
  10536. #endif
  10537. #if defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  10538. if (!isServer && ssl->options.postHandshakeAuth) {
  10539. ret = TLSX_PostHandAuth_Use(ssl);
  10540. if (ret != 0)
  10541. return ret;
  10542. }
  10543. #endif
  10544. }
  10545. #endif
  10546. (void)isServer;
  10547. (void)public_key;
  10548. (void)public_key_len;
  10549. (void)ssl;
  10550. return ret;
  10551. }
  10552. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  10553. /** Tells the buffered size of extensions to be sent into the client hello. */
  10554. int TLSX_GetRequestSize(WOLFSSL* ssl, byte msgType, word16* pLength)
  10555. {
  10556. int ret = 0;
  10557. word16 length = 0;
  10558. byte semaphore[SEMAPHORE_SIZE] = {0};
  10559. if (!TLSX_SupportExtensions(ssl))
  10560. return 0;
  10561. if (msgType == client_hello) {
  10562. EC_VALIDATE_REQUEST(ssl, semaphore);
  10563. PF_VALIDATE_REQUEST(ssl, semaphore);
  10564. WOLF_STK_VALIDATE_REQUEST(ssl);
  10565. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10566. if (ssl->suites->hashSigAlgoSz == 0)
  10567. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10568. #endif
  10569. #if defined(WOLFSSL_TLS13)
  10570. if (!IsAtLeastTLSv1_2(ssl))
  10571. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10572. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10573. if (!IsAtLeastTLSv1_3(ssl->version)) {
  10574. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10575. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10576. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10577. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
  10578. #endif
  10579. #ifdef WOLFSSL_EARLY_DATA
  10580. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10581. #endif
  10582. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10583. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10584. #endif
  10585. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  10586. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
  10587. #endif
  10588. }
  10589. #endif
  10590. #endif
  10591. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  10592. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  10593. if (!SSL_CM(ssl)->ocspStaplingEnabled) {
  10594. /* mark already sent, so it won't send it */
  10595. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10596. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10597. }
  10598. #endif
  10599. }
  10600. #ifdef WOLFSSL_TLS13
  10601. #ifndef NO_CERTS
  10602. else if (msgType == certificate_request) {
  10603. /* Don't send out any extension except those that are turned off. */
  10604. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10605. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10606. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10607. #endif
  10608. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10609. * TLSX_CERTIFICATE_AUTHORITIES, OID_FILTERS
  10610. * TLSX_STATUS_REQUEST
  10611. */
  10612. }
  10613. #endif
  10614. #endif
  10615. if (ssl->extensions) {
  10616. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
  10617. if (ret != 0)
  10618. return ret;
  10619. }
  10620. if (ssl->ctx && ssl->ctx->extensions) {
  10621. ret = TLSX_GetSize(ssl->ctx->extensions, semaphore, msgType, &length);
  10622. if (ret != 0)
  10623. return ret;
  10624. }
  10625. #ifdef HAVE_EXTENDED_MASTER
  10626. if (msgType == client_hello && ssl->options.haveEMS &&
  10627. (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
  10628. length += HELLO_EXT_SZ;
  10629. }
  10630. #endif
  10631. if (length)
  10632. length += OPAQUE16_LEN; /* for total length storage. */
  10633. *pLength += length;
  10634. return ret;
  10635. }
  10636. /** Writes the extensions to be sent into the client hello. */
  10637. int TLSX_WriteRequest(WOLFSSL* ssl, byte* output, byte msgType, word16* pOffset)
  10638. {
  10639. int ret = 0;
  10640. word16 offset = 0;
  10641. byte semaphore[SEMAPHORE_SIZE] = {0};
  10642. if (!TLSX_SupportExtensions(ssl) || output == NULL)
  10643. return 0;
  10644. offset += OPAQUE16_LEN; /* extensions length */
  10645. if (msgType == client_hello) {
  10646. EC_VALIDATE_REQUEST(ssl, semaphore);
  10647. PF_VALIDATE_REQUEST(ssl, semaphore);
  10648. WOLF_STK_VALIDATE_REQUEST(ssl);
  10649. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10650. if (ssl->suites->hashSigAlgoSz == 0)
  10651. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10652. #endif
  10653. #ifdef WOLFSSL_TLS13
  10654. if (!IsAtLeastTLSv1_2(ssl))
  10655. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10656. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10657. if (!IsAtLeastTLSv1_3(ssl->version)) {
  10658. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10659. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10660. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
  10661. #endif
  10662. #ifdef WOLFSSL_EARLY_DATA
  10663. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10664. #endif
  10665. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10666. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10667. #endif
  10668. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  10669. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
  10670. #endif
  10671. }
  10672. #endif
  10673. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10674. /* Must write Pre-shared Key extension at the end in TLS v1.3.
  10675. * Must not write out Pre-shared Key extension in earlier versions of
  10676. * protocol.
  10677. */
  10678. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10679. #endif
  10680. #endif
  10681. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  10682. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  10683. /* mark already sent, so it won't send it */
  10684. if (!SSL_CM(ssl)->ocspStaplingEnabled) {
  10685. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10686. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10687. }
  10688. #endif
  10689. }
  10690. #ifdef WOLFSSL_TLS13
  10691. #ifndef NO_CERTS
  10692. else if (msgType == certificate_request) {
  10693. /* Don't send out any extension except those that are turned off. */
  10694. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10695. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10696. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10697. #endif
  10698. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10699. * TLSX_CERTIFICATE_AUTHORITIES, TLSX_OID_FILTERS
  10700. * TLSX_STATUS_REQUEST
  10701. */
  10702. }
  10703. #endif
  10704. #endif
  10705. if (ssl->extensions) {
  10706. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10707. msgType, &offset);
  10708. if (ret != 0)
  10709. return ret;
  10710. }
  10711. if (ssl->ctx && ssl->ctx->extensions) {
  10712. ret = TLSX_Write(ssl->ctx->extensions, output + offset, semaphore,
  10713. msgType, &offset);
  10714. if (ret != 0)
  10715. return ret;
  10716. }
  10717. #ifdef HAVE_EXTENDED_MASTER
  10718. if (msgType == client_hello && ssl->options.haveEMS &&
  10719. (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
  10720. WOLFSSL_MSG("EMS extension to write");
  10721. c16toa(HELLO_EXT_EXTMS, output + offset);
  10722. offset += HELLO_EXT_TYPE_SZ;
  10723. c16toa(0, output + offset);
  10724. offset += HELLO_EXT_SZ_SZ;
  10725. }
  10726. #endif
  10727. #ifdef WOLFSSL_TLS13
  10728. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10729. if (msgType == client_hello && IsAtLeastTLSv1_3(ssl->version)) {
  10730. /* Write out what we can of Pre-shared key extension. */
  10731. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10732. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10733. client_hello, &offset);
  10734. if (ret != 0)
  10735. return ret;
  10736. }
  10737. #endif
  10738. #endif
  10739. if (offset > OPAQUE16_LEN || msgType != client_hello)
  10740. c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
  10741. *pOffset += offset;
  10742. return ret;
  10743. }
  10744. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  10745. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_SERVER)
  10746. /** Tells the buffered size of extensions to be sent into the server hello. */
  10747. int TLSX_GetResponseSize(WOLFSSL* ssl, byte msgType, word16* pLength)
  10748. {
  10749. int ret = 0;
  10750. word16 length = 0;
  10751. byte semaphore[SEMAPHORE_SIZE] = {0};
  10752. switch (msgType) {
  10753. #ifndef NO_WOLFSSL_SERVER
  10754. case server_hello:
  10755. PF_VALIDATE_RESPONSE(ssl, semaphore);
  10756. #ifdef WOLFSSL_TLS13
  10757. if (IsAtLeastTLSv1_3(ssl->version)) {
  10758. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10759. TURN_OFF(semaphore,
  10760. TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10761. #ifdef HAVE_SUPPORTED_CURVES
  10762. if (!ssl->options.noPskDheKe)
  10763. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10764. #endif
  10765. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10766. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10767. #endif
  10768. #ifdef WOLFSSL_DTLS_CID
  10769. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10770. #endif /* WOLFSSL_DTLS_CID */
  10771. }
  10772. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10773. else {
  10774. #ifdef HAVE_SUPPORTED_CURVES
  10775. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10776. #endif
  10777. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10778. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10779. #endif
  10780. }
  10781. #endif
  10782. #endif
  10783. break;
  10784. #ifdef WOLFSSL_TLS13
  10785. case hello_retry_request:
  10786. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10787. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10788. #ifdef HAVE_SUPPORTED_CURVES
  10789. if (!ssl->options.noPskDheKe)
  10790. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10791. #endif
  10792. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10793. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10794. #endif
  10795. break;
  10796. #endif
  10797. #ifdef WOLFSSL_TLS13
  10798. case encrypted_extensions:
  10799. /* Send out all extension except those that are turned on. */
  10800. #ifdef HAVE_ECC
  10801. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  10802. #endif
  10803. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10804. #ifdef HAVE_SESSION_TICKET
  10805. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
  10806. #endif
  10807. #ifdef HAVE_SUPPORTED_CURVES
  10808. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10809. #endif
  10810. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10811. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10812. #endif
  10813. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  10814. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10815. #endif
  10816. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  10817. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10818. #endif
  10819. #if defined(HAVE_SERVER_RENEGOTIATION_INFO)
  10820. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
  10821. #endif
  10822. #ifdef WOLFSSL_DTLS_CID
  10823. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10824. #endif /* WOLFSSL_DTLS_CID */
  10825. break;
  10826. #ifdef WOLFSSL_EARLY_DATA
  10827. case session_ticket:
  10828. if (ssl->options.tls1_3) {
  10829. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10830. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10831. }
  10832. break;
  10833. #endif
  10834. #endif
  10835. #endif
  10836. #ifdef WOLFSSL_TLS13
  10837. #ifndef NO_CERTS
  10838. case certificate:
  10839. /* Don't send out any extension except those that are turned off. */
  10840. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10841. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10842. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10843. * TLSX_SERVER_CERTIFICATE_TYPE
  10844. */
  10845. break;
  10846. #endif
  10847. #endif
  10848. }
  10849. #ifdef HAVE_EXTENDED_MASTER
  10850. if (ssl->options.haveEMS && msgType == server_hello &&
  10851. !IsAtLeastTLSv1_3(ssl->version)) {
  10852. length += HELLO_EXT_SZ;
  10853. }
  10854. #endif
  10855. if (TLSX_SupportExtensions(ssl)) {
  10856. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
  10857. if (ret != 0)
  10858. return ret;
  10859. }
  10860. /* All the response data is set at the ssl object only, so no ctx here. */
  10861. if (length || msgType != server_hello)
  10862. length += OPAQUE16_LEN; /* for total length storage. */
  10863. *pLength += length;
  10864. return ret;
  10865. }
  10866. /** Writes the server hello extensions into a buffer. */
  10867. int TLSX_WriteResponse(WOLFSSL *ssl, byte* output, byte msgType, word16* pOffset)
  10868. {
  10869. int ret = 0;
  10870. word16 offset = 0;
  10871. if (TLSX_SupportExtensions(ssl) && output) {
  10872. byte semaphore[SEMAPHORE_SIZE] = {0};
  10873. switch (msgType) {
  10874. #ifndef NO_WOLFSSL_SERVER
  10875. case server_hello:
  10876. PF_VALIDATE_RESPONSE(ssl, semaphore);
  10877. #ifdef WOLFSSL_TLS13
  10878. if (IsAtLeastTLSv1_3(ssl->version)) {
  10879. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10880. TURN_OFF(semaphore,
  10881. TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10882. #ifdef HAVE_SUPPORTED_CURVES
  10883. if (!ssl->options.noPskDheKe)
  10884. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10885. #endif
  10886. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10887. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10888. #endif
  10889. #ifdef WOLFSSL_DTLS_CID
  10890. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10891. #endif /* WOLFSSL_DTLS_CID */
  10892. }
  10893. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10894. else {
  10895. #ifdef HAVE_SUPPORTED_CURVES
  10896. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10897. #endif
  10898. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10899. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10900. #endif
  10901. }
  10902. #endif
  10903. #endif
  10904. break;
  10905. #ifdef WOLFSSL_TLS13
  10906. case hello_retry_request:
  10907. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10908. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10909. #ifdef HAVE_SUPPORTED_CURVES
  10910. if (!ssl->options.noPskDheKe)
  10911. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10912. #endif
  10913. /* Cookie is written below as last extension. */
  10914. break;
  10915. #endif
  10916. #ifdef WOLFSSL_TLS13
  10917. case encrypted_extensions:
  10918. /* Send out all extension except those that are turned on. */
  10919. #ifdef HAVE_ECC
  10920. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  10921. #endif
  10922. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10923. #ifdef HAVE_SESSION_TICKET
  10924. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
  10925. #endif
  10926. #ifdef HAVE_SUPPORTED_CURVES
  10927. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10928. #endif
  10929. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10930. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10931. #endif
  10932. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  10933. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10934. #endif
  10935. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  10936. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10937. #endif
  10938. #if defined(HAVE_SERVER_RENEGOTIATION_INFO)
  10939. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
  10940. #endif
  10941. #ifdef WOLFSSL_DTLS_CID
  10942. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10943. #endif /* WOLFSSL_DTLS_CID */
  10944. break;
  10945. #ifdef WOLFSSL_EARLY_DATA
  10946. case session_ticket:
  10947. if (ssl->options.tls1_3) {
  10948. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10949. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10950. }
  10951. break;
  10952. #endif
  10953. #endif
  10954. #endif
  10955. #ifdef WOLFSSL_TLS13
  10956. #ifndef NO_CERTS
  10957. case certificate:
  10958. /* Don't send out any extension except those that are turned
  10959. * off. */
  10960. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10961. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10962. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10963. * TLSX_SERVER_CERTIFICATE_TYPE
  10964. */
  10965. break;
  10966. #endif
  10967. #endif
  10968. default:
  10969. break;
  10970. }
  10971. offset += OPAQUE16_LEN; /* extensions length */
  10972. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10973. msgType, &offset);
  10974. if (ret != 0)
  10975. return ret;
  10976. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  10977. if (msgType == hello_retry_request) {
  10978. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10979. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10980. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10981. msgType, &offset);
  10982. if (ret != 0)
  10983. return ret;
  10984. }
  10985. #endif
  10986. #ifdef HAVE_EXTENDED_MASTER
  10987. if (ssl->options.haveEMS && msgType == server_hello &&
  10988. !IsAtLeastTLSv1_3(ssl->version)) {
  10989. WOLFSSL_MSG("EMS extension to write");
  10990. c16toa(HELLO_EXT_EXTMS, output + offset);
  10991. offset += HELLO_EXT_TYPE_SZ;
  10992. c16toa(0, output + offset);
  10993. offset += HELLO_EXT_SZ_SZ;
  10994. }
  10995. #endif
  10996. if (offset > OPAQUE16_LEN || msgType != server_hello)
  10997. c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
  10998. }
  10999. if (pOffset)
  11000. *pOffset += offset;
  11001. return ret;
  11002. }
  11003. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_SERVER */
  11004. #ifdef WOLFSSL_TLS13
  11005. int TLSX_ParseVersion(WOLFSSL* ssl, const byte* input, word16 length,
  11006. byte msgType, int* found)
  11007. {
  11008. int ret = 0;
  11009. int offset = 0;
  11010. *found = 0;
  11011. while (offset < (int)length) {
  11012. word16 type;
  11013. word16 size;
  11014. if (offset + (2 * OPAQUE16_LEN) > length) {
  11015. ret = BUFFER_ERROR;
  11016. break;
  11017. }
  11018. ato16(input + offset, &type);
  11019. offset += HELLO_EXT_TYPE_SZ;
  11020. ato16(input + offset, &size);
  11021. offset += OPAQUE16_LEN;
  11022. if (offset + size > length) {
  11023. ret = BUFFER_ERROR;
  11024. break;
  11025. }
  11026. if (type == TLSX_SUPPORTED_VERSIONS) {
  11027. *found = 1;
  11028. WOLFSSL_MSG("Supported Versions extension received");
  11029. ret = SV_PARSE(ssl, input + offset, size, msgType);
  11030. break;
  11031. }
  11032. offset += size;
  11033. }
  11034. return ret;
  11035. }
  11036. #endif
  11037. /** Parses a buffer of TLS extensions. */
  11038. int TLSX_Parse(WOLFSSL* ssl, const byte* input, word16 length, byte msgType,
  11039. Suites *suites)
  11040. {
  11041. int ret = 0;
  11042. word16 offset = 0;
  11043. byte isRequest = (msgType == client_hello ||
  11044. msgType == certificate_request);
  11045. #ifdef HAVE_EXTENDED_MASTER
  11046. byte pendingEMS = 0;
  11047. #endif
  11048. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  11049. int pskDone = 0;
  11050. #endif
  11051. if (!ssl || !input || (isRequest && !suites))
  11052. return BAD_FUNC_ARG;
  11053. while (ret == 0 && offset < length) {
  11054. word16 type;
  11055. word16 size;
  11056. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  11057. if (msgType == client_hello && pskDone) {
  11058. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  11059. return PSK_KEY_ERROR;
  11060. }
  11061. #endif
  11062. if (length - offset < HELLO_EXT_TYPE_SZ + OPAQUE16_LEN)
  11063. return BUFFER_ERROR;
  11064. ato16(input + offset, &type);
  11065. offset += HELLO_EXT_TYPE_SZ;
  11066. ato16(input + offset, &size);
  11067. offset += OPAQUE16_LEN;
  11068. if (length - offset < size)
  11069. return BUFFER_ERROR;
  11070. switch (type) {
  11071. #ifdef HAVE_SNI
  11072. case TLSX_SERVER_NAME:
  11073. WOLFSSL_MSG("SNI extension received");
  11074. #ifdef WOLFSSL_DEBUG_TLS
  11075. WOLFSSL_BUFFER(input + offset, size);
  11076. #endif
  11077. #ifdef WOLFSSL_TLS13
  11078. if (IsAtLeastTLSv1_3(ssl->version)) {
  11079. if (msgType != client_hello &&
  11080. msgType != encrypted_extensions)
  11081. return EXT_NOT_ALLOWED;
  11082. }
  11083. else
  11084. #endif
  11085. {
  11086. if (msgType != client_hello &&
  11087. msgType != server_hello)
  11088. return EXT_NOT_ALLOWED;
  11089. }
  11090. ret = SNI_PARSE(ssl, input + offset, size, isRequest);
  11091. break;
  11092. #endif
  11093. case TLSX_TRUSTED_CA_KEYS:
  11094. WOLFSSL_MSG("Trusted CA extension received");
  11095. #ifdef WOLFSSL_DEBUG_TLS
  11096. WOLFSSL_BUFFER(input + offset, size);
  11097. #endif
  11098. #ifdef WOLFSSL_TLS13
  11099. if (IsAtLeastTLSv1_3(ssl->version)) {
  11100. if (msgType != client_hello &&
  11101. msgType != encrypted_extensions)
  11102. return EXT_NOT_ALLOWED;
  11103. }
  11104. else
  11105. #endif
  11106. {
  11107. if (msgType != client_hello)
  11108. return EXT_NOT_ALLOWED;
  11109. }
  11110. ret = TCA_PARSE(ssl, input + offset, size, isRequest);
  11111. break;
  11112. case TLSX_MAX_FRAGMENT_LENGTH:
  11113. WOLFSSL_MSG("Max Fragment Length extension received");
  11114. #ifdef WOLFSSL_DEBUG_TLS
  11115. WOLFSSL_BUFFER(input + offset, size);
  11116. #endif
  11117. #ifdef WOLFSSL_TLS13
  11118. if (IsAtLeastTLSv1_3(ssl->version)) {
  11119. if (msgType != client_hello &&
  11120. msgType != encrypted_extensions) {
  11121. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11122. return EXT_NOT_ALLOWED;
  11123. }
  11124. }
  11125. else
  11126. #endif
  11127. {
  11128. if (msgType != client_hello &&
  11129. msgType != server_hello) {
  11130. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11131. return EXT_NOT_ALLOWED;
  11132. }
  11133. }
  11134. ret = MFL_PARSE(ssl, input + offset, size, isRequest);
  11135. break;
  11136. case TLSX_TRUNCATED_HMAC:
  11137. WOLFSSL_MSG("Truncated HMAC extension received");
  11138. #ifdef WOLFSSL_DEBUG_TLS
  11139. WOLFSSL_BUFFER(input + offset, size);
  11140. #endif
  11141. #ifdef WOLFSSL_TLS13
  11142. if (IsAtLeastTLSv1_3(ssl->version))
  11143. break;
  11144. #endif
  11145. if (msgType != client_hello)
  11146. return EXT_NOT_ALLOWED;
  11147. ret = THM_PARSE(ssl, input + offset, size, isRequest);
  11148. break;
  11149. case TLSX_SUPPORTED_GROUPS:
  11150. WOLFSSL_MSG("Supported Groups extension received");
  11151. #ifdef WOLFSSL_DEBUG_TLS
  11152. WOLFSSL_BUFFER(input + offset, size);
  11153. #endif
  11154. #ifdef WOLFSSL_TLS13
  11155. if (IsAtLeastTLSv1_3(ssl->version)) {
  11156. if (msgType != client_hello &&
  11157. msgType != encrypted_extensions) {
  11158. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11159. return EXT_NOT_ALLOWED;
  11160. }
  11161. }
  11162. else
  11163. #endif
  11164. {
  11165. if (msgType != client_hello) {
  11166. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11167. return EXT_NOT_ALLOWED;
  11168. }
  11169. }
  11170. ret = EC_PARSE(ssl, input + offset, size, isRequest);
  11171. break;
  11172. case TLSX_EC_POINT_FORMATS:
  11173. WOLFSSL_MSG("Point Formats extension received");
  11174. #ifdef WOLFSSL_DEBUG_TLS
  11175. WOLFSSL_BUFFER(input + offset, size);
  11176. #endif
  11177. #ifdef WOLFSSL_TLS13
  11178. if (IsAtLeastTLSv1_3(ssl->version))
  11179. break;
  11180. #endif
  11181. if (msgType != client_hello &&
  11182. msgType != server_hello) {
  11183. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11184. return EXT_NOT_ALLOWED;
  11185. }
  11186. ret = PF_PARSE(ssl, input + offset, size, isRequest);
  11187. break;
  11188. case TLSX_STATUS_REQUEST:
  11189. WOLFSSL_MSG("Certificate Status Request extension received");
  11190. #ifdef WOLFSSL_DEBUG_TLS
  11191. WOLFSSL_BUFFER(input + offset, size);
  11192. #endif
  11193. #ifdef WOLFSSL_TLS13
  11194. if (IsAtLeastTLSv1_3(ssl->version)) {
  11195. if (msgType != client_hello &&
  11196. msgType != certificate_request &&
  11197. msgType != certificate)
  11198. return EXT_NOT_ALLOWED;
  11199. }
  11200. else
  11201. #endif
  11202. {
  11203. if (msgType != client_hello &&
  11204. msgType != server_hello)
  11205. return EXT_NOT_ALLOWED;
  11206. }
  11207. ret = CSR_PARSE(ssl, input + offset, size, isRequest);
  11208. break;
  11209. case TLSX_STATUS_REQUEST_V2:
  11210. WOLFSSL_MSG("Certificate Status Request v2 extension received");
  11211. #ifdef WOLFSSL_DEBUG_TLS
  11212. WOLFSSL_BUFFER(input + offset, size);
  11213. #endif
  11214. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  11215. if (IsAtLeastTLSv1_3(ssl->version)) {
  11216. if (msgType != client_hello &&
  11217. msgType != certificate_request &&
  11218. msgType != certificate)
  11219. return EXT_NOT_ALLOWED;
  11220. }
  11221. else
  11222. #endif
  11223. {
  11224. if (msgType != client_hello &&
  11225. msgType != server_hello)
  11226. return EXT_NOT_ALLOWED;
  11227. }
  11228. ret = CSR2_PARSE(ssl, input + offset, size, isRequest);
  11229. break;
  11230. #ifdef HAVE_EXTENDED_MASTER
  11231. case HELLO_EXT_EXTMS:
  11232. WOLFSSL_MSG("Extended Master Secret extension received");
  11233. #ifdef WOLFSSL_DEBUG_TLS
  11234. WOLFSSL_BUFFER(input + offset, size);
  11235. #endif
  11236. #if defined(WOLFSSL_TLS13)
  11237. if (IsAtLeastTLSv1_3(ssl->version))
  11238. break;
  11239. #endif
  11240. if (msgType != client_hello &&
  11241. msgType != server_hello)
  11242. return EXT_NOT_ALLOWED;
  11243. if (size != 0)
  11244. return BUFFER_ERROR;
  11245. #ifndef NO_WOLFSSL_SERVER
  11246. if (isRequest)
  11247. ssl->options.haveEMS = 1;
  11248. #endif
  11249. pendingEMS = 1;
  11250. break;
  11251. #endif
  11252. case TLSX_RENEGOTIATION_INFO:
  11253. WOLFSSL_MSG("Secure Renegotiation extension received");
  11254. #ifdef WOLFSSL_DEBUG_TLS
  11255. WOLFSSL_BUFFER(input + offset, size);
  11256. #endif
  11257. #ifdef WOLFSSL_TLS13
  11258. if (IsAtLeastTLSv1_3(ssl->version))
  11259. break;
  11260. #endif
  11261. if (msgType != client_hello &&
  11262. msgType != server_hello)
  11263. return EXT_NOT_ALLOWED;
  11264. ret = SCR_PARSE(ssl, input + offset, size, isRequest);
  11265. break;
  11266. case TLSX_SESSION_TICKET:
  11267. WOLFSSL_MSG("Session Ticket extension received");
  11268. #ifdef WOLFSSL_DEBUG_TLS
  11269. WOLFSSL_BUFFER(input + offset, size);
  11270. #endif
  11271. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  11272. if (IsAtLeastTLSv1_3(ssl->version)) {
  11273. if (msgType != client_hello)
  11274. return EXT_NOT_ALLOWED;
  11275. }
  11276. else
  11277. #endif
  11278. {
  11279. if (msgType != client_hello &&
  11280. msgType != server_hello)
  11281. return EXT_NOT_ALLOWED;
  11282. }
  11283. ret = WOLF_STK_PARSE(ssl, input + offset, size, isRequest);
  11284. break;
  11285. case TLSX_APPLICATION_LAYER_PROTOCOL:
  11286. WOLFSSL_MSG("ALPN extension received");
  11287. #ifdef WOLFSSL_DEBUG_TLS
  11288. WOLFSSL_BUFFER(input + offset, size);
  11289. #endif
  11290. #if defined(WOLFSSL_TLS13) && defined(HAVE_ALPN)
  11291. if (IsAtLeastTLSv1_3(ssl->version)) {
  11292. if (msgType != client_hello &&
  11293. msgType != encrypted_extensions)
  11294. return EXT_NOT_ALLOWED;
  11295. }
  11296. else
  11297. #endif
  11298. {
  11299. if (msgType != client_hello &&
  11300. msgType != server_hello)
  11301. return EXT_NOT_ALLOWED;
  11302. }
  11303. ret = ALPN_PARSE(ssl, input + offset, size, isRequest);
  11304. break;
  11305. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  11306. case TLSX_SIGNATURE_ALGORITHMS:
  11307. WOLFSSL_MSG("Signature Algorithms extension received");
  11308. #ifdef WOLFSSL_DEBUG_TLS
  11309. WOLFSSL_BUFFER(input + offset, size);
  11310. #endif
  11311. if (!IsAtLeastTLSv1_2(ssl))
  11312. break;
  11313. #ifdef WOLFSSL_TLS13
  11314. if (IsAtLeastTLSv1_3(ssl->version)) {
  11315. if (msgType != client_hello &&
  11316. msgType != certificate_request)
  11317. return EXT_NOT_ALLOWED;
  11318. }
  11319. else
  11320. #endif
  11321. {
  11322. if (msgType != client_hello)
  11323. return EXT_NOT_ALLOWED;
  11324. }
  11325. ret = SA_PARSE(ssl, input + offset, size, isRequest, suites);
  11326. break;
  11327. #endif
  11328. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  11329. case TLSX_ENCRYPT_THEN_MAC:
  11330. WOLFSSL_MSG("Encrypt-Then-Mac extension received");
  11331. /* Ignore for TLS 1.3+ */
  11332. if (IsAtLeastTLSv1_3(ssl->version))
  11333. break;
  11334. if (msgType != client_hello &&
  11335. msgType != server_hello)
  11336. return EXT_NOT_ALLOWED;
  11337. ret = ETM_PARSE(ssl, input + offset, size, msgType);
  11338. break;
  11339. #endif /* HAVE_ENCRYPT_THEN_MAC */
  11340. #ifdef WOLFSSL_TLS13
  11341. case TLSX_SUPPORTED_VERSIONS:
  11342. WOLFSSL_MSG("Skipping Supported Versions - already processed");
  11343. #ifdef WOLFSSL_DEBUG_TLS
  11344. WOLFSSL_BUFFER(input + offset, size);
  11345. #endif
  11346. if (msgType != client_hello &&
  11347. msgType != server_hello &&
  11348. msgType != hello_retry_request)
  11349. return EXT_NOT_ALLOWED;
  11350. break;
  11351. #ifdef WOLFSSL_SEND_HRR_COOKIE
  11352. case TLSX_COOKIE:
  11353. WOLFSSL_MSG("Cookie extension received");
  11354. #ifdef WOLFSSL_DEBUG_TLS
  11355. WOLFSSL_BUFFER(input + offset, size);
  11356. #endif
  11357. if (!IsAtLeastTLSv1_3(ssl->version))
  11358. break;
  11359. if (msgType != client_hello &&
  11360. msgType != hello_retry_request) {
  11361. return EXT_NOT_ALLOWED;
  11362. }
  11363. ret = CKE_PARSE(ssl, input + offset, size, msgType);
  11364. break;
  11365. #endif
  11366. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11367. case TLSX_PRE_SHARED_KEY:
  11368. WOLFSSL_MSG("Pre-Shared Key extension received");
  11369. #ifdef WOLFSSL_DEBUG_TLS
  11370. WOLFSSL_BUFFER(input + offset, size);
  11371. #endif
  11372. if (!IsAtLeastTLSv1_3(ssl->version))
  11373. break;
  11374. if (msgType != client_hello &&
  11375. msgType != server_hello) {
  11376. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11377. return EXT_NOT_ALLOWED;
  11378. }
  11379. ret = PSK_PARSE(ssl, input + offset, size, msgType);
  11380. pskDone = 1;
  11381. break;
  11382. case TLSX_PSK_KEY_EXCHANGE_MODES:
  11383. WOLFSSL_MSG("PSK Key Exchange Modes extension received");
  11384. #ifdef WOLFSSL_DEBUG_TLS
  11385. WOLFSSL_BUFFER(input + offset, size);
  11386. #endif
  11387. if (!IsAtLeastTLSv1_3(ssl->version))
  11388. break;
  11389. if (msgType != client_hello) {
  11390. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11391. return EXT_NOT_ALLOWED;
  11392. }
  11393. ret = PKM_PARSE(ssl, input + offset, size, msgType);
  11394. break;
  11395. #endif
  11396. #ifdef WOLFSSL_EARLY_DATA
  11397. case TLSX_EARLY_DATA:
  11398. WOLFSSL_MSG("Early Data extension received");
  11399. #ifdef WOLFSSL_DEBUG_TLS
  11400. WOLFSSL_BUFFER(input + offset, size);
  11401. #endif
  11402. if (!IsAtLeastTLSv1_3(ssl->version))
  11403. break;
  11404. if (msgType != client_hello && msgType != session_ticket &&
  11405. msgType != encrypted_extensions) {
  11406. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11407. return EXT_NOT_ALLOWED;
  11408. }
  11409. ret = EDI_PARSE(ssl, input + offset, size, msgType);
  11410. break;
  11411. #endif
  11412. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  11413. case TLSX_POST_HANDSHAKE_AUTH:
  11414. WOLFSSL_MSG("Post Handshake Authentication extension received");
  11415. #ifdef WOLFSSL_DEBUG_TLS
  11416. WOLFSSL_BUFFER(input + offset, size);
  11417. #endif
  11418. if (!IsAtLeastTLSv1_3(ssl->version))
  11419. break;
  11420. if (msgType != client_hello) {
  11421. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11422. return EXT_NOT_ALLOWED;
  11423. }
  11424. ret = PHA_PARSE(ssl, input + offset, size, msgType);
  11425. break;
  11426. #endif
  11427. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  11428. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  11429. WOLFSSL_MSG("Signature Algorithms extension received");
  11430. #ifdef WOLFSSL_DEBUG_TLS
  11431. WOLFSSL_BUFFER(input + offset, size);
  11432. #endif
  11433. if (!IsAtLeastTLSv1_3(ssl->version))
  11434. break;
  11435. if (msgType != client_hello &&
  11436. msgType != certificate_request) {
  11437. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11438. return EXT_NOT_ALLOWED;
  11439. }
  11440. ret = SAC_PARSE(ssl, input + offset, size, isRequest);
  11441. break;
  11442. #endif
  11443. case TLSX_KEY_SHARE:
  11444. WOLFSSL_MSG("Key Share extension received");
  11445. #ifdef WOLFSSL_DEBUG_TLS
  11446. WOLFSSL_BUFFER(input + offset, size);
  11447. #endif
  11448. #ifdef HAVE_SUPPORTED_CURVES
  11449. if (!IsAtLeastTLSv1_3(ssl->version))
  11450. break;
  11451. if (msgType != client_hello && msgType != server_hello &&
  11452. msgType != hello_retry_request) {
  11453. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11454. return EXT_NOT_ALLOWED;
  11455. }
  11456. #endif
  11457. ret = KS_PARSE(ssl, input + offset, size, msgType);
  11458. break;
  11459. #endif
  11460. #ifdef WOLFSSL_SRTP
  11461. case TLSX_USE_SRTP:
  11462. WOLFSSL_MSG("Use SRTP extension received");
  11463. ret = SRTP_PARSE(ssl, input + offset, size, isRequest);
  11464. break;
  11465. #endif
  11466. #ifdef WOLFSSL_QUIC
  11467. case TLSX_KEY_QUIC_TP_PARAMS:
  11468. FALL_THROUGH;
  11469. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  11470. WOLFSSL_MSG("QUIC transport parameter received");
  11471. #ifdef WOLFSSL_DEBUG_TLS
  11472. WOLFSSL_BUFFER(input + offset, size);
  11473. #endif
  11474. if (IsAtLeastTLSv1_3(ssl->version) &&
  11475. msgType != client_hello &&
  11476. msgType != server_hello &&
  11477. msgType != encrypted_extensions) {
  11478. return EXT_NOT_ALLOWED;
  11479. }
  11480. else if (!IsAtLeastTLSv1_3(ssl->version) &&
  11481. msgType == encrypted_extensions) {
  11482. return EXT_NOT_ALLOWED;
  11483. }
  11484. else if (WOLFSSL_IS_QUIC(ssl)) {
  11485. ret = QTP_PARSE(ssl, input + offset, size, type, msgType);
  11486. }
  11487. else {
  11488. WOLFSSL_MSG("QUIC transport param TLS extension type, but no QUIC");
  11489. return EXT_NOT_ALLOWED; /* be safe, this should not happen */
  11490. }
  11491. break;
  11492. #endif /* WOLFSSL_QUIC */
  11493. #if defined(WOLFSSL_DTLS_CID)
  11494. case TLSX_CONNECTION_ID:
  11495. /* connection ID not supported in DTLSv1.2 */
  11496. if (!IsAtLeastTLSv1_3(ssl->version))
  11497. break;
  11498. if (msgType != client_hello && msgType != server_hello)
  11499. return EXT_NOT_ALLOWED;
  11500. WOLFSSL_MSG("ConnectionID extension received");
  11501. ret = CID_PARSE(ssl, input + offset, size, isRequest);
  11502. break;
  11503. #endif /* defined(WOLFSSL_DTLS_CID) */
  11504. default:
  11505. WOLFSSL_MSG("Unknown TLS extension type");
  11506. }
  11507. /* offset should be updated here! */
  11508. offset += size;
  11509. }
  11510. #ifdef HAVE_EXTENDED_MASTER
  11511. if (IsAtLeastTLSv1_3(ssl->version) && msgType == hello_retry_request) {
  11512. /* Don't change EMS status until server_hello received.
  11513. * Second ClientHello must have same extensions.
  11514. */
  11515. }
  11516. else if (!isRequest && ssl->options.haveEMS && !pendingEMS)
  11517. ssl->options.haveEMS = 0;
  11518. #endif
  11519. if (ret == 0)
  11520. ret = SNI_VERIFY_PARSE(ssl, isRequest);
  11521. if (ret == 0)
  11522. ret = TCA_VERIFY_PARSE(ssl, isRequest);
  11523. return ret;
  11524. }
  11525. /* undefining semaphore macros */
  11526. #undef IS_OFF
  11527. #undef TURN_ON
  11528. #undef SEMAPHORE_SIZE
  11529. #endif /* HAVE_TLS_EXTENSIONS */
  11530. #ifndef NO_WOLFSSL_CLIENT
  11531. WOLFSSL_METHOD* wolfTLS_client_method(void)
  11532. {
  11533. return wolfTLS_client_method_ex(NULL);
  11534. }
  11535. WOLFSSL_METHOD* wolfTLS_client_method_ex(void* heap)
  11536. {
  11537. WOLFSSL_METHOD* method =
  11538. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11539. heap, DYNAMIC_TYPE_METHOD);
  11540. (void)heap;
  11541. WOLFSSL_ENTER("TLS_client_method_ex");
  11542. if (method) {
  11543. #if defined(WOLFSSL_TLS13)
  11544. InitSSL_Method(method, MakeTLSv1_3());
  11545. #elif !defined(WOLFSSL_NO_TLS12)
  11546. InitSSL_Method(method, MakeTLSv1_2());
  11547. #elif !defined(NO_OLD_TLS)
  11548. InitSSL_Method(method, MakeTLSv1_1());
  11549. #elif defined(WOLFSSL_ALLOW_TLSV10)
  11550. InitSSL_Method(method, MakeTLSv1());
  11551. #else
  11552. #error No TLS version enabled!
  11553. #endif
  11554. method->downgrade = 1;
  11555. method->side = WOLFSSL_CLIENT_END;
  11556. }
  11557. return method;
  11558. }
  11559. #ifndef NO_OLD_TLS
  11560. #ifdef WOLFSSL_ALLOW_TLSV10
  11561. WOLFSSL_METHOD* wolfTLSv1_client_method(void)
  11562. {
  11563. return wolfTLSv1_client_method_ex(NULL);
  11564. }
  11565. WOLFSSL_METHOD* wolfTLSv1_client_method_ex(void* heap)
  11566. {
  11567. WOLFSSL_METHOD* method =
  11568. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11569. heap, DYNAMIC_TYPE_METHOD);
  11570. (void)heap;
  11571. WOLFSSL_ENTER("TLSv1_client_method_ex");
  11572. if (method)
  11573. InitSSL_Method(method, MakeTLSv1());
  11574. return method;
  11575. }
  11576. #endif /* WOLFSSL_ALLOW_TLSV10 */
  11577. WOLFSSL_METHOD* wolfTLSv1_1_client_method(void)
  11578. {
  11579. return wolfTLSv1_1_client_method_ex(NULL);
  11580. }
  11581. WOLFSSL_METHOD* wolfTLSv1_1_client_method_ex(void* heap)
  11582. {
  11583. WOLFSSL_METHOD* method =
  11584. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11585. heap, DYNAMIC_TYPE_METHOD);
  11586. (void)heap;
  11587. WOLFSSL_ENTER("TLSv1_1_client_method_ex");
  11588. if (method)
  11589. InitSSL_Method(method, MakeTLSv1_1());
  11590. return method;
  11591. }
  11592. #endif /* !NO_OLD_TLS */
  11593. #ifndef WOLFSSL_NO_TLS12
  11594. WOLFSSL_ABI
  11595. WOLFSSL_METHOD* wolfTLSv1_2_client_method(void)
  11596. {
  11597. return wolfTLSv1_2_client_method_ex(NULL);
  11598. }
  11599. WOLFSSL_METHOD* wolfTLSv1_2_client_method_ex(void* heap)
  11600. {
  11601. WOLFSSL_METHOD* method =
  11602. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11603. heap, DYNAMIC_TYPE_METHOD);
  11604. (void)heap;
  11605. WOLFSSL_ENTER("TLSv1_2_client_method_ex");
  11606. if (method)
  11607. InitSSL_Method(method, MakeTLSv1_2());
  11608. return method;
  11609. }
  11610. #endif /* WOLFSSL_NO_TLS12 */
  11611. #ifdef WOLFSSL_TLS13
  11612. /* The TLS v1.3 client method data.
  11613. *
  11614. * returns the method data for a TLS v1.3 client.
  11615. */
  11616. WOLFSSL_ABI
  11617. WOLFSSL_METHOD* wolfTLSv1_3_client_method(void)
  11618. {
  11619. return wolfTLSv1_3_client_method_ex(NULL);
  11620. }
  11621. /* The TLS v1.3 client method data.
  11622. *
  11623. * heap The heap used for allocation.
  11624. * returns the method data for a TLS v1.3 client.
  11625. */
  11626. WOLFSSL_METHOD* wolfTLSv1_3_client_method_ex(void* heap)
  11627. {
  11628. WOLFSSL_METHOD* method = (WOLFSSL_METHOD*)
  11629. XMALLOC(sizeof(WOLFSSL_METHOD), heap,
  11630. DYNAMIC_TYPE_METHOD);
  11631. (void)heap;
  11632. WOLFSSL_ENTER("TLSv1_3_client_method_ex");
  11633. if (method)
  11634. InitSSL_Method(method, MakeTLSv1_3());
  11635. return method;
  11636. }
  11637. #endif /* WOLFSSL_TLS13 */
  11638. #ifdef WOLFSSL_DTLS
  11639. WOLFSSL_METHOD* wolfDTLS_client_method(void)
  11640. {
  11641. return wolfDTLS_client_method_ex(NULL);
  11642. }
  11643. WOLFSSL_METHOD* wolfDTLS_client_method_ex(void* heap)
  11644. {
  11645. WOLFSSL_METHOD* method =
  11646. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11647. heap, DYNAMIC_TYPE_METHOD);
  11648. (void)heap;
  11649. WOLFSSL_ENTER("DTLS_client_method_ex");
  11650. if (method) {
  11651. #if defined(WOLFSSL_DTLS13)
  11652. InitSSL_Method(method, MakeDTLSv1_3());
  11653. #elif !defined(WOLFSSL_NO_TLS12)
  11654. InitSSL_Method(method, MakeDTLSv1_2());
  11655. #elif !defined(NO_OLD_TLS)
  11656. InitSSL_Method(method, MakeDTLSv1());
  11657. #else
  11658. #error No DTLS version enabled!
  11659. #endif
  11660. method->downgrade = 1;
  11661. method->side = WOLFSSL_CLIENT_END;
  11662. }
  11663. return method;
  11664. }
  11665. #ifndef NO_OLD_TLS
  11666. WOLFSSL_METHOD* wolfDTLSv1_client_method(void)
  11667. {
  11668. return wolfDTLSv1_client_method_ex(NULL);
  11669. }
  11670. WOLFSSL_METHOD* wolfDTLSv1_client_method_ex(void* heap)
  11671. {
  11672. WOLFSSL_METHOD* method =
  11673. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11674. heap, DYNAMIC_TYPE_METHOD);
  11675. (void)heap;
  11676. WOLFSSL_ENTER("DTLSv1_client_method_ex");
  11677. if (method)
  11678. InitSSL_Method(method, MakeDTLSv1());
  11679. return method;
  11680. }
  11681. #endif /* NO_OLD_TLS */
  11682. #ifndef WOLFSSL_NO_TLS12
  11683. WOLFSSL_METHOD* wolfDTLSv1_2_client_method(void)
  11684. {
  11685. return wolfDTLSv1_2_client_method_ex(NULL);
  11686. }
  11687. WOLFSSL_METHOD* wolfDTLSv1_2_client_method_ex(void* heap)
  11688. {
  11689. WOLFSSL_METHOD* method =
  11690. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11691. heap, DYNAMIC_TYPE_METHOD);
  11692. (void)heap;
  11693. WOLFSSL_ENTER("DTLSv1_2_client_method_ex");
  11694. if (method)
  11695. InitSSL_Method(method, MakeDTLSv1_2());
  11696. (void)heap;
  11697. return method;
  11698. }
  11699. #endif /* !WOLFSSL_NO_TLS12 */
  11700. #endif /* WOLFSSL_DTLS */
  11701. #endif /* NO_WOLFSSL_CLIENT */
  11702. /* EITHER SIDE METHODS */
  11703. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  11704. #ifndef NO_OLD_TLS
  11705. #ifdef WOLFSSL_ALLOW_TLSV10
  11706. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11707. *
  11708. * Returns a pointer to a WOLFSSL_METHOD struct
  11709. */
  11710. WOLFSSL_METHOD* wolfTLSv1_method(void)
  11711. {
  11712. return wolfTLSv1_method_ex(NULL);
  11713. }
  11714. WOLFSSL_METHOD* wolfTLSv1_method_ex(void* heap)
  11715. {
  11716. WOLFSSL_METHOD* m;
  11717. WOLFSSL_ENTER("TLSv1_method");
  11718. #ifndef NO_WOLFSSL_CLIENT
  11719. m = wolfTLSv1_client_method_ex(heap);
  11720. #else
  11721. m = wolfTLSv1_server_method_ex(heap);
  11722. #endif
  11723. if (m != NULL) {
  11724. m->side = WOLFSSL_NEITHER_END;
  11725. }
  11726. return m;
  11727. }
  11728. #endif /* WOLFSSL_ALLOW_TLSV10 */
  11729. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11730. *
  11731. * Returns a pointer to a WOLFSSL_METHOD struct
  11732. */
  11733. WOLFSSL_METHOD* wolfTLSv1_1_method(void)
  11734. {
  11735. return wolfTLSv1_1_method_ex(NULL);
  11736. }
  11737. WOLFSSL_METHOD* wolfTLSv1_1_method_ex(void* heap)
  11738. {
  11739. WOLFSSL_METHOD* m;
  11740. WOLFSSL_ENTER("TLSv1_1_method");
  11741. #ifndef NO_WOLFSSL_CLIENT
  11742. m = wolfTLSv1_1_client_method_ex(heap);
  11743. #else
  11744. m = wolfTLSv1_1_server_method_ex(heap);
  11745. #endif
  11746. if (m != NULL) {
  11747. m->side = WOLFSSL_NEITHER_END;
  11748. }
  11749. return m;
  11750. }
  11751. #endif /* !NO_OLD_TLS */
  11752. #ifndef WOLFSSL_NO_TLS12
  11753. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11754. *
  11755. * Returns a pointer to a WOLFSSL_METHOD struct
  11756. */
  11757. WOLFSSL_METHOD* wolfTLSv1_2_method(void)
  11758. {
  11759. return wolfTLSv1_2_method_ex(NULL);
  11760. }
  11761. WOLFSSL_METHOD* wolfTLSv1_2_method_ex(void* heap)
  11762. {
  11763. WOLFSSL_METHOD* m;
  11764. WOLFSSL_ENTER("TLSv1_2_method");
  11765. #ifndef NO_WOLFSSL_CLIENT
  11766. m = wolfTLSv1_2_client_method_ex(heap);
  11767. #else
  11768. m = wolfTLSv1_2_server_method_ex(heap);
  11769. #endif
  11770. if (m != NULL) {
  11771. m->side = WOLFSSL_NEITHER_END;
  11772. }
  11773. return m;
  11774. }
  11775. #endif /* !WOLFSSL_NO_TLS12 */
  11776. #ifdef WOLFSSL_TLS13
  11777. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11778. *
  11779. * Returns a pointer to a WOLFSSL_METHOD struct
  11780. */
  11781. WOLFSSL_METHOD* wolfTLSv1_3_method(void)
  11782. {
  11783. return wolfTLSv1_3_method_ex(NULL);
  11784. }
  11785. WOLFSSL_METHOD* wolfTLSv1_3_method_ex(void* heap)
  11786. {
  11787. WOLFSSL_METHOD* m;
  11788. WOLFSSL_ENTER("TLSv1_3_method");
  11789. #ifndef NO_WOLFSSL_CLIENT
  11790. m = wolfTLSv1_3_client_method_ex(heap);
  11791. #else
  11792. m = wolfTLSv1_3_server_method_ex(heap);
  11793. #endif
  11794. if (m != NULL) {
  11795. m->side = WOLFSSL_NEITHER_END;
  11796. }
  11797. return m;
  11798. }
  11799. #endif /* WOLFSSL_TLS13 */
  11800. #ifdef WOLFSSL_DTLS
  11801. WOLFSSL_METHOD* wolfDTLS_method(void)
  11802. {
  11803. return wolfDTLS_method_ex(NULL);
  11804. }
  11805. WOLFSSL_METHOD* wolfDTLS_method_ex(void* heap)
  11806. {
  11807. WOLFSSL_METHOD* m;
  11808. WOLFSSL_ENTER("DTLS_method_ex");
  11809. #ifndef NO_WOLFSSL_CLIENT
  11810. m = wolfDTLS_client_method_ex(heap);
  11811. #else
  11812. m = wolfDTLS_server_method_ex(heap);
  11813. #endif
  11814. if (m != NULL) {
  11815. m->side = WOLFSSL_NEITHER_END;
  11816. }
  11817. return m;
  11818. }
  11819. #ifndef NO_OLD_TLS
  11820. WOLFSSL_METHOD* wolfDTLSv1_method(void)
  11821. {
  11822. return wolfDTLSv1_method_ex(NULL);
  11823. }
  11824. WOLFSSL_METHOD* wolfDTLSv1_method_ex(void* heap)
  11825. {
  11826. WOLFSSL_METHOD* m;
  11827. WOLFSSL_ENTER("DTLSv1_method_ex");
  11828. #ifndef NO_WOLFSSL_CLIENT
  11829. m = wolfDTLSv1_client_method_ex(heap);
  11830. #else
  11831. m = wolfDTLSv1_server_method_ex(heap);
  11832. #endif
  11833. if (m != NULL) {
  11834. m->side = WOLFSSL_NEITHER_END;
  11835. }
  11836. return m;
  11837. }
  11838. #endif /* !NO_OLD_TLS */
  11839. #ifndef WOLFSSL_NO_TLS12
  11840. WOLFSSL_METHOD* wolfDTLSv1_2_method(void)
  11841. {
  11842. return wolfDTLSv1_2_method_ex(NULL);
  11843. }
  11844. WOLFSSL_METHOD* wolfDTLSv1_2_method_ex(void* heap)
  11845. {
  11846. WOLFSSL_METHOD* m;
  11847. WOLFSSL_ENTER("DTLSv1_2_method");
  11848. #ifndef NO_WOLFSSL_CLIENT
  11849. m = wolfDTLSv1_2_client_method_ex(heap);
  11850. #else
  11851. m = wolfDTLSv1_2_server_method_ex(heap);
  11852. #endif
  11853. if (m != NULL) {
  11854. m->side = WOLFSSL_NEITHER_END;
  11855. }
  11856. return m;
  11857. }
  11858. #endif /* !WOLFSSL_NO_TLS12 */
  11859. #endif /* WOLFSSL_DTLS */
  11860. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  11861. #ifndef NO_WOLFSSL_SERVER
  11862. WOLFSSL_METHOD* wolfTLS_server_method(void)
  11863. {
  11864. return wolfTLS_server_method_ex(NULL);
  11865. }
  11866. WOLFSSL_METHOD* wolfTLS_server_method_ex(void* heap)
  11867. {
  11868. WOLFSSL_METHOD* method =
  11869. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11870. heap, DYNAMIC_TYPE_METHOD);
  11871. (void)heap;
  11872. WOLFSSL_ENTER("TLS_server_method_ex");
  11873. if (method) {
  11874. #if defined(WOLFSSL_TLS13)
  11875. InitSSL_Method(method, MakeTLSv1_3());
  11876. #elif !defined(WOLFSSL_NO_TLS12)
  11877. InitSSL_Method(method, MakeTLSv1_2());
  11878. #elif !defined(NO_OLD_TLS)
  11879. InitSSL_Method(method, MakeTLSv1_1());
  11880. #elif defined(WOLFSSL_ALLOW_TLSV10)
  11881. InitSSL_Method(method, MakeTLSv1());
  11882. #else
  11883. #error No TLS version enabled!
  11884. #endif
  11885. method->downgrade = 1;
  11886. method->side = WOLFSSL_SERVER_END;
  11887. }
  11888. return method;
  11889. }
  11890. #ifndef NO_OLD_TLS
  11891. #ifdef WOLFSSL_ALLOW_TLSV10
  11892. WOLFSSL_METHOD* wolfTLSv1_server_method(void)
  11893. {
  11894. return wolfTLSv1_server_method_ex(NULL);
  11895. }
  11896. WOLFSSL_METHOD* wolfTLSv1_server_method_ex(void* heap)
  11897. {
  11898. WOLFSSL_METHOD* method =
  11899. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11900. heap, DYNAMIC_TYPE_METHOD);
  11901. (void)heap;
  11902. WOLFSSL_ENTER("TLSv1_server_method_ex");
  11903. if (method) {
  11904. InitSSL_Method(method, MakeTLSv1());
  11905. method->side = WOLFSSL_SERVER_END;
  11906. }
  11907. return method;
  11908. }
  11909. #endif /* WOLFSSL_ALLOW_TLSV10 */
  11910. WOLFSSL_METHOD* wolfTLSv1_1_server_method(void)
  11911. {
  11912. return wolfTLSv1_1_server_method_ex(NULL);
  11913. }
  11914. WOLFSSL_METHOD* wolfTLSv1_1_server_method_ex(void* heap)
  11915. {
  11916. WOLFSSL_METHOD* method =
  11917. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11918. heap, DYNAMIC_TYPE_METHOD);
  11919. (void)heap;
  11920. WOLFSSL_ENTER("TLSv1_1_server_method_ex");
  11921. if (method) {
  11922. InitSSL_Method(method, MakeTLSv1_1());
  11923. method->side = WOLFSSL_SERVER_END;
  11924. }
  11925. return method;
  11926. }
  11927. #endif /* !NO_OLD_TLS */
  11928. #ifndef WOLFSSL_NO_TLS12
  11929. WOLFSSL_ABI
  11930. WOLFSSL_METHOD* wolfTLSv1_2_server_method(void)
  11931. {
  11932. return wolfTLSv1_2_server_method_ex(NULL);
  11933. }
  11934. WOLFSSL_METHOD* wolfTLSv1_2_server_method_ex(void* heap)
  11935. {
  11936. WOLFSSL_METHOD* method =
  11937. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11938. heap, DYNAMIC_TYPE_METHOD);
  11939. (void)heap;
  11940. WOLFSSL_ENTER("TLSv1_2_server_method_ex");
  11941. if (method) {
  11942. InitSSL_Method(method, MakeTLSv1_2());
  11943. method->side = WOLFSSL_SERVER_END;
  11944. }
  11945. return method;
  11946. }
  11947. #endif /* !WOLFSSL_NO_TLS12 */
  11948. #ifdef WOLFSSL_TLS13
  11949. /* The TLS v1.3 server method data.
  11950. *
  11951. * returns the method data for a TLS v1.3 server.
  11952. */
  11953. WOLFSSL_ABI
  11954. WOLFSSL_METHOD* wolfTLSv1_3_server_method(void)
  11955. {
  11956. return wolfTLSv1_3_server_method_ex(NULL);
  11957. }
  11958. /* The TLS v1.3 server method data.
  11959. *
  11960. * heap The heap used for allocation.
  11961. * returns the method data for a TLS v1.3 server.
  11962. */
  11963. WOLFSSL_METHOD* wolfTLSv1_3_server_method_ex(void* heap)
  11964. {
  11965. WOLFSSL_METHOD* method =
  11966. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11967. heap, DYNAMIC_TYPE_METHOD);
  11968. (void)heap;
  11969. WOLFSSL_ENTER("TLSv1_3_server_method_ex");
  11970. if (method) {
  11971. InitSSL_Method(method, MakeTLSv1_3());
  11972. method->side = WOLFSSL_SERVER_END;
  11973. }
  11974. return method;
  11975. }
  11976. #endif /* WOLFSSL_TLS13 */
  11977. #ifdef WOLFSSL_DTLS
  11978. WOLFSSL_METHOD* wolfDTLS_server_method(void)
  11979. {
  11980. return wolfDTLS_server_method_ex(NULL);
  11981. }
  11982. WOLFSSL_METHOD* wolfDTLS_server_method_ex(void* heap)
  11983. {
  11984. WOLFSSL_METHOD* method =
  11985. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11986. heap, DYNAMIC_TYPE_METHOD);
  11987. (void)heap;
  11988. WOLFSSL_ENTER("DTLS_server_method_ex");
  11989. if (method) {
  11990. #if defined(WOLFSSL_DTLS13)
  11991. InitSSL_Method(method, MakeDTLSv1_3());
  11992. #elif !defined(WOLFSSL_NO_TLS12)
  11993. InitSSL_Method(method, MakeDTLSv1_2());
  11994. #elif !defined(NO_OLD_TLS)
  11995. InitSSL_Method(method, MakeDTLSv1());
  11996. #else
  11997. #error No DTLS version enabled!
  11998. #endif
  11999. method->downgrade = 1;
  12000. method->side = WOLFSSL_SERVER_END;
  12001. }
  12002. return method;
  12003. }
  12004. #ifndef NO_OLD_TLS
  12005. WOLFSSL_METHOD* wolfDTLSv1_server_method(void)
  12006. {
  12007. return wolfDTLSv1_server_method_ex(NULL);
  12008. }
  12009. WOLFSSL_METHOD* wolfDTLSv1_server_method_ex(void* heap)
  12010. {
  12011. WOLFSSL_METHOD* method =
  12012. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12013. heap, DYNAMIC_TYPE_METHOD);
  12014. (void)heap;
  12015. WOLFSSL_ENTER("DTLSv1_server_method_ex");
  12016. if (method) {
  12017. InitSSL_Method(method, MakeDTLSv1());
  12018. method->side = WOLFSSL_SERVER_END;
  12019. }
  12020. return method;
  12021. }
  12022. #endif /* !NO_OLD_TLS */
  12023. #ifndef WOLFSSL_NO_TLS12
  12024. WOLFSSL_METHOD* wolfDTLSv1_2_server_method(void)
  12025. {
  12026. return wolfDTLSv1_2_server_method_ex(NULL);
  12027. }
  12028. WOLFSSL_METHOD* wolfDTLSv1_2_server_method_ex(void* heap)
  12029. {
  12030. WOLFSSL_METHOD* method =
  12031. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12032. heap, DYNAMIC_TYPE_METHOD);
  12033. WOLFSSL_ENTER("DTLSv1_2_server_method_ex");
  12034. (void)heap;
  12035. if (method) {
  12036. InitSSL_Method(method, MakeDTLSv1_2());
  12037. method->side = WOLFSSL_SERVER_END;
  12038. }
  12039. (void)heap;
  12040. return method;
  12041. }
  12042. #endif /* !WOLFSSL_NO_TLS12 */
  12043. #endif /* WOLFSSL_DTLS */
  12044. #endif /* NO_WOLFSSL_SERVER */
  12045. #endif /* NO_TLS */
  12046. #endif /* WOLFCRYPT_ONLY */