internal.h 251 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663
  1. /* internal.h
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifndef WOLFSSL_INT_H
  22. #define WOLFSSL_INT_H
  23. #include <wolfssl/wolfcrypt/types.h>
  24. #include <wolfssl/ssl.h>
  25. #ifdef HAVE_CRL
  26. #include <wolfssl/crl.h>
  27. #endif
  28. #include <wolfssl/wolfcrypt/random.h>
  29. #ifndef NO_DES3
  30. #include <wolfssl/wolfcrypt/des3.h>
  31. #endif
  32. #ifdef HAVE_CHACHA
  33. #include <wolfssl/wolfcrypt/chacha.h>
  34. #endif
  35. #ifndef NO_ASN
  36. #include <wolfssl/wolfcrypt/asn.h>
  37. #include <wolfssl/wolfcrypt/pkcs12.h>
  38. #endif
  39. #ifndef NO_MD5
  40. #include <wolfssl/wolfcrypt/md5.h>
  41. #endif
  42. #ifndef NO_SHA
  43. #include <wolfssl/wolfcrypt/sha.h>
  44. #endif
  45. #ifndef NO_AES
  46. #include <wolfssl/wolfcrypt/aes.h>
  47. #endif
  48. #ifdef HAVE_POLY1305
  49. #include <wolfssl/wolfcrypt/poly1305.h>
  50. #endif
  51. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && defined(OPENSSL_EXTRA)
  52. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  53. #endif
  54. #ifdef HAVE_ARIA
  55. #include <wolfssl/wolfcrypt/port/aria/aria-crypt.h>
  56. #endif
  57. #ifdef HAVE_CAMELLIA
  58. #include <wolfssl/wolfcrypt/camellia.h>
  59. #endif
  60. #ifdef WOLFSSL_SM4
  61. #include <wolfssl/wolfcrypt/sm4.h>
  62. #endif
  63. #include <wolfssl/wolfcrypt/logging.h>
  64. #ifndef NO_HMAC
  65. #include <wolfssl/wolfcrypt/hmac.h>
  66. #endif
  67. #ifndef NO_RC4
  68. #include <wolfssl/wolfcrypt/arc4.h>
  69. #endif
  70. #ifndef NO_SHA256
  71. #include <wolfssl/wolfcrypt/sha256.h>
  72. #endif
  73. #if defined(WOLFSSL_SHA384)
  74. #include <wolfssl/wolfcrypt/sha512.h>
  75. #endif
  76. #ifdef HAVE_OCSP
  77. #include <wolfssl/ocsp.h>
  78. #endif
  79. #ifdef WOLFSSL_QUIC
  80. #include <wolfssl/quic.h>
  81. #endif
  82. #ifdef WOLFSSL_SHA384
  83. #include <wolfssl/wolfcrypt/sha512.h>
  84. #endif
  85. #ifdef WOLFSSL_SHA512
  86. #include <wolfssl/wolfcrypt/sha512.h>
  87. #endif
  88. #ifdef WOLFSSL_SM3
  89. #include <wolfssl/wolfcrypt/sm3.h>
  90. #endif
  91. #ifdef HAVE_AESGCM
  92. #include <wolfssl/wolfcrypt/sha512.h>
  93. #endif
  94. #ifdef WOLFSSL_RIPEMD
  95. #include <wolfssl/wolfcrypt/ripemd.h>
  96. #endif
  97. #ifndef NO_RSA
  98. #include <wolfssl/wolfcrypt/rsa.h>
  99. #endif
  100. #ifdef HAVE_ECC
  101. #include <wolfssl/wolfcrypt/ecc.h>
  102. #endif
  103. #ifdef WOLFSSL_SM2
  104. #include <wolfssl/wolfcrypt/sm2.h>
  105. #endif
  106. #ifndef NO_DH
  107. #include <wolfssl/wolfcrypt/dh.h>
  108. #endif
  109. #ifdef HAVE_ED25519
  110. #include <wolfssl/wolfcrypt/ed25519.h>
  111. #endif
  112. #ifdef HAVE_CURVE25519
  113. #include <wolfssl/wolfcrypt/curve25519.h>
  114. #endif
  115. #ifdef HAVE_ED448
  116. #include <wolfssl/wolfcrypt/ed448.h>
  117. #endif
  118. #ifdef HAVE_CURVE448
  119. #include <wolfssl/wolfcrypt/curve448.h>
  120. #endif
  121. #ifdef HAVE_PQC
  122. #include <wolfssl/wolfcrypt/falcon.h>
  123. #include <wolfssl/wolfcrypt/dilithium.h>
  124. #endif
  125. #ifdef HAVE_HKDF
  126. #include <wolfssl/wolfcrypt/kdf.h>
  127. #endif
  128. #ifndef WOLFSSL_NO_DEF_TICKET_ENC_CB
  129. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && \
  130. !defined(WOLFSSL_TICKET_ENC_AES128_GCM) && \
  131. !defined(WOLFSSL_TICKET_ENC_AES256_GCM)
  132. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  133. #else
  134. #include <wolfssl/wolfcrypt/aes.h>
  135. #endif
  136. #endif
  137. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  138. #include <wolfssl/wolfcrypt/hash.h>
  139. #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
  140. #include <wolfssl/callbacks.h>
  141. #endif
  142. #ifdef WOLFSSL_CALLBACKS
  143. #include <signal.h>
  144. #endif
  145. #ifdef USE_WINDOWS_API
  146. #ifdef WOLFSSL_GAME_BUILD
  147. #include "system/xtl.h"
  148. #else
  149. #if defined(_WIN32_WCE) || defined(WIN32_LEAN_AND_MEAN)
  150. /* On WinCE winsock2.h must be included before windows.h */
  151. #include <winsock2.h>
  152. #endif
  153. #include <windows.h>
  154. #endif
  155. #elif defined(THREADX)
  156. #ifndef SINGLE_THREADED
  157. #include "tx_api.h"
  158. #endif
  159. #elif defined(WOLFSSL_DEOS)
  160. /* do nothing, just don't pick Unix */
  161. #elif defined(MICRIUM)
  162. /* do nothing, just don't pick Unix */
  163. #elif defined(FREERTOS) || defined(FREERTOS_TCP) || defined(WOLFSSL_SAFERTOS)
  164. /* do nothing */
  165. #elif defined(RTTHREAD)
  166. /* do nothing */
  167. #elif defined(EBSNET)
  168. /* do nothing */
  169. #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  170. /* do nothing */
  171. #elif defined(FREESCALE_FREE_RTOS)
  172. #include "fsl_os_abstraction.h"
  173. #elif defined(WOLFSSL_uITRON4)
  174. /* do nothing */
  175. #elif defined(WOLFSSL_uTKERNEL2)
  176. /* do nothing */
  177. #elif defined(WOLFSSL_CMSIS_RTOS)
  178. #include "cmsis_os.h"
  179. #elif defined(WOLFSSL_CMSIS_RTOSv2)
  180. #include "cmsis_os2.h"
  181. #elif defined(WOLFSSL_MDK_ARM)
  182. #if defined(WOLFSSL_MDK5)
  183. #include "cmsis_os.h"
  184. #else
  185. #include <rtl.h>
  186. #endif
  187. #elif defined(MBED)
  188. #elif defined(WOLFSSL_TIRTOS)
  189. /* do nothing */
  190. #elif defined(INTIME_RTOS)
  191. #include <rt.h>
  192. #elif defined(WOLFSSL_NUCLEUS_1_2)
  193. /* do nothing */
  194. #elif defined(WOLFSSL_APACHE_MYNEWT)
  195. #if !defined(WOLFSSL_LWIP)
  196. void mynewt_ctx_clear(void *ctx);
  197. void* mynewt_ctx_new();
  198. #endif
  199. #elif defined(WOLFSSL_ZEPHYR)
  200. #ifndef SINGLE_THREADED
  201. #include <zephyr/kernel.h>
  202. #endif
  203. #elif defined(WOLFSSL_TELIT_M2MB)
  204. /* do nothing */
  205. #elif defined(WOLFSSL_EMBOS)
  206. /* do nothing */
  207. #else
  208. #ifndef SINGLE_THREADED
  209. #if defined(WOLFSSL_LINUXKM)
  210. /* setup is in linuxkm/linuxkm_wc_port.h */
  211. #elif defined(WOLFSSL_USER_MUTEX)
  212. /* do nothing */
  213. #else
  214. #define WOLFSSL_PTHREADS
  215. #include <pthread.h>
  216. #endif
  217. #endif
  218. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  219. #ifdef FUSION_RTOS
  220. #include <fclunistd.h>
  221. #else
  222. #include <unistd.h> /* for close of BIO */
  223. #endif
  224. #endif
  225. #endif
  226. #if !defined(CHAR_BIT) || (defined(OPENSSL_EXTRA) && !defined(INT_MAX))
  227. /* Needed for DTLS without big math and INT_MAX */
  228. #include <limits.h>
  229. #endif
  230. #ifdef HAVE_LIBZ
  231. #include "zlib.h"
  232. #endif
  233. #ifdef WOLFSSL_ASYNC_CRYPT
  234. #include <wolfssl/wolfcrypt/async.h>
  235. #endif
  236. #ifdef OPENSSL_EXTRA
  237. #ifdef WOLFCRYPT_HAVE_SRP
  238. #include <wolfssl/wolfcrypt/srp.h>
  239. #endif
  240. #endif
  241. #ifdef _MSC_VER
  242. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  243. #pragma warning(disable: 4996)
  244. #endif
  245. #ifdef NO_SHA
  246. #define WC_SHA_DIGEST_SIZE 20
  247. #endif
  248. #ifdef NO_SHA256
  249. #define WC_SHA256_DIGEST_SIZE 32
  250. #endif
  251. #ifdef NO_MD5
  252. #define WC_MD5_DIGEST_SIZE 16
  253. #endif
  254. #ifdef WOLFSSL_IOTSAFE
  255. #include <wolfssl/wolfcrypt/port/iotsafe/iotsafe.h>
  256. #endif
  257. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  258. #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
  259. #endif
  260. #include <wolfssl/wolfcrypt/hpke.h>
  261. #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE)
  262. #include <wolfssl/sniffer.h>
  263. #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */
  264. #ifdef __cplusplus
  265. extern "C" {
  266. #endif
  267. /* Define or comment out the cipher suites you'd like to be compiled in
  268. make sure to use at least one BUILD_SSL_xxx or BUILD_TLS_xxx is defined
  269. When adding cipher suites, add name to cipher_names, idx to cipher_name_idx
  270. Now that there is a maximum strength crypto build, the following BUILD_XXX
  271. flags need to be divided into two groups selected by WOLFSSL_MAX_STRENGTH.
  272. Those that do not use Perfect Forward Security and do not use AEAD ciphers
  273. need to be switched off. Allowed suites use (EC)DHE, AES-GCM|CCM, or
  274. CHACHA-POLY.
  275. */
  276. /* Check that if WOLFSSL_MAX_STRENGTH is set that all the required options are
  277. * not turned off. */
  278. #if defined(WOLFSSL_MAX_STRENGTH) && \
  279. ((!defined(HAVE_ECC) && (defined(NO_DH) || defined(NO_RSA))) || \
  280. (!defined(HAVE_AESGCM) && !defined(HAVE_AESCCM) && \
  281. (!defined(HAVE_POLY1305) || !defined(HAVE_CHACHA))) || \
  282. (defined(NO_SHA256) && !defined(WOLFSSL_SHA384)) || \
  283. !defined(NO_OLD_TLS))
  284. #error "You are trying to build max strength with requirements disabled."
  285. #endif
  286. #ifndef WOLFSSL_NO_TLS12
  287. #ifndef WOLFSSL_MAX_STRENGTH
  288. #ifdef WOLFSSL_AEAD_ONLY
  289. /* AES CBC ciphers are not allowed in AEAD only mode */
  290. #undef HAVE_AES_CBC
  291. #endif
  292. /* When adding new ciphersuites, make sure that they have appropriate
  293. * guards for WOLFSSL_HARDEN_TLS. */
  294. #if defined(WOLFSSL_HARDEN_TLS) && \
  295. !defined(WOLFSSL_HARDEN_TLS_ALLOW_ALL_CIPHERSUITES)
  296. /* Use a separate define (undef'ed later) to simplify macro logic. */
  297. #define WSSL_HARDEN_TLS WOLFSSL_HARDEN_TLS
  298. #define NO_TLS_DH
  299. #endif
  300. #ifndef WOLFSSL_AEAD_ONLY
  301. #if !defined(NO_RSA) && !defined(NO_RC4) && !defined(WSSL_HARDEN_TLS)
  302. /* MUST NOT negotiate RC4 cipher suites
  303. * https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
  304. #if defined(WOLFSSL_STATIC_RSA)
  305. #if !defined(NO_SHA)
  306. #define BUILD_SSL_RSA_WITH_RC4_128_SHA
  307. #endif
  308. #if !defined(NO_MD5)
  309. #define BUILD_SSL_RSA_WITH_RC4_128_MD5
  310. #endif
  311. #endif
  312. #endif
  313. #if !defined(NO_RSA) && !defined(NO_DES3)
  314. #if !defined(NO_SHA)
  315. #if defined(WOLFSSL_STATIC_RSA)
  316. #define BUILD_SSL_RSA_WITH_3DES_EDE_CBC_SHA
  317. #endif
  318. #endif
  319. #endif
  320. #endif /* !WOLFSSL_AEAD_ONLY */
  321. #if !defined(NO_RSA) && !defined(NO_AES) && !defined(NO_TLS)
  322. #if !defined(NO_SHA) && defined(HAVE_AES_CBC)
  323. #if defined(WOLFSSL_STATIC_RSA)
  324. #ifdef WOLFSSL_AES_128
  325. #define BUILD_TLS_RSA_WITH_AES_128_CBC_SHA
  326. #endif
  327. #ifdef WOLFSSL_AES_256
  328. #define BUILD_TLS_RSA_WITH_AES_256_CBC_SHA
  329. #endif
  330. #endif
  331. #endif
  332. #if defined(WOLFSSL_STATIC_RSA)
  333. #if !defined (NO_SHA256) && defined(HAVE_AES_CBC)
  334. #ifdef WOLFSSL_AES_128
  335. #define BUILD_TLS_RSA_WITH_AES_128_CBC_SHA256
  336. #endif
  337. #ifdef WOLFSSL_AES_256
  338. #define BUILD_TLS_RSA_WITH_AES_256_CBC_SHA256
  339. #endif
  340. #endif
  341. #if defined (HAVE_AESGCM)
  342. #ifdef WOLFSSL_AES_128
  343. #define BUILD_TLS_RSA_WITH_AES_128_GCM_SHA256
  344. #endif
  345. #if defined (WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  346. #define BUILD_TLS_RSA_WITH_AES_256_GCM_SHA384
  347. #endif
  348. #endif
  349. #if defined (HAVE_AESCCM)
  350. #ifdef WOLFSSL_AES_128
  351. #define BUILD_TLS_RSA_WITH_AES_128_CCM_8
  352. #endif
  353. #ifdef WOLFSSL_AES_256
  354. #define BUILD_TLS_RSA_WITH_AES_256_CCM_8
  355. #endif
  356. #endif
  357. #endif
  358. #endif
  359. #if defined(HAVE_CAMELLIA) && !defined(NO_TLS) && !defined(NO_CAMELLIA_CBC)
  360. #ifndef NO_RSA
  361. #if defined(WOLFSSL_STATIC_RSA)
  362. #if !defined(NO_SHA)
  363. #define BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA
  364. #define BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA
  365. #endif
  366. #ifndef NO_SHA256
  367. #define BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256
  368. #define BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256
  369. #endif
  370. #endif
  371. #if !defined(NO_DH) && !defined(NO_TLS_DH)
  372. /* SHOULD NOT negotiate cipher suites based on ephemeral
  373. * finite-field Diffie-Hellman key agreement (i.e., "TLS_DHE_*"
  374. * suites). https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
  375. #if !defined(NO_SHA)
  376. #define BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA
  377. #define BUILD_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA
  378. #endif
  379. #ifndef NO_SHA256
  380. #define BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256
  381. #define BUILD_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256
  382. #endif
  383. #endif
  384. #endif
  385. #endif
  386. #if defined(WOLFSSL_STATIC_PSK)
  387. #if !defined(NO_PSK) && !defined(NO_AES) && !defined(NO_TLS)
  388. #if !defined(NO_SHA)
  389. #ifdef WOLFSSL_AES_128
  390. #define BUILD_TLS_PSK_WITH_AES_128_CBC_SHA
  391. #endif
  392. #ifdef WOLFSSL_AES_256
  393. #define BUILD_TLS_PSK_WITH_AES_256_CBC_SHA
  394. #endif
  395. #endif
  396. #ifndef NO_SHA256
  397. #ifdef WOLFSSL_AES_128
  398. #ifdef HAVE_AES_CBC
  399. #define BUILD_TLS_PSK_WITH_AES_128_CBC_SHA256
  400. #endif
  401. #ifdef HAVE_AESGCM
  402. #define BUILD_TLS_PSK_WITH_AES_128_GCM_SHA256
  403. #endif
  404. #endif /* WOLFSSL_AES_128 */
  405. #ifdef HAVE_AESCCM
  406. #ifdef WOLFSSL_AES_128
  407. #define BUILD_TLS_PSK_WITH_AES_128_CCM_8
  408. #define BUILD_TLS_PSK_WITH_AES_128_CCM
  409. #endif
  410. #ifdef WOLFSSL_AES_256
  411. #define BUILD_TLS_PSK_WITH_AES_256_CCM_8
  412. #define BUILD_TLS_PSK_WITH_AES_256_CCM
  413. #endif
  414. #endif
  415. #endif
  416. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  417. #ifdef HAVE_AES_CBC
  418. #define BUILD_TLS_PSK_WITH_AES_256_CBC_SHA384
  419. #endif
  420. #ifdef HAVE_AESGCM
  421. #define BUILD_TLS_PSK_WITH_AES_256_GCM_SHA384
  422. #endif
  423. #endif
  424. #endif
  425. #endif
  426. #if !defined(NO_TLS) && defined(HAVE_NULL_CIPHER)
  427. #if !defined(NO_RSA)
  428. #if defined(WOLFSSL_STATIC_RSA)
  429. #ifndef NO_MD5
  430. #define BUILD_TLS_RSA_WITH_NULL_MD5
  431. #endif
  432. #if !defined(NO_SHA)
  433. #define BUILD_TLS_RSA_WITH_NULL_SHA
  434. #endif
  435. #ifndef NO_SHA256
  436. #define BUILD_TLS_RSA_WITH_NULL_SHA256
  437. #endif
  438. #endif
  439. #endif
  440. #if !defined(NO_PSK) && defined(WOLFSSL_STATIC_PSK)
  441. #if !defined(NO_SHA)
  442. #define BUILD_TLS_PSK_WITH_NULL_SHA
  443. #endif
  444. #ifndef NO_SHA256
  445. #define BUILD_TLS_PSK_WITH_NULL_SHA256
  446. #endif
  447. #ifdef WOLFSSL_SHA384
  448. #define BUILD_TLS_PSK_WITH_NULL_SHA384
  449. #endif
  450. #endif
  451. #endif
  452. #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
  453. !defined(NO_RSA) && !defined(NO_TLS_DH)
  454. /* SHOULD NOT negotiate cipher suites based on ephemeral
  455. * finite-field Diffie-Hellman key agreement (i.e., "TLS_DHE_*"
  456. * suites). https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
  457. #if !defined(NO_SHA)
  458. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  459. #define BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  460. #endif
  461. #if defined(WOLFSSL_AES_256) && defined(HAVE_AES_CBC)
  462. #define BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA
  463. #endif
  464. #if !defined(NO_DES3)
  465. #define BUILD_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA
  466. #endif
  467. #endif
  468. #if !defined(NO_SHA256) && defined(HAVE_AES_CBC)
  469. #ifdef WOLFSSL_AES_128
  470. #define BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
  471. #endif
  472. #ifdef WOLFSSL_AES_256
  473. #define BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
  474. #endif
  475. #endif
  476. #endif
  477. #if defined(HAVE_ANON) && !defined(NO_TLS) && !defined(NO_DH) && \
  478. !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  479. #ifdef HAVE_AES_CBC
  480. #define BUILD_TLS_DH_anon_WITH_AES_128_CBC_SHA
  481. #endif
  482. #if defined(WOLFSSL_SHA384) && defined(HAVE_AESGCM)
  483. #define BUILD_TLS_DH_anon_WITH_AES_256_GCM_SHA384
  484. #endif
  485. #endif
  486. #if !defined(NO_DH) && !defined(NO_PSK) && !defined(NO_TLS) && \
  487. !defined(NO_TLS_DH)
  488. /* SHOULD NOT negotiate cipher suites based on ephemeral
  489. * finite-field Diffie-Hellman key agreement (i.e., "TLS_DHE_*"
  490. * suites). https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
  491. #ifndef NO_SHA256
  492. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && \
  493. defined(HAVE_AES_CBC)
  494. #define BUILD_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256
  495. #endif
  496. #ifdef HAVE_NULL_CIPHER
  497. #define BUILD_TLS_DHE_PSK_WITH_NULL_SHA256
  498. #endif
  499. #endif
  500. #ifdef WOLFSSL_SHA384
  501. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && \
  502. defined(HAVE_AES_CBC)
  503. #define BUILD_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384
  504. #endif
  505. #ifdef HAVE_NULL_CIPHER
  506. #define BUILD_TLS_DHE_PSK_WITH_NULL_SHA384
  507. #endif
  508. #endif
  509. #endif
  510. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  511. defined(HAVE_CURVE448)) && !defined(NO_TLS)
  512. #if !defined(NO_AES)
  513. #if !defined(NO_SHA) && defined(HAVE_AES_CBC)
  514. #if !defined(NO_RSA)
  515. #ifdef WOLFSSL_AES_128
  516. #define BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
  517. #endif
  518. #ifdef WOLFSSL_AES_256
  519. #define BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
  520. #endif
  521. #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
  522. #ifdef WOLFSSL_AES_128
  523. #define BUILD_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
  524. #endif
  525. #ifdef WOLFSSL_AES_256
  526. #define BUILD_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
  527. #endif
  528. #endif
  529. #endif
  530. #if defined(HAVE_ECC) || \
  531. (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
  532. (defined(HAVE_CURVE448) && defined(HAVE_ED448))
  533. #ifdef WOLFSSL_AES_128
  534. #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
  535. #endif
  536. #ifdef WOLFSSL_AES_256
  537. #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
  538. #endif
  539. #endif
  540. #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
  541. #ifdef WOLFSSL_AES_128
  542. #define BUILD_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
  543. #endif
  544. #ifdef WOLFSSL_AES_256
  545. #define BUILD_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
  546. #endif
  547. #endif
  548. #endif /* NO_SHA */
  549. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  550. defined(HAVE_AES_CBC)
  551. #if !defined(NO_RSA)
  552. #define BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
  553. #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
  554. #define BUILD_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
  555. #endif
  556. #endif
  557. #if defined(HAVE_ECC) || \
  558. (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
  559. (defined(HAVE_CURVE448) && defined(HAVE_ED448))
  560. #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
  561. #endif
  562. #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
  563. #define BUILD_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
  564. #endif
  565. #endif
  566. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  567. defined(HAVE_AES_CBC)
  568. #if !defined(NO_RSA)
  569. #define BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
  570. #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
  571. #define BUILD_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
  572. #endif
  573. #endif
  574. #if defined(HAVE_ECC) || \
  575. (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
  576. (defined(HAVE_CURVE448) && defined(HAVE_ED448))
  577. #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
  578. #endif
  579. #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
  580. #define BUILD_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
  581. #endif
  582. #endif
  583. #if defined (HAVE_AESGCM)
  584. #if !defined(NO_RSA)
  585. #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
  586. #ifdef WOLFSSL_AES_128
  587. #define BUILD_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
  588. #endif
  589. #endif
  590. #if defined(WOLFSSL_SHA384)
  591. #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
  592. #ifdef WOLFSSL_AES_256
  593. #define BUILD_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
  594. #endif
  595. #endif
  596. #endif
  597. #endif
  598. #if defined(WOLFSSL_STATIC_DH) && defined(WOLFSSL_AES_128) && \
  599. defined(HAVE_ECC)
  600. #define BUILD_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
  601. #endif
  602. #if defined(WOLFSSL_SHA384)
  603. #if defined(WOLFSSL_STATIC_DH) && \
  604. defined(WOLFSSL_AES_256) && defined(HAVE_ECC)
  605. #define BUILD_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
  606. #endif
  607. #endif
  608. #endif
  609. #endif /* NO_AES */
  610. #ifdef HAVE_ARIA
  611. #define BUILD_TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256
  612. #define BUILD_TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384
  613. #endif /* HAVE_ARIA */
  614. #if !defined(NO_RC4) && !defined(WSSL_HARDEN_TLS)
  615. /* MUST NOT negotiate RC4 cipher suites
  616. * https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
  617. #if !defined(NO_SHA)
  618. #if !defined(NO_RSA)
  619. #ifndef WOLFSSL_AEAD_ONLY
  620. #define BUILD_TLS_ECDHE_RSA_WITH_RC4_128_SHA
  621. #endif
  622. #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
  623. #define BUILD_TLS_ECDH_RSA_WITH_RC4_128_SHA
  624. #endif
  625. #endif
  626. #if defined(HAVE_ECC) || \
  627. (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
  628. (defined(HAVE_CURVE448) && defined(HAVE_ED448))
  629. #ifndef WOLFSSL_AEAD_ONLY
  630. #define BUILD_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
  631. #endif
  632. #endif
  633. #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
  634. #define BUILD_TLS_ECDH_ECDSA_WITH_RC4_128_SHA
  635. #endif
  636. #endif
  637. #endif
  638. #if !defined(NO_DES3) && !(defined(WSSL_HARDEN_TLS) && \
  639. WSSL_HARDEN_TLS > 112)
  640. /* 3DES offers only 112 bits of security.
  641. * Using guidance from section 5.6.1
  642. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r5.pdf */
  643. #ifndef NO_SHA
  644. #if !defined(NO_RSA)
  645. #define BUILD_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
  646. #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
  647. #define BUILD_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
  648. #endif
  649. #endif
  650. #if defined(HAVE_ECC) || \
  651. (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
  652. (defined(HAVE_CURVE448) && defined(HAVE_ED448))
  653. #define BUILD_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
  654. #endif
  655. #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
  656. #define BUILD_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
  657. #endif
  658. #endif /* NO_SHA */
  659. #endif
  660. #if defined(HAVE_NULL_CIPHER)
  661. #if !defined(NO_SHA)
  662. #if defined(HAVE_ECC) || \
  663. (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
  664. (defined(HAVE_CURVE448) && defined(HAVE_ED448))
  665. #define BUILD_TLS_ECDHE_ECDSA_WITH_NULL_SHA
  666. #endif
  667. #endif
  668. #if !defined(NO_PSK) && !defined(NO_SHA256)
  669. #define BUILD_TLS_ECDHE_PSK_WITH_NULL_SHA256
  670. #endif
  671. #endif
  672. #if !defined(NO_PSK) && !defined(NO_SHA256) && !defined(NO_AES) && \
  673. defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  674. #define BUILD_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256
  675. #endif
  676. #if !defined(NO_PSK) && !defined(NO_SHA256) && !defined(NO_AES) && \
  677. defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM)
  678. #define BUILD_TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256
  679. #endif
  680. #endif
  681. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_SHA256)
  682. #if !defined(NO_OLD_POLY1305)
  683. #if defined(HAVE_ECC) || \
  684. (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
  685. (defined(HAVE_CURVE448) && defined(HAVE_ED448))
  686. #define BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256
  687. #endif
  688. #if !defined(NO_RSA) && defined(HAVE_ECC)
  689. #define BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256
  690. #endif
  691. #if !defined(NO_DH) && !defined(NO_RSA) && !defined(NO_TLS_DH)
  692. /* SHOULD NOT negotiate cipher suites based on ephemeral
  693. * finite-field Diffie-Hellman key agreement (i.e., "TLS_DHE_*"
  694. * suites). https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
  695. #define BUILD_TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256
  696. #endif
  697. #endif /* NO_OLD_POLY1305 */
  698. #if !defined(NO_PSK)
  699. #define BUILD_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256
  700. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || \
  701. defined(HAVE_ED448)
  702. #define BUILD_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256
  703. #endif
  704. #if !defined(NO_DH) && !defined(NO_TLS_DH)
  705. /* SHOULD NOT negotiate cipher suites based on ephemeral
  706. * finite-field Diffie-Hellman key agreement (i.e., "TLS_DHE_*"
  707. * suites). https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
  708. #define BUILD_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256
  709. #endif
  710. #endif /* !NO_PSK */
  711. #endif
  712. #endif /* !WOLFSSL_MAX_STRENGTH */
  713. #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
  714. !defined(NO_RSA) && defined(HAVE_AESGCM) && !defined(NO_TLS_DH)
  715. /* SHOULD NOT negotiate cipher suites based on ephemeral
  716. * finite-field Diffie-Hellman key agreement (i.e., "TLS_DHE_*"
  717. * suites). https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
  718. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  719. #define BUILD_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
  720. #endif
  721. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  722. #define BUILD_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
  723. #endif
  724. #endif
  725. #if !defined(NO_DH) && !defined(NO_PSK) && !defined(NO_TLS) && \
  726. !defined(NO_TLS_DH)
  727. /* SHOULD NOT negotiate cipher suites based on ephemeral
  728. * finite-field Diffie-Hellman key agreement (i.e., "TLS_DHE_*"
  729. * suites). https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
  730. #ifndef NO_SHA256
  731. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  732. #define BUILD_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256
  733. #endif
  734. #ifdef HAVE_AESCCM
  735. #ifdef WOLFSSL_AES_128
  736. #define BUILD_TLS_DHE_PSK_WITH_AES_128_CCM
  737. #endif
  738. #ifdef WOLFSSL_AES_256
  739. #define BUILD_TLS_DHE_PSK_WITH_AES_256_CCM
  740. #endif
  741. #endif
  742. #endif
  743. #if defined(WOLFSSL_SHA384) && defined(HAVE_AESGCM) && \
  744. defined(WOLFSSL_AES_256)
  745. #define BUILD_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384
  746. #endif
  747. #endif
  748. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) \
  749. && !defined(NO_TLS) && !defined(NO_AES)
  750. #ifdef HAVE_AESGCM
  751. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  752. #if defined(HAVE_ECC) || \
  753. (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
  754. (defined(HAVE_CURVE448) && defined(HAVE_ED448))
  755. #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
  756. #endif
  757. #ifndef NO_RSA
  758. #define BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  759. #endif
  760. #endif
  761. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  762. #if defined(HAVE_ECC) || \
  763. (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
  764. (defined(HAVE_CURVE448) && defined(HAVE_ED448))
  765. #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
  766. #endif
  767. #ifndef NO_RSA
  768. #define BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
  769. #endif
  770. #endif
  771. #endif
  772. #if defined(HAVE_AESCCM) && !defined(NO_SHA256)
  773. #if defined(HAVE_ECC) || \
  774. (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
  775. (defined(HAVE_CURVE448) && defined(HAVE_ED448))
  776. #ifdef WOLFSSL_AES_128
  777. #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM
  778. #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
  779. #endif
  780. #ifdef WOLFSSL_AES_256
  781. #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8
  782. #endif
  783. #endif
  784. #endif
  785. #endif
  786. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_SHA256)
  787. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  788. #if defined(HAVE_ECC) || \
  789. (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
  790. (defined(HAVE_CURVE448) && defined(HAVE_ED448))
  791. #define BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
  792. #endif
  793. #ifndef NO_RSA
  794. #define BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
  795. #endif
  796. #endif
  797. #if !defined(NO_DH) && !defined(NO_RSA) && !defined(NO_TLS_DH)
  798. /* SHOULD NOT negotiate cipher suites based on ephemeral
  799. * finite-field Diffie-Hellman key agreement (i.e., "TLS_DHE_*"
  800. * suites). https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
  801. #define BUILD_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256
  802. #endif
  803. #endif
  804. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)
  805. #ifdef WOLFSSL_SM4_CBC
  806. #define BUILD_TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3
  807. #endif
  808. #ifdef WOLFSSL_SM4_GCM
  809. #define BUILD_TLS_ECDHE_ECDSA_WITH_SM4_GCM_SM3
  810. #endif
  811. #ifdef WOLFSSL_SM4_CCM
  812. #define BUILD_TLS_ECDHE_ECDSA_WITH_SM4_CCM_SM3
  813. #endif
  814. #endif
  815. #endif
  816. #if defined(WOLFSSL_TLS13)
  817. #ifdef HAVE_AESGCM
  818. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  819. #define BUILD_TLS_AES_128_GCM_SHA256
  820. #endif
  821. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  822. #define BUILD_TLS_AES_256_GCM_SHA384
  823. #endif
  824. #endif
  825. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  826. #ifndef NO_SHA256
  827. #define BUILD_TLS_CHACHA20_POLY1305_SHA256
  828. #endif
  829. #endif
  830. #ifdef HAVE_AESCCM
  831. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  832. #define BUILD_TLS_AES_128_CCM_SHA256
  833. #define BUILD_TLS_AES_128_CCM_8_SHA256
  834. #endif
  835. #endif
  836. #ifdef HAVE_NULL_CIPHER
  837. #ifndef NO_SHA256
  838. #define BUILD_TLS_SHA256_SHA256
  839. #endif
  840. #ifdef WOLFSSL_SHA384
  841. #define BUILD_TLS_SHA384_SHA384
  842. #endif
  843. #endif
  844. #ifdef WOLFSSL_SM3
  845. #ifdef WOLFSSL_SM4_GCM
  846. #define BUILD_TLS_SM4_GCM_SM3
  847. #endif
  848. #ifdef WOLFSSL_SM4_CCM
  849. #define BUILD_TLS_SM4_CCM_SM3
  850. #endif
  851. #endif
  852. #endif
  853. #if !defined(WOLFCRYPT_ONLY) && defined(NO_PSK) && \
  854. (defined(NO_DH) || !defined(HAVE_ANON)) && \
  855. defined(NO_RSA) && !defined(HAVE_ECC) && \
  856. !defined(HAVE_ED25519) && !defined(HAVE_ED448)
  857. #error "No cipher suites available with this build"
  858. #endif
  859. #ifdef WOLFSSL_MULTICAST
  860. #if defined(HAVE_NULL_CIPHER) && !defined(NO_SHA256)
  861. #define BUILD_WDM_WITH_NULL_SHA256
  862. #endif
  863. #endif
  864. #if defined(BUILD_SSL_RSA_WITH_RC4_128_SHA) || \
  865. defined(BUILD_SSL_RSA_WITH_RC4_128_MD5)
  866. #define BUILD_ARC4
  867. #endif
  868. #if defined(BUILD_SSL_RSA_WITH_3DES_EDE_CBC_SHA)
  869. #define BUILD_DES3
  870. #endif
  871. #if defined(BUILD_TLS_RSA_WITH_AES_128_CBC_SHA) || \
  872. defined(BUILD_TLS_RSA_WITH_AES_256_CBC_SHA) || \
  873. defined(BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256) || \
  874. defined(BUILD_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256)
  875. #undef BUILD_AES
  876. #define BUILD_AES
  877. #endif
  878. #if defined(BUILD_TLS_RSA_WITH_AES_128_GCM_SHA256) || \
  879. defined(BUILD_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256) || \
  880. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) || \
  881. defined(BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) || \
  882. defined(BUILD_TLS_PSK_WITH_AES_128_GCM_SHA256) || \
  883. defined(BUILD_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256) || \
  884. defined(BUILD_TLS_RSA_WITH_AES_256_GCM_SHA384) || \
  885. defined(BUILD_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384) || \
  886. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) || \
  887. defined(BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) || \
  888. defined(BUILD_TLS_PSK_WITH_AES_256_GCM_SHA384) || \
  889. defined(BUILD_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384) || \
  890. defined(BUILD_TLS_AES_128_GCM_SHA256) || \
  891. defined(BUILD_TLS_AES_256_GCM_SHA384)
  892. #define BUILD_AESGCM
  893. #else
  894. /* No AES-GCM cipher suites available with build */
  895. #define NO_AESGCM_AEAD
  896. #endif
  897. #if defined(BUILD_TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256) || \
  898. defined(BUILD_TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384)
  899. #define BUILD_ARIA
  900. #endif
  901. #if defined(BUILD_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256) || \
  902. defined(BUILD_TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256) || \
  903. defined(BUILD_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256) || \
  904. defined(BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256) || \
  905. defined(BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256) || \
  906. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) || \
  907. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256) || \
  908. defined(BUILD_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256) || \
  909. defined(BUILD_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256) || \
  910. defined(BUILD_TLS_CHACHA20_POLY1305_SHA256)
  911. /* Have an available ChaCha Poly cipher suite */
  912. #else
  913. /* No ChaCha Poly cipher suites available with build */
  914. #define NO_CHAPOL_AEAD
  915. #endif
  916. #ifdef NO_DES3
  917. #define DES_BLOCK_SIZE 8
  918. #else
  919. #undef BUILD_DES3
  920. #define BUILD_DES3
  921. #endif
  922. #if defined(NO_AES) || !defined(HAVE_AES_DECRYPT)
  923. #define AES_BLOCK_SIZE 16
  924. #undef BUILD_AES
  925. #else
  926. #undef BUILD_AES
  927. #define BUILD_AES
  928. #endif
  929. #if !defined(NO_RC4) && !defined(WSSL_HARDEN_TLS)
  930. /* MUST NOT negotiate RC4 cipher suites
  931. * https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
  932. #undef BUILD_ARC4
  933. #define BUILD_ARC4
  934. #endif
  935. #ifdef HAVE_CHACHA
  936. #define CHACHA20_BLOCK_SIZE 16
  937. #endif
  938. #if defined(WOLFSSL_MAX_STRENGTH) || \
  939. (defined(HAVE_AESGCM) && !defined(NO_AESGCM_AEAD)) || \
  940. defined(HAVE_AESCCM) || \
  941. defined(HAVE_ARIA) || \
  942. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && \
  943. !defined(NO_CHAPOL_AEAD)) || \
  944. defined(WOLFSSL_SM4_GCM) || defined(WOLFSSL_SM4_CCM) || \
  945. (defined(WOLFSSL_TLS13) && defined(HAVE_NULL_CIPHER))
  946. #define HAVE_AEAD
  947. #endif
  948. #if defined(WOLFSSL_MAX_STRENGTH) || \
  949. defined(HAVE_ECC) || !defined(NO_DH)
  950. #define HAVE_PFS
  951. #endif
  952. #ifdef WSSL_HARDEN_TLS
  953. #ifdef HAVE_NULL_CIPHER
  954. #error "NULL ciphers not allowed https://www.rfc-editor.org/rfc/rfc9325#section-4.1"
  955. #endif
  956. #ifdef WOLFSSL_STATIC_RSA
  957. #error "Static RSA ciphers not allowed https://www.rfc-editor.org/rfc/rfc9325#section-4.1"
  958. #endif
  959. #ifdef WOLFSSL_STATIC_DH
  960. #error "Static DH ciphers not allowed https://www.rfc-editor.org/rfc/rfc9325#section-4.1"
  961. #endif
  962. #ifdef HAVE_ANON
  963. #error "At least the server side has to be authenticated"
  964. #endif
  965. #endif
  966. #undef WSSL_HARDEN_TLS
  967. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA) || defined(HAVE_LIGHTY)
  968. #define SSL_CA_NAMES(ssl) ((ssl)->client_ca_names != NULL ? (ssl)->client_ca_names : \
  969. (ssl)->ctx->client_ca_names)
  970. #else
  971. #define WOLFSSL_NO_CA_NAMES
  972. #endif
  973. /* actual cipher values, 2nd byte */
  974. enum {
  975. TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA = 0x16,
  976. TLS_DHE_RSA_WITH_AES_256_CBC_SHA = 0x39,
  977. TLS_DHE_RSA_WITH_AES_128_CBC_SHA = 0x33,
  978. TLS_DH_anon_WITH_AES_128_CBC_SHA = 0x34,
  979. TLS_RSA_WITH_AES_256_CBC_SHA = 0x35,
  980. TLS_RSA_WITH_AES_128_CBC_SHA = 0x2F,
  981. TLS_RSA_WITH_NULL_MD5 = 0x01,
  982. TLS_RSA_WITH_NULL_SHA = 0x02,
  983. TLS_PSK_WITH_AES_256_CBC_SHA = 0x8d,
  984. TLS_PSK_WITH_AES_128_CBC_SHA256 = 0xae,
  985. TLS_PSK_WITH_AES_256_CBC_SHA384 = 0xaf,
  986. TLS_PSK_WITH_AES_128_CBC_SHA = 0x8c,
  987. TLS_PSK_WITH_NULL_SHA256 = 0xb0,
  988. TLS_PSK_WITH_NULL_SHA384 = 0xb1,
  989. TLS_PSK_WITH_NULL_SHA = 0x2c,
  990. SSL_RSA_WITH_RC4_128_SHA = 0x05,
  991. SSL_RSA_WITH_RC4_128_MD5 = 0x04,
  992. SSL_RSA_WITH_3DES_EDE_CBC_SHA = 0x0A,
  993. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  994. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA = 0x14,
  995. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA = 0x13,
  996. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA = 0x0A,
  997. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA = 0x09,
  998. TLS_ECDHE_RSA_WITH_RC4_128_SHA = 0x11,
  999. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA = 0x07,
  1000. TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA = 0x12,
  1001. TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA = 0x08,
  1002. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 = 0x27,
  1003. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 = 0x23,
  1004. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 = 0x28,
  1005. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 = 0x24,
  1006. TLS_ECDHE_ECDSA_WITH_NULL_SHA = 0x06,
  1007. TLS_ECDHE_PSK_WITH_NULL_SHA256 = 0x3a,
  1008. TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 = 0x37,
  1009. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  1010. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA = 0x0F,
  1011. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA = 0x0E,
  1012. TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA = 0x05,
  1013. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA = 0x04,
  1014. TLS_ECDH_RSA_WITH_RC4_128_SHA = 0x0C,
  1015. TLS_ECDH_ECDSA_WITH_RC4_128_SHA = 0x02,
  1016. TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA = 0x0D,
  1017. TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA = 0x03,
  1018. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 = 0x29,
  1019. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 = 0x25,
  1020. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 = 0x2A,
  1021. TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 = 0x26,
  1022. WDM_WITH_NULL_SHA256 = 0xFE, /* wolfSSL DTLS Multicast */
  1023. /* SHA256 */
  1024. TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 = 0x6b,
  1025. TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 = 0x67,
  1026. TLS_RSA_WITH_AES_256_CBC_SHA256 = 0x3d,
  1027. TLS_RSA_WITH_AES_128_CBC_SHA256 = 0x3c,
  1028. TLS_RSA_WITH_NULL_SHA256 = 0x3b,
  1029. TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 = 0xb2,
  1030. TLS_DHE_PSK_WITH_NULL_SHA256 = 0xb4,
  1031. /* SHA384 */
  1032. TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 = 0xb3,
  1033. TLS_DHE_PSK_WITH_NULL_SHA384 = 0xb5,
  1034. /* AES-GCM */
  1035. TLS_RSA_WITH_AES_128_GCM_SHA256 = 0x9c,
  1036. TLS_RSA_WITH_AES_256_GCM_SHA384 = 0x9d,
  1037. TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 = 0x9e,
  1038. TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 = 0x9f,
  1039. TLS_DH_anon_WITH_AES_256_GCM_SHA384 = 0xa7,
  1040. TLS_PSK_WITH_AES_128_GCM_SHA256 = 0xa8,
  1041. TLS_PSK_WITH_AES_256_GCM_SHA384 = 0xa9,
  1042. TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 = 0xaa,
  1043. TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 = 0xab,
  1044. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  1045. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 = 0x2b,
  1046. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 = 0x2c,
  1047. TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 = 0x2d,
  1048. TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 = 0x2e,
  1049. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 = 0x2f,
  1050. TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 = 0x30,
  1051. TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 = 0x31,
  1052. TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 = 0x32,
  1053. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  1054. * also, in some of the other AES-CCM suites
  1055. * there will be second byte number conflicts
  1056. * with non-ECC AES-GCM */
  1057. TLS_RSA_WITH_AES_128_CCM_8 = 0xa0,
  1058. TLS_RSA_WITH_AES_256_CCM_8 = 0xa1,
  1059. TLS_ECDHE_ECDSA_WITH_AES_128_CCM = 0xac,
  1060. TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 = 0xae,
  1061. TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 = 0xaf,
  1062. TLS_PSK_WITH_AES_128_CCM = 0xa4,
  1063. TLS_PSK_WITH_AES_256_CCM = 0xa5,
  1064. TLS_PSK_WITH_AES_128_CCM_8 = 0xa8,
  1065. TLS_PSK_WITH_AES_256_CCM_8 = 0xa9,
  1066. TLS_DHE_PSK_WITH_AES_128_CCM = 0xa6,
  1067. TLS_DHE_PSK_WITH_AES_256_CCM = 0xa7,
  1068. /* Camellia */
  1069. TLS_RSA_WITH_CAMELLIA_128_CBC_SHA = 0x41,
  1070. TLS_RSA_WITH_CAMELLIA_256_CBC_SHA = 0x84,
  1071. TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 = 0xba,
  1072. TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 = 0xc0,
  1073. TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA = 0x45,
  1074. TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA = 0x88,
  1075. TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 = 0xbe,
  1076. TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 = 0xc4,
  1077. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  1078. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 = 0xa8,
  1079. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 = 0xa9,
  1080. TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 = 0xaa,
  1081. TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 = 0xac,
  1082. TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 = 0xab,
  1083. TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 = 0xad,
  1084. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  1085. TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 = 0x13,
  1086. TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 = 0x14,
  1087. TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 = 0x15,
  1088. /* ECDHE_PSK RFC8442, first byte is 0xD0 (EDHE_PSK_BYTE) */
  1089. TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 = 0x01,
  1090. /* TLS v1.3 cipher suites */
  1091. TLS_AES_128_GCM_SHA256 = 0x01,
  1092. TLS_AES_256_GCM_SHA384 = 0x02,
  1093. TLS_CHACHA20_POLY1305_SHA256 = 0x03,
  1094. TLS_AES_128_CCM_SHA256 = 0x04,
  1095. TLS_AES_128_CCM_8_SHA256 = 0x05,
  1096. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1097. TLS_SHA256_SHA256 = 0xB4,
  1098. TLS_SHA384_SHA384 = 0xB5,
  1099. /* ARIA-GCM, first byte is 0xC0 (ECC_BYTE)
  1100. * See: https://www.rfc-editor.org/rfc/rfc6209.html#section-5
  1101. */
  1102. TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256 = 0x5c,
  1103. TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384 = 0x5d,
  1104. /* TLS v1.3 SM cipher suites - 0x00 (CIPHER_BYTE) is first byte */
  1105. TLS_SM4_GCM_SM3 = 0xC6,
  1106. TLS_SM4_CCM_SM3 = 0xC7,
  1107. /* TLS v1.2 SM cipher suites - 0xE0 (SM_BYTE) is first byte */
  1108. TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3 = 0x11,
  1109. TLS_ECDHE_ECDSA_WITH_SM4_GCM_SM3 = 0x51,
  1110. TLS_ECDHE_ECDSA_WITH_SM4_CCM_SM3 = 0x52,
  1111. /* Fallback SCSV (Signaling Cipher Suite Value) */
  1112. TLS_FALLBACK_SCSV = 0x56,
  1113. /* Renegotiation Indication Extension Special Suite */
  1114. TLS_EMPTY_RENEGOTIATION_INFO_SCSV = 0xff
  1115. };
  1116. #ifndef WOLFSSL_SESSION_TIMEOUT
  1117. #define WOLFSSL_SESSION_TIMEOUT 500
  1118. /* default session resumption cache timeout in seconds */
  1119. #endif
  1120. #ifndef WOLFSSL_DTLS_WINDOW_WORDS
  1121. #define WOLFSSL_DTLS_WINDOW_WORDS 2
  1122. #endif /* WOLFSSL_DTLS_WINDOW_WORDS */
  1123. #define DTLS_WORD_BITS (sizeof(word32) * CHAR_BIT)
  1124. #define DTLS_SEQ_BITS (WOLFSSL_DTLS_WINDOW_WORDS * DTLS_WORD_BITS)
  1125. #define DTLS_SEQ_SZ (sizeof(word32) * WOLFSSL_DTLS_WINDOW_WORDS)
  1126. #ifndef WOLFSSL_MULTICAST
  1127. #define WOLFSSL_DTLS_PEERSEQ_SZ 1
  1128. #else
  1129. #ifndef WOLFSSL_MULTICAST_PEERS
  1130. /* max allowed multicast group peers */
  1131. #define WOLFSSL_MULTICAST_PEERS 100
  1132. #endif
  1133. #define WOLFSSL_DTLS_PEERSEQ_SZ WOLFSSL_MULTICAST_PEERS
  1134. #endif /* WOLFSSL_MULTICAST */
  1135. #ifndef WOLFSSL_MAX_MTU
  1136. /* 1500 - 100 bytes to account for UDP and IP headers */
  1137. #define WOLFSSL_MAX_MTU 1400
  1138. #endif /* WOLFSSL_MAX_MTU */
  1139. #ifndef WOLFSSL_DTLS_MTU_ADDITIONAL_READ_BUFFER
  1140. #define WOLFSSL_DTLS_MTU_ADDITIONAL_READ_BUFFER 500
  1141. #endif /* WOLFSSL_DTLS_MTU_ADDITIONAL_READ_BUFFER */
  1142. #ifndef WOLFSSL_DTLS_FRAG_POOL_SZ
  1143. #define WOLFSSL_DTLS_FRAG_POOL_SZ 10
  1144. #endif
  1145. /* set minimum DH key size allowed */
  1146. #ifndef WOLFSSL_MIN_DHKEY_BITS
  1147. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_HARDEN_TLS_NO_PKEY_CHECK)
  1148. /* Using guidance from section 5.6.1
  1149. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r5.pdf */
  1150. #if WOLFSSL_HARDEN_TLS >= 128
  1151. #define WOLFSSL_MIN_DHKEY_BITS 3072
  1152. #elif WOLFSSL_HARDEN_TLS >= 112
  1153. #define WOLFSSL_MIN_DHKEY_BITS 2048
  1154. #endif
  1155. #elif defined(WOLFSSL_MAX_STRENGTH)
  1156. #define WOLFSSL_MIN_DHKEY_BITS 2048
  1157. #else
  1158. #define WOLFSSL_MIN_DHKEY_BITS 1024
  1159. #endif
  1160. #endif
  1161. #if defined(WOLFSSL_HARDEN_TLS) && WOLFSSL_MIN_DHKEY_BITS < 2048 && \
  1162. !defined(WOLFSSL_HARDEN_TLS_NO_PKEY_CHECK)
  1163. /* Implementations MUST NOT negotiate cipher suites offering less than
  1164. * 112 bits of security.
  1165. * https://www.rfc-editor.org/rfc/rfc9325#section-4.1
  1166. * Using guidance from section 5.6.1
  1167. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r5.pdf */
  1168. #error "For 112 bits of security DH needs at least 2048 bit keys"
  1169. #endif
  1170. #if (WOLFSSL_MIN_DHKEY_BITS % 8)
  1171. #error DH minimum bit size must be multiple of 8
  1172. #endif
  1173. #if (WOLFSSL_MIN_DHKEY_BITS > 16000)
  1174. #error DH minimum bit size must not be greater than 16000
  1175. #endif
  1176. #define MIN_DHKEY_SZ (WOLFSSL_MIN_DHKEY_BITS / 8)
  1177. /* set maximum DH key size allowed */
  1178. #ifndef WOLFSSL_MAX_DHKEY_BITS
  1179. #if (defined(USE_FAST_MATH) && defined(FP_MAX_BITS) && FP_MAX_BITS >= 16384)
  1180. #define WOLFSSL_MAX_DHKEY_BITS (FP_MAX_BITS / 2)
  1181. #elif (defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)) && \
  1182. defined(SP_INT_BITS)
  1183. /* SP implementation supports numbers of SP_INT_BITS bits. */
  1184. #define WOLFSSL_MAX_DHKEY_BITS (((SP_INT_BITS + 7) / 8) * 8)
  1185. #else
  1186. #define WOLFSSL_MAX_DHKEY_BITS 4096
  1187. #endif
  1188. #endif
  1189. #if (WOLFSSL_MAX_DHKEY_BITS % 8)
  1190. #error DH maximum bit size must be multiple of 8
  1191. #endif
  1192. #if (WOLFSSL_MAX_DHKEY_BITS > 16384)
  1193. #error DH maximum bit size must not be greater than 16384
  1194. #endif
  1195. #define MAX_DHKEY_SZ (WOLFSSL_MAX_DHKEY_BITS / 8)
  1196. #ifndef NO_DH
  1197. #if WOLFSSL_MAX_DHKEY_BITS < WOLFSSL_MIN_DHKEY_BITS
  1198. #error "WOLFSSL_MAX_DHKEY_BITS has to be greater than WOLFSSL_MIN_DHKEY_BITS"
  1199. #endif
  1200. #endif /* NO_DH */
  1201. #ifndef MAX_PSK_ID_LEN
  1202. /* max psk identity/hint supported */
  1203. #if defined(WOLFSSL_TLS13)
  1204. /* OpenSSL has a 1472 byte session ticket */
  1205. #define MAX_PSK_ID_LEN 1536
  1206. #else
  1207. #define MAX_PSK_ID_LEN 128
  1208. #endif
  1209. #endif
  1210. #ifndef MAX_EARLY_DATA_SZ
  1211. /* maximum early data size */
  1212. #define MAX_EARLY_DATA_SZ 4096
  1213. #endif
  1214. #ifndef NO_RSA
  1215. #ifndef WOLFSSL_MAX_RSA_BITS
  1216. #ifdef USE_FAST_MATH
  1217. /* FP implementation support numbers up to FP_MAX_BITS / 2 bits. */
  1218. #define WOLFSSL_MAX_RSA_BITS (FP_MAX_BITS / 2)
  1219. #elif defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  1220. /* SP implementation supports numbers of SP_INT_BITS bits. */
  1221. #define WOLFSSL_MAX_RSA_BITS (((SP_INT_BITS + 7) / 8) * 8)
  1222. #else
  1223. /* Integer maths is dynamic but we only go up to 4096 bits. */
  1224. #define WOLFSSL_MAX_RSA_BITS 4096
  1225. #endif
  1226. #endif
  1227. #if (WOLFSSL_MAX_RSA_BITS % 8)
  1228. #error RSA maximum bit size must be multiple of 8
  1229. #endif
  1230. #endif
  1231. #if !defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC)
  1232. /* MySQL wants to be able to use 8192-bit numbers. */
  1233. #if defined(USE_FAST_MATH) && defined(FP_MAX_BITS)
  1234. /* Use the FP size up to 8192-bit and down to a min of 1024-bit. */
  1235. #if FP_MAX_BITS >= 16384
  1236. #define ENCRYPT_BASE_BITS 8192
  1237. #elif defined(HAVE_ECC)
  1238. #if FP_MAX_BITS > 2224
  1239. #define ENCRYPT_BASE_BITS (FP_MAX_BITS / 2)
  1240. #else
  1241. /* 521-bit ASN.1 signature - 3 + 2 * (2 + 66) bytes. */
  1242. #define ENCRYPT_BASE_BITS 1112
  1243. #endif
  1244. #else
  1245. #if FP_MAX_BITS > 2048
  1246. #define ENCRYPT_BASE_BITS (FP_MAX_BITS / 2)
  1247. #else
  1248. #define ENCRYPT_BASE_BITS 1024
  1249. #endif
  1250. #endif
  1251. /* Check MySQL size requirements met. */
  1252. #if defined(WOLFSSL_MYSQL_COMPATIBLE) && ENCRYPT_BASE_BITS < 8192
  1253. #error "MySQL needs FP_MAX_BITS at least at 16384"
  1254. #endif
  1255. #if !defined(NO_RSA) && defined(WOLFSSL_MAX_RSA_BITS) && \
  1256. WOLFSSL_MAX_RSA_BITS > ENCRYPT_BASE_BITS
  1257. #error "FP_MAX_BITS too small for WOLFSSL_MAX_RSA_BITS"
  1258. #endif
  1259. #elif defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  1260. /* Use the SP size up to 8192-bit and down to a min of 1024-bit. */
  1261. #if SP_INT_BITS >= 8192
  1262. #define ENCRYPT_BASE_BITS 8192
  1263. #elif defined(HAVE_ECC)
  1264. #if SP_INT_BITS > 1112
  1265. #define ENCRYPT_BASE_BITS SP_INT_BITS
  1266. #else
  1267. /* 521-bit ASN.1 signature - 3 + 2 * (2 + 66) bytes. */
  1268. #define ENCRYPT_BASE_BITS 1112
  1269. #endif
  1270. #else
  1271. #if SP_INT_BITS > 1024
  1272. #define ENCRYPT_BASE_BITS SP_INT_BITS
  1273. #else
  1274. #define ENCRYPT_BASE_BITS 1024
  1275. #endif
  1276. #endif
  1277. /* Check MySQL size requirements met. */
  1278. #if defined(WOLFSSL_MYSQL_COMPATIBLE) && ENCRYPT_BASE_BITS < 8192
  1279. #error "MySQL needs SP_INT_BITS at least at 8192"
  1280. #endif
  1281. #if !defined(NO_RSA) && defined(WOLFSSL_MAX_RSA_BITS) && \
  1282. WOLFSSL_MAX_RSA_BITS > SP_INT_BITS
  1283. #error "SP_INT_BITS too small for WOLFSSL_MAX_RSA_BITS"
  1284. #endif
  1285. #else
  1286. /* Integer/heap maths - support 4096-bit. */
  1287. #define ENCRYPT_BASE_BITS 4096
  1288. #endif
  1289. #elif defined(HAVE_CURVE448)
  1290. #define ENCRYPT_BASE_BITS (456 * 2)
  1291. #elif defined(HAVE_CURVE25519)
  1292. #define ENCRYPT_BASE_BITS (256 * 2)
  1293. #else
  1294. /* No secret from public key operation but PSK key plus length used. */
  1295. #define ENCRYPT_BASE_BITS ((MAX_PSK_ID_LEN + 2) * 8)
  1296. #endif
  1297. #ifdef WOLFSSL_DTLS_CID
  1298. #ifndef DTLS_CID_MAX_SIZE
  1299. /* DTLSv1.3 parsing code copies the record header in a static buffer to decrypt
  1300. * the record. Increasing the CID max size does increase also this buffer,
  1301. * impacting on per-session runtime memory footprint. */
  1302. #define DTLS_CID_MAX_SIZE 2
  1303. #endif
  1304. #else
  1305. #undef DTLS_CID_MAX_SIZE
  1306. #define DTLS_CID_MAX_SIZE 0
  1307. #endif /* WOLFSSL_DTLS_CID */
  1308. #if DTLS_CID_MAX_SIZE > 255
  1309. #error "Max size for DTLS CID is 255 bytes"
  1310. #endif
  1311. #ifndef MAX_TICKET_AGE_DIFF
  1312. /* maximum ticket age difference in seconds, 10 seconds */
  1313. #define MAX_TICKET_AGE_DIFF 10
  1314. #endif
  1315. #ifndef TLS13_MAX_TICKET_AGE
  1316. /* max ticket age in seconds, 7 days */
  1317. #define TLS13_MAX_TICKET_AGE (7*24*60*60)
  1318. #endif
  1319. /* Limit is 2^24.5
  1320. * https://www.rfc-editor.org/rfc/rfc8446#section-5.5
  1321. * Without the fraction is 23726566 (0x016A09E6) */
  1322. #define AEAD_AES_LIMIT w64From32(0x016A, 0x09E6)
  1323. /* Limit is 2^23
  1324. * https://www.rfc-editor.org/rfc/rfc9147.html#name-integrity-limits */
  1325. #define DTLS_AEAD_AES_CCM_LIMIT w64From32(0, 1 << 22)
  1326. /* Limit is 2^36
  1327. * https://www.rfc-editor.org/rfc/rfc9147.html#name-aead-limits */
  1328. #define DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT w64From32(1 << 3, 0)
  1329. #define DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT w64From32(1 << 2, 0)
  1330. /* Limit is 2^7
  1331. * https://www.rfc-editor.org/rfc/rfc9147.html#name-limits-for-aead_aes_128_ccm */
  1332. #define DTLS_AEAD_AES_CCM_8_FAIL_LIMIT w64From32(0, 1 << 6)
  1333. #define DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT w64From32(0, 1 << 5)
  1334. /* Limit is 2^23.5.
  1335. * https://www.rfc-editor.org/rfc/rfc9147.html#name-integrity-limits
  1336. * Without the fraction is 11863283 (0x00B504F3)
  1337. * Half of this value is 5931641 (0x005A8279) */
  1338. #define DTLS_AEAD_AES_CCM_FAIL_LIMIT w64From32(0x00B5, 0x04F3)
  1339. #define DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT w64From32(0x005A, 0x8279)
  1340. /* Limit is (2^22 - 1) full messages [2^36 - 31 octets]
  1341. * https://www.rfc-editor.org/rfc/rfc8998.html#name-aead_sm4_gcm
  1342. */
  1343. #define AEAD_SM4_GCM_LIMIT w64From32(0, (1 << 22) - 1)
  1344. /* Limit is (2^10 - 1) full messages [2^24 - 1 octets]
  1345. * https://www.rfc-editor.org/rfc/rfc8998.html#name-aead_sm4_ccm
  1346. */
  1347. #define AEAD_SM4_CCM_LIMIT w64From32(0, (1 << 10) - 1)
  1348. #if defined(WOLFSSL_TLS13) || !defined(NO_PSK)
  1349. #define TLS13_TICKET_NONCE_MAX_SZ 255
  1350. #if (defined(HAVE_FIPS) && \
  1351. !(defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))) && \
  1352. defined(TLS13_TICKET_NONCE_STATIC_SZ)
  1353. #error "TLS13_TICKET_NONCE_STATIC_SZ is not supported in this FIPS version"
  1354. #endif
  1355. #ifndef TLS13_TICKET_NONCE_STATIC_SZ
  1356. #define TLS13_TICKET_NONCE_STATIC_SZ 8
  1357. #endif
  1358. #if TLS13_TICKET_NONCE_STATIC_SZ > TLS13_TICKET_NONCE_MAX_SZ
  1359. #error "Max size for ticket nonce is 255 bytes"
  1360. #endif
  1361. #endif /* WOLFSSL_TLS13 || !NO_PSK */
  1362. #ifdef WOLFSSL_TLS13
  1363. /* The length of the certificate verification label - client and server. */
  1364. #define CERT_VFY_LABEL_SZ 34
  1365. /* The number of prefix bytes for signature data. */
  1366. #define SIGNING_DATA_PREFIX_SZ 64
  1367. /* Maximum length of the signature data. */
  1368. #define MAX_SIG_DATA_SZ (SIGNING_DATA_PREFIX_SZ + \
  1369. CERT_VFY_LABEL_SZ + \
  1370. WC_MAX_DIGEST_SIZE)
  1371. #endif /* WOLFSSL_TLS13 */
  1372. enum Misc {
  1373. CIPHER_BYTE = 0x00, /* Default ciphers */
  1374. ECC_BYTE = 0xC0, /* ECC first cipher suite byte */
  1375. CHACHA_BYTE = 0xCC, /* ChaCha first cipher suite */
  1376. TLS13_BYTE = 0x13, /* TLS v1.3 first byte of cipher suite */
  1377. ECDHE_PSK_BYTE = 0xD0, /* RFC 8442 */
  1378. SM_BYTE = 0xE0, /* SM first byte - private range */
  1379. SEND_CERT = 1,
  1380. SEND_BLANK_CERT = 2,
  1381. DTLS_MAJOR = 0xfe, /* DTLS major version number */
  1382. DTLS_MINOR = 0xff, /* DTLS minor version number */
  1383. DTLS_BOGUS_MINOR = 0xfe, /* DTLS 0xfe was skipped, see RFC6347 Sec. 1 */
  1384. DTLSv1_2_MINOR = 0xfd, /* DTLS minor version number */
  1385. DTLSv1_3_MINOR = 0xfc, /* DTLS minor version number */
  1386. SSLv3_MAJOR = 3, /* SSLv3 and TLSv1+ major version number */
  1387. SSLv3_MINOR = 0, /* TLSv1 minor version number */
  1388. TLSv1_MINOR = 1, /* TLSv1 minor version number */
  1389. TLSv1_1_MINOR = 2, /* TLSv1_1 minor version number */
  1390. TLSv1_2_MINOR = 3, /* TLSv1_2 minor version number */
  1391. TLSv1_3_MINOR = 4, /* TLSv1_3 minor version number */
  1392. TLS_DRAFT_MAJOR = 0x7f, /* Draft TLS major version number */
  1393. OLD_HELLO_ID = 0x01, /* SSLv2 Client Hello Indicator */
  1394. INVALID_BYTE = 0xff, /* Used to initialize cipher specs values */
  1395. NO_COMPRESSION = 0,
  1396. ZLIB_COMPRESSION = 221, /* wolfSSL zlib compression */
  1397. HELLO_EXT_SIG_ALGO = 13, /* ID for the sig_algo hello extension */
  1398. HELLO_EXT_EXTMS = 0x0017, /* ID for the extended master secret ext */
  1399. SECRET_LEN = WOLFSSL_MAX_MASTER_KEY_LENGTH,
  1400. /* pre RSA and all master */
  1401. #if !defined(WOLFSSL_TLS13) || defined(WOLFSSL_32BIT_MILLI_TIME)
  1402. TIMESTAMP_LEN = 4, /* timestamp size in ticket */
  1403. #else
  1404. TIMESTAMP_LEN = 8, /* timestamp size in ticket */
  1405. #endif
  1406. #ifdef WOLFSSL_TLS13
  1407. AGEADD_LEN = 4, /* ageAdd size in ticket */
  1408. NAMEDGROUP_LEN = 2, /* namedGroup size in ticket */
  1409. #ifdef WOLFSSL_EARLY_DATA
  1410. MAXEARLYDATASZ_LEN = 4, /* maxEarlyDataSz size in ticket */
  1411. #endif
  1412. #endif
  1413. #ifdef HAVE_PQC
  1414. ENCRYPT_LEN = 4600, /* allow 4600 byte buffer for dilithium. */
  1415. #else
  1416. #ifndef NO_PSK
  1417. ENCRYPT_LEN = (ENCRYPT_BASE_BITS / 8) + MAX_PSK_ID_LEN + 2,
  1418. #else
  1419. ENCRYPT_LEN = (ENCRYPT_BASE_BITS / 8),
  1420. #endif
  1421. #endif
  1422. SIZEOF_SENDER = 4, /* clnt or srvr */
  1423. FINISHED_SZ = 36, /* WC_MD5_DIGEST_SIZE + WC_SHA_DIGEST_SIZE */
  1424. MAX_RECORD_SIZE = 16384, /* 2^14, max size by standard */
  1425. MAX_PLAINTEXT_SZ = (1 << 14), /* Max plaintext sz */
  1426. MAX_TLS_CIPHER_SZ = (1 << 14) + 2048, /* Max TLS encrypted data sz */
  1427. #ifdef WOLFSSL_TLS13
  1428. MAX_TLS13_PLAIN_SZ = (1 << 14) + 1, /* Max unencrypted data sz */
  1429. MAX_TLS13_ENC_SZ = (1 << 14) + 256, /* Max encrypted data sz */
  1430. #endif
  1431. MAX_MSG_EXTRA = 38 + WC_MAX_DIGEST_SIZE,
  1432. /* max added to msg, mac + pad from */
  1433. /* RECORD_HEADER_SZ + BLOCK_SZ (pad) + Max
  1434. digest sz + BLOC_SZ (iv) + pad byte (1) */
  1435. MAX_COMP_EXTRA = 1024, /* max compression extra */
  1436. MAX_MTU = WOLFSSL_MAX_MTU, /* max expected MTU */
  1437. MAX_UDP_SIZE = 8192 - 100, /* was MAX_MTU - 100 */
  1438. MAX_DH_SZ = (MAX_DHKEY_SZ * 3) + 12, /* DH_P, DH_G and DH_Pub */
  1439. /* 4096 p, pub, g + 2 byte size for each */
  1440. MAX_STR_VERSION = 8, /* string rep of protocol version */
  1441. PAD_MD5 = 48, /* pad length for finished */
  1442. PAD_SHA = 40, /* pad length for finished */
  1443. MAX_PAD_SIZE = 256, /* maximum length of padding */
  1444. LENGTH_SZ = 2, /* length field for HMAC, data only */
  1445. VERSION_SZ = 2, /* length of proctocol version */
  1446. SEQ_SZ = 8, /* 64 bit sequence number */
  1447. ALERT_SIZE = 2, /* level + description */
  1448. VERIFY_HEADER = 2, /* always use 2 bytes */
  1449. EXTS_SZ = 2, /* always use 2 bytes */
  1450. EXT_ID_SZ = 2, /* always use 2 bytes */
  1451. MAX_DH_SIZE = MAX_DHKEY_SZ+1,
  1452. /* Max size plus possible leading 0 */
  1453. MIN_FFHDE_GROUP = 0x100, /* Named group minimum for FFDHE parameters */
  1454. MAX_FFHDE_GROUP = 0x1FF, /* Named group maximum for FFDHE parameters */
  1455. SESSION_HINT_SZ = 4, /* session timeout hint */
  1456. SESSION_ADD_SZ = 4, /* session age add */
  1457. TICKET_NONCE_LEN_SZ = 1, /* Ticket nonce length size */
  1458. DEF_TICKET_NONCE_SZ = 1, /* Default ticket nonce size */
  1459. #if defined(WOLFSSL_TLS13) || !defined(NO_PSK)
  1460. MAX_TICKET_NONCE_STATIC_SZ = TLS13_TICKET_NONCE_STATIC_SZ,
  1461. /* maximum ticket nonce static size */
  1462. #endif /* WOLFSSL_TLS13 || !NO_PSK */
  1463. MAX_LIFETIME = 604800, /* maximum ticket lifetime */
  1464. RAN_LEN = 32, /* random length */
  1465. SEED_LEN = RAN_LEN * 2, /* tls prf seed length */
  1466. ID_LEN = 32, /* session id length */
  1467. COOKIE_SECRET_SZ = 14, /* dtls cookie secret size */
  1468. MAX_COOKIE_LEN = 32, /* max dtls cookie size */
  1469. COOKIE_SZ = 20, /* use a 20 byte cookie */
  1470. SUITE_LEN = 2, /* cipher suite sz length */
  1471. ENUM_LEN = 1, /* always a byte */
  1472. OPAQUE8_LEN = 1, /* 1 byte */
  1473. OPAQUE16_LEN = 2, /* 2 bytes */
  1474. OPAQUE24_LEN = 3, /* 3 bytes */
  1475. OPAQUE32_LEN = 4, /* 4 bytes */
  1476. OPAQUE64_LEN = 8, /* 8 bytes */
  1477. COMP_LEN = 1, /* compression length */
  1478. CURVE_LEN = 2, /* ecc named curve length */
  1479. KE_GROUP_LEN = 2, /* key exchange group length */
  1480. #if defined(NO_SHA) && !defined(NO_SHA256)
  1481. SERVER_ID_LEN = WC_SHA256_DIGEST_SIZE,
  1482. #else
  1483. SERVER_ID_LEN = WC_SHA_DIGEST_SIZE,
  1484. #endif
  1485. HANDSHAKE_HEADER_SZ = 4, /* type + length(3) */
  1486. RECORD_HEADER_SZ = 5, /* type + version + len(2) */
  1487. CERT_HEADER_SZ = 3, /* always 3 bytes */
  1488. REQ_HEADER_SZ = 2, /* cert request header sz */
  1489. HINT_LEN_SZ = 2, /* length of hint size field */
  1490. TRUNCATED_HMAC_SZ = 10, /* length of hmac w/ truncated hmac extension */
  1491. HELLO_EXT_SZ = 4, /* base length of a hello extension */
  1492. HELLO_EXT_TYPE_SZ = 2, /* length of a hello extension type */
  1493. HELLO_EXT_SZ_SZ = 2, /* length of a hello extension size */
  1494. HELLO_EXT_SIGALGO_SZ = 2, /* length of number of items in sigalgo list */
  1495. DTLS_HANDSHAKE_HEADER_SZ = 12, /* normal + seq(2) + offset(3) + length(3) */
  1496. DTLS_RECORD_HEADER_SZ = 13, /* normal + epoch(2) + seq_num(6) */
  1497. DTLS_UNIFIED_HEADER_MIN_SZ = 2,
  1498. /* flags + seq_number(2) + length(2) + CID */
  1499. DTLS_RECVD_RL_HEADER_MAX_SZ = 5 + DTLS_CID_MAX_SIZE,
  1500. DTLS_RECORD_HEADER_MAX_SZ = 13,
  1501. DTLS_HANDSHAKE_EXTRA = 8, /* diff from normal */
  1502. DTLS_RECORD_EXTRA = 8, /* diff from normal */
  1503. DTLS_HANDSHAKE_SEQ_SZ = 2, /* handshake header sequence number */
  1504. DTLS_HANDSHAKE_FRAG_SZ = 3, /* fragment offset and length are 24 bit */
  1505. DTLS_POOL_SZ = 20, /* allowed number of list items in TX and
  1506. * RX pool */
  1507. DTLS_FRAG_POOL_SZ = WOLFSSL_DTLS_FRAG_POOL_SZ,
  1508. /* allowed number of fragments per msg */
  1509. DTLS_EXPORT_PRO = 165,/* wolfSSL protocol for serialized session */
  1510. DTLS_EXPORT_STATE_PRO = 166,/* wolfSSL protocol for serialized state */
  1511. TLS_EXPORT_PRO = 167,/* wolfSSL protocol for serialized TLS */
  1512. DTLS_EXPORT_OPT_SZ = 61, /* amount of bytes used from Options */
  1513. TLS_EXPORT_OPT_SZ = 65, /* amount of bytes used from Options */
  1514. DTLS_EXPORT_OPT_SZ_3 = 60, /* amount of bytes used from Options */
  1515. DTLS_EXPORT_KEY_SZ = 325 + (DTLS_SEQ_SZ * 2),
  1516. /* max amount of bytes used from Keys */
  1517. DTLS_EXPORT_MIN_KEY_SZ = 85 + (DTLS_SEQ_SZ * 2),
  1518. /* min amount of bytes used from Keys */
  1519. WOLFSSL_EXPORT_TLS = 1,
  1520. WOLFSSL_EXPORT_DTLS = 0,
  1521. #ifndef WOLFSSL_EXPORT_SPC_SZ
  1522. WOLFSSL_EXPORT_SPC_SZ = 16, /* amount of bytes used from CipherSpecs */
  1523. #endif
  1524. WOLFSSL_EXPORT_LEN = 2, /* 2 bytes for length and protocol */
  1525. WOLFSSL_EXPORT_VERSION = 4, /* wolfSSL version for serialized session */
  1526. /* older export versions supported */
  1527. WOLFSSL_EXPORT_VERSION_3 = 3, /* wolfSSL version before TLS 1.3 addition */
  1528. MAX_EXPORT_IP = 46, /* max ip size IPv4 mapped IPv6 */
  1529. DTLS_MTU_ADDITIONAL_READ_BUFFER = WOLFSSL_DTLS_MTU_ADDITIONAL_READ_BUFFER,
  1530. /* Additional bytes to read so that
  1531. * we can work with a peer that has
  1532. * a slightly different MTU than us. */
  1533. MAX_EXPORT_BUFFER = 514, /* max size of buffer for exporting */
  1534. MAX_EXPORT_STATE_BUFFER = (DTLS_EXPORT_MIN_KEY_SZ) + (3 * WOLFSSL_EXPORT_LEN),
  1535. /* max size of buffer for exporting state */
  1536. FINISHED_LABEL_SZ = 15, /* TLS finished label size */
  1537. TLS_FINISHED_SZ = 12, /* TLS has a shorter size */
  1538. TLS_FINISHED_SZ_MAX = WC_MAX_DIGEST_SIZE,
  1539. /* longest message digest size is SHA512, 64 */
  1540. EXT_MASTER_LABEL_SZ = 22, /* TLS extended master secret label sz */
  1541. MASTER_LABEL_SZ = 13, /* TLS master secret label sz */
  1542. KEY_LABEL_SZ = 13, /* TLS key block expansion sz */
  1543. PROTOCOL_LABEL_SZ = 9, /* Length of the protocol label */
  1544. MAX_LABEL_SZ = 34, /* Maximum length of a label */
  1545. MAX_REQUEST_SZ = 256, /* Maximum cert req len (no auth yet */
  1546. SESSION_FLUSH_COUNT = 256, /* Flush session cache unless user turns off */
  1547. TLS_MAX_PAD_SZ = 255, /* Max padding in TLS */
  1548. #if defined(HAVE_NULL_CIPHER) && defined(WOLFSSL_TLS13)
  1549. #if defined(WOLFSSL_SHA384) && WC_MAX_SYM_KEY_SIZE < 48
  1550. MAX_SYM_KEY_SIZE = WC_SHA384_DIGEST_SIZE,
  1551. #elif !defined(NO_SHA256) && WC_MAX_SYM_KEY_SIZE < 32
  1552. MAX_SYM_KEY_SIZE = WC_SHA256_DIGEST_SIZE,
  1553. #else
  1554. MAX_SYM_KEY_SIZE = WC_MAX_SYM_KEY_SIZE,
  1555. #endif
  1556. #else
  1557. MAX_SYM_KEY_SIZE = WC_MAX_SYM_KEY_SIZE,
  1558. #endif
  1559. #if defined(HAVE_SELFTEST) && \
  1560. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  1561. #ifndef WOLFSSL_AES_KEY_SIZE_ENUM
  1562. #define WOLFSSL_AES_KEY_SIZE_ENUM
  1563. AES_IV_SIZE = 16,
  1564. AES_128_KEY_SIZE = 16,
  1565. AES_192_KEY_SIZE = 24,
  1566. AES_256_KEY_SIZE = 32,
  1567. #endif
  1568. #endif
  1569. MAX_IV_SZ = AES_BLOCK_SIZE,
  1570. AEAD_SEQ_OFFSET = 4, /* Auth Data: Sequence number */
  1571. AEAD_TYPE_OFFSET = 8, /* Auth Data: Type */
  1572. AEAD_VMAJ_OFFSET = 9, /* Auth Data: Major Version */
  1573. AEAD_VMIN_OFFSET = 10, /* Auth Data: Minor Version */
  1574. AEAD_LEN_OFFSET = 11, /* Auth Data: Length */
  1575. AEAD_AUTH_DATA_SZ = 13, /* Size of the data to authenticate */
  1576. AEAD_NONCE_SZ = 12,
  1577. AESGCM_IMP_IV_SZ = 4, /* Size of GCM/CCM AEAD implicit IV */
  1578. AESGCM_EXP_IV_SZ = 8, /* Size of GCM/CCM AEAD explicit IV */
  1579. AESGCM_NONCE_SZ = AESGCM_EXP_IV_SZ + AESGCM_IMP_IV_SZ,
  1580. GCM_IMP_IV_SZ = 4, /* Size of GCM/CCM AEAD implicit IV */
  1581. GCM_EXP_IV_SZ = 8, /* Size of GCM/CCM AEAD explicit IV */
  1582. GCM_NONCE_SZ = GCM_EXP_IV_SZ + GCM_IMP_IV_SZ,
  1583. CHACHA20_IMP_IV_SZ = 12, /* Size of ChaCha20 AEAD implicit IV */
  1584. CHACHA20_NONCE_SZ = 12, /* Size of ChacCha20 nonce */
  1585. CHACHA20_OLD_OFFSET = 4, /* Offset for seq # in old poly1305 */
  1586. /* For any new implicit/explicit IV size adjust AEAD_MAX_***_SZ */
  1587. AES_GCM_AUTH_SZ = 16, /* AES-GCM Auth Tag length */
  1588. AES_CCM_16_AUTH_SZ = 16, /* AES-CCM-16 Auth Tag length */
  1589. AES_CCM_8_AUTH_SZ = 8, /* AES-CCM-8 Auth Tag Length */
  1590. AESCCM_NONCE_SZ = 12,
  1591. SM4_GCM_AUTH_SZ = 16, /* SM4-GCM Auth Tag length */
  1592. SM4_GCM_NONCE_SZ = 12, /* SM4 GCM Nonce length */
  1593. SM4_CCM_AUTH_SZ = 16, /* SM4-CCM Auth Tag length */
  1594. SM4_CCM_NONCE_SZ = 12, /* SM4 CCM Nonce length */
  1595. CAMELLIA_128_KEY_SIZE = 16, /* for 128 bit */
  1596. CAMELLIA_192_KEY_SIZE = 24, /* for 192 bit */
  1597. CAMELLIA_256_KEY_SIZE = 32, /* for 256 bit */
  1598. CAMELLIA_IV_SIZE = 16, /* always block size */
  1599. CHACHA20_256_KEY_SIZE = 32, /* for 256 bit */
  1600. CHACHA20_128_KEY_SIZE = 16, /* for 128 bit */
  1601. CHACHA20_IV_SIZE = 12, /* 96 bits for iv */
  1602. POLY1305_AUTH_SZ = 16, /* 128 bits */
  1603. HMAC_NONCE_SZ = 12, /* Size of HMAC nonce */
  1604. EVP_SALT_SIZE = 8, /* evp salt size 64 bits */
  1605. #ifndef ECDHE_SIZE /* allow this to be overridden at compile-time */
  1606. ECDHE_SIZE = 32, /* ECDHE server size defaults to 256 bit */
  1607. #endif
  1608. MAX_EXPORT_ECC_SZ = 256, /* Export ANS X9.62 max future size */
  1609. MAX_CURVE_NAME_SZ = 16, /* Maximum size of curve name string */
  1610. NEW_SA_MAJOR = 8, /* Most significant byte used with new sig algos */
  1611. ED25519_SA_MAJOR = 8, /* Most significant byte for ED25519 */
  1612. ED25519_SA_MINOR = 7, /* Least significant byte for ED25519 */
  1613. ED448_SA_MAJOR = 8, /* Most significant byte for ED448 */
  1614. ED448_SA_MINOR = 8, /* Least significant byte for ED448 */
  1615. SM2_SA_MAJOR = 7, /* Most significant byte for SM2 with SM3 */
  1616. SM2_SA_MINOR = 8, /* Least significant byte for SM2 with SM3 */
  1617. PQC_SA_MAJOR = 0xFE,/* Most significant byte used with PQC sig algs */
  1618. /* These values for falcon and dilithium match what OQS has defined in their OpenSSL fork. */
  1619. FALCON_LEVEL1_SA_MAJOR = 0xFE,
  1620. FALCON_LEVEL1_SA_MINOR = 0x0B,
  1621. FALCON_LEVEL5_SA_MAJOR = 0xFE,
  1622. FALCON_LEVEL5_SA_MINOR = 0x0E,
  1623. DILITHIUM_LEVEL2_SA_MAJOR = 0xFE,
  1624. DILITHIUM_LEVEL2_SA_MINOR = 0xA0,
  1625. DILITHIUM_LEVEL3_SA_MAJOR = 0xFE,
  1626. DILITHIUM_LEVEL3_SA_MINOR = 0xA3,
  1627. DILITHIUM_LEVEL5_SA_MAJOR = 0xFE,
  1628. DILITHIUM_LEVEL5_SA_MINOR = 0xA5,
  1629. MIN_RSA_SHA512_PSS_BITS = 512 * 2 + 8 * 8, /* Min key size */
  1630. MIN_RSA_SHA384_PSS_BITS = 384 * 2 + 8 * 8, /* Min key size */
  1631. #if defined(HAVE_PQC)
  1632. MAX_CERT_VERIFY_SZ = 6000, /* For Dilithium */
  1633. #elif !defined(NO_RSA) && defined(WOLFSSL_MAX_RSA_BITS)
  1634. MAX_CERT_VERIFY_SZ = WOLFSSL_MAX_RSA_BITS / 8, /* max RSA bytes */
  1635. #elif defined(HAVE_ECC)
  1636. MAX_CERT_VERIFY_SZ = ECC_MAX_SIG_SIZE, /* max ECC */
  1637. #elif defined(HAVE_ED448)
  1638. MAX_CERT_VERIFY_SZ = ED448_SIG_SIZE, /* max Ed448 */
  1639. #elif defined(HAVE_ED25519)
  1640. MAX_CERT_VERIFY_SZ = ED25519_SIG_SIZE, /* max Ed25519 */
  1641. #else
  1642. MAX_CERT_VERIFY_SZ = 1024, /* max default */
  1643. #endif
  1644. CLIENT_HELLO_FIRST = 35, /* Protocol + RAN_LEN + sizeof(id_len) */
  1645. MAX_SUITE_NAME = 48, /* maximum length of cipher suite string */
  1646. DTLS_TIMEOUT_INIT = 1, /* default timeout init for DTLS receive */
  1647. DTLS_TIMEOUT_MAX = 64, /* default max timeout for DTLS receive */
  1648. DTLS_TIMEOUT_MULTIPLIER = 2, /* default timeout multiplier for DTLS recv */
  1649. NULL_TERM_LEN = 1, /* length of null '\0' termination character */
  1650. MAX_PSK_KEY_LEN = 64, /* max psk key supported */
  1651. MIN_PSK_ID_LEN = 6, /* min length of identities */
  1652. MIN_PSK_BINDERS_LEN = 33, /* min length of binders */
  1653. #ifndef MAX_WOLFSSL_FILE_SIZE
  1654. MAX_WOLFSSL_FILE_SIZE = 1024UL * 1024UL * 4, /* 4 mb file size alloc limit */
  1655. #endif
  1656. CERT_MIN_SIZE = 256, /* min PEM cert size with header/footer */
  1657. NO_SNIFF = 0, /* not sniffing */
  1658. SNIFF = 1, /* currently sniffing */
  1659. HASH_SIG_SIZE = 2, /* default SHA1 RSA */
  1660. NO_COPY = 0, /* should we copy static buffer for write */
  1661. COPY = 1, /* should we copy static buffer for write */
  1662. INVALID_PEER_ID = 0xFFFF, /* Initialize value for peer ID. */
  1663. PREV_ORDER = -1, /* Sequence number is in previous epoch. */
  1664. PEER_ORDER = 1, /* Peer sequence number for verify. */
  1665. CUR_ORDER = 0, /* Current sequence number. */
  1666. WRITE_PROTO = 1, /* writing a protocol message */
  1667. READ_PROTO = 0 /* reading a protocol message */
  1668. };
  1669. #define WOLFSSL_NAMED_GROUP_IS_FFHDE(group) \
  1670. (MIN_FFHDE_GROUP <= (group) && (group) <= MAX_FFHDE_GROUP)
  1671. #ifdef HAVE_PQC
  1672. #define WOLFSSL_NAMED_GROUP_IS_PQC(group) \
  1673. (WOLFSSL_PQC_MIN <= (group) && (group) <= WOLFSSL_PQC_MAX)
  1674. #else
  1675. #define WOLFSSL_NAMED_GROUP_IS_PQC(group) ((void)(group), 0)
  1676. #endif /* HAVE_PQC */
  1677. /* minimum Downgrade Minor version */
  1678. #ifndef WOLFSSL_MIN_DOWNGRADE
  1679. #ifndef NO_OLD_TLS
  1680. #define WOLFSSL_MIN_DOWNGRADE TLSv1_MINOR
  1681. #else
  1682. #define WOLFSSL_MIN_DOWNGRADE TLSv1_2_MINOR
  1683. #endif
  1684. #endif
  1685. /* minimum DTLS Downgrade Minor version */
  1686. #ifndef WOLFSSL_MIN_DTLS_DOWNGRADE
  1687. #define WOLFSSL_MIN_DTLS_DOWNGRADE DTLS_MINOR;
  1688. #endif
  1689. /* Set max implicit IV size for AEAD cipher suites */
  1690. #define AEAD_MAX_IMP_SZ 12
  1691. /* Set max explicit IV size for AEAD cipher suites */
  1692. #define AEAD_MAX_EXP_SZ 8
  1693. #ifndef WOLFSSL_MAX_SUITE_SZ
  1694. #define WOLFSSL_MAX_SUITE_SZ 300
  1695. /* 150 suites for now! */
  1696. #endif
  1697. /* number of items in the signature algo list */
  1698. #ifndef WOLFSSL_MAX_SIGALGO
  1699. #ifdef HAVE_PQC
  1700. /* If we are building with post-quantum algorithms, we likely want to
  1701. * inter-op with OQS's OpenSSL and they send a lot more sigalgs.
  1702. */
  1703. #define WOLFSSL_MAX_SIGALGO 128
  1704. #else
  1705. #define WOLFSSL_MAX_SIGALGO 38
  1706. #endif
  1707. #endif
  1708. /* set minimum ECC key size allowed */
  1709. #ifndef WOLFSSL_MIN_ECC_BITS
  1710. #ifdef WOLFSSL_MAX_STRENGTH
  1711. #define WOLFSSL_MIN_ECC_BITS 256
  1712. #else
  1713. #define WOLFSSL_MIN_ECC_BITS 224
  1714. #endif
  1715. #endif /* WOLFSSL_MIN_ECC_BITS */
  1716. #if (WOLFSSL_MIN_ECC_BITS % 8)
  1717. /* Some ECC keys are not divisible by 8 such as prime239v1 or sect131r1.
  1718. In these cases round down to the nearest value divisible by 8. The
  1719. restriction of being divisible by 8 is in place to match wc_ecc_size
  1720. function from wolfSSL.
  1721. */
  1722. #error ECC minimum bit size must be a multiple of 8
  1723. #endif
  1724. #define MIN_ECCKEY_SZ (WOLFSSL_MIN_ECC_BITS / 8)
  1725. #ifdef HAVE_PQC
  1726. #ifndef MIN_FALCONKEY_SZ
  1727. #define MIN_FALCONKEY_SZ 897
  1728. #endif
  1729. #ifndef MIN_DILITHIUMKEY_SZ
  1730. #define MIN_DILITHIUMKEY_SZ 1312
  1731. #endif
  1732. #endif
  1733. /* set minimum RSA key size allowed */
  1734. #ifndef WOLFSSL_MIN_RSA_BITS
  1735. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_HARDEN_TLS_NO_PKEY_CHECK)
  1736. /* Using guidance from section 5.6.1
  1737. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r5.pdf */
  1738. #if WOLFSSL_HARDEN_TLS >= 128
  1739. #define WOLFSSL_MIN_RSA_BITS 3072
  1740. #elif WOLFSSL_HARDEN_TLS >= 112
  1741. #define WOLFSSL_MIN_RSA_BITS 2048
  1742. #endif
  1743. #elif defined(WOLFSSL_MAX_STRENGTH)
  1744. #define WOLFSSL_MIN_RSA_BITS 2048
  1745. #else
  1746. #define WOLFSSL_MIN_RSA_BITS 1024
  1747. #endif
  1748. #endif /* WOLFSSL_MIN_RSA_BITS */
  1749. #if defined(WOLFSSL_HARDEN_TLS) && WOLFSSL_MIN_RSA_BITS < 2048 && \
  1750. !defined(WOLFSSL_HARDEN_TLS_NO_PKEY_CHECK)
  1751. /* Implementations MUST NOT negotiate cipher suites offering less than
  1752. * 112 bits of security.
  1753. * https://www.rfc-editor.org/rfc/rfc9325#section-4.1
  1754. * Using guidance from section 5.6.1
  1755. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r5.pdf */
  1756. #error "For 112 bits of security RSA needs at least 2048 bit keys"
  1757. #endif
  1758. #if (WOLFSSL_MIN_RSA_BITS % 8)
  1759. /* This is to account for the example case of a min size of 2050 bits but
  1760. still allows 2049 bit key. So we need the measurement to be in bytes. */
  1761. #error RSA minimum bit size must be a multiple of 8
  1762. #endif
  1763. #define MIN_RSAKEY_SZ (WOLFSSL_MIN_RSA_BITS / 8)
  1764. #ifdef SESSION_INDEX
  1765. /* Shift values for making a session index */
  1766. #define SESSIDX_ROW_SHIFT 4
  1767. #define SESSIDX_IDX_MASK 0x0F
  1768. #endif
  1769. #ifndef MAX_X509_SIZE
  1770. #if defined(HAVE_PQC)
  1771. #define MAX_X509_SIZE (8*1024) /* max static x509 buffer size; dilithium is big */
  1772. #elif defined(WOLFSSL_HAPROXY)
  1773. #define MAX_X509_SIZE 3072 /* max static x509 buffer size */
  1774. #else
  1775. #define MAX_X509_SIZE 2048 /* max static x509 buffer size */
  1776. #endif
  1777. #endif
  1778. /* max cert chain peer depth */
  1779. #ifndef MAX_CHAIN_DEPTH
  1780. #define MAX_CHAIN_DEPTH 9
  1781. #endif
  1782. /* max size of a certificate message payload */
  1783. /* assumes MAX_CHAIN_DEPTH number of certificates at 2kb per certificate */
  1784. #ifndef MAX_CERTIFICATE_SZ
  1785. #define MAX_CERTIFICATE_SZ \
  1786. (CERT_HEADER_SZ + \
  1787. (MAX_X509_SIZE + CERT_HEADER_SZ) * MAX_CHAIN_DEPTH)
  1788. #endif
  1789. /* max size of a handshake message, currently set to the certificate */
  1790. #ifndef MAX_HANDSHAKE_SZ
  1791. #define MAX_HANDSHAKE_SZ MAX_CERTIFICATE_SZ
  1792. #endif
  1793. #ifndef PREALLOC_SESSION_TICKET_LEN
  1794. #define PREALLOC_SESSION_TICKET_LEN 512
  1795. #endif
  1796. #ifndef PREALLOC_SESSION_TICKET_NONCE_LEN
  1797. #define PREALLOC_SESSION_TICKET_NONCE_LEN 32
  1798. #endif
  1799. #ifndef SESSION_TICKET_HINT_DEFAULT
  1800. #define SESSION_TICKET_HINT_DEFAULT 300
  1801. #endif
  1802. #if !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_WOLFSSL_SERVER)
  1803. /* Check chosen encryption is available. */
  1804. #if !(defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) && \
  1805. defined(WOLFSSL_TICKET_ENC_CHACHA20_POLY1305)
  1806. #error "ChaCha20-Poly1305 not available for default ticket encryption"
  1807. #endif
  1808. #if !defined(HAVE_AESGCM) && (defined(WOLFSSL_TICKET_ENC_AES128_GCM) || \
  1809. defined(WOLFSSL_TICKET_ENC_AES256_GCM))
  1810. #error "AES-GCM not available for default ticket encryption"
  1811. #endif
  1812. #ifndef WOLFSSL_TICKET_KEY_LIFETIME
  1813. /* Default lifetime is 1 hour from issue of first ticket with key. */
  1814. #define WOLFSSL_TICKET_KEY_LIFETIME (60 * 60)
  1815. #endif
  1816. #if WOLFSSL_TICKET_KEY_LIFETIME <= SESSION_TICKET_HINT_DEFAULT
  1817. #error "Ticket Key lifetime must be longer than ticket life hint."
  1818. #endif
  1819. #endif
  1820. #define MAX_ENCRYPT_SZ ENCRYPT_LEN
  1821. /* A static check to assert a relation between x and y */
  1822. #define WOLFSSL_ASSERT_TEST(x, y, op) do { \
  1823. typedef char _args_test_[(x) op (y) ? 1 : -1]; \
  1824. (void)sizeof(_args_test_); \
  1825. } while(0)
  1826. #define WOLFSSL_ASSERT_EQ(x, y) WOLFSSL_ASSERT_TEST(x, y, ==)
  1827. #define WOLFSSL_ASSERT_SIZEOF_TEST(x, y, op) \
  1828. WOLFSSL_ASSERT_TEST(sizeof((x)), sizeof((y)), op)
  1829. #define WOLFSSL_ASSERT_SIZEOF_GE(x, y) WOLFSSL_ASSERT_SIZEOF_TEST(x, y, >=)
  1830. /* states. Adding state before HANDSHAKE_DONE will break session importing */
  1831. enum states {
  1832. NULL_STATE = 0,
  1833. SERVER_HELLOVERIFYREQUEST_COMPLETE,
  1834. SERVER_HELLO_RETRY_REQUEST_COMPLETE,
  1835. SERVER_HELLO_COMPLETE,
  1836. SERVER_ENCRYPTED_EXTENSIONS_COMPLETE,
  1837. SERVER_CERT_COMPLETE,
  1838. SERVER_CERT_VERIFY_COMPLETE,
  1839. SERVER_KEYEXCHANGE_COMPLETE,
  1840. SERVER_HELLODONE_COMPLETE,
  1841. SERVER_CHANGECIPHERSPEC_COMPLETE,
  1842. SERVER_FINISHED_COMPLETE,
  1843. CLIENT_HELLO_RETRY,
  1844. CLIENT_HELLO_COMPLETE,
  1845. CLIENT_KEYEXCHANGE_COMPLETE,
  1846. CLIENT_CHANGECIPHERSPEC_COMPLETE,
  1847. CLIENT_FINISHED_COMPLETE,
  1848. HANDSHAKE_DONE,
  1849. #ifdef WOLFSSL_DTLS13
  1850. SERVER_FINISHED_ACKED,
  1851. #endif /* WOLFSSL_DTLS13 */
  1852. };
  1853. /* SSL Version */
  1854. typedef struct ProtocolVersion {
  1855. byte major;
  1856. byte minor;
  1857. } WOLFSSL_PACK ProtocolVersion;
  1858. WOLFSSL_LOCAL ProtocolVersion MakeSSLv3(void);
  1859. WOLFSSL_LOCAL ProtocolVersion MakeTLSv1(void);
  1860. WOLFSSL_LOCAL ProtocolVersion MakeTLSv1_1(void);
  1861. WOLFSSL_LOCAL ProtocolVersion MakeTLSv1_2(void);
  1862. WOLFSSL_LOCAL ProtocolVersion MakeTLSv1_3(void);
  1863. #ifdef WOLFSSL_DTLS
  1864. WOLFSSL_LOCAL ProtocolVersion MakeDTLSv1(void);
  1865. WOLFSSL_LOCAL ProtocolVersion MakeDTLSv1_2(void);
  1866. #ifdef WOLFSSL_DTLS13
  1867. WOLFSSL_LOCAL ProtocolVersion MakeDTLSv1_3(void);
  1868. #endif /* WOLFSSL_DTLS13 */
  1869. #endif
  1870. #ifdef WOLFSSL_SESSION_EXPORT
  1871. WOLFSSL_LOCAL int wolfSSL_session_export_internal(WOLFSSL* ssl, byte* buf,
  1872. word32* sz, int type);
  1873. WOLFSSL_LOCAL int wolfSSL_session_import_internal(WOLFSSL* ssl, const byte* buf,
  1874. word32 sz, int type);
  1875. #ifdef WOLFSSL_DTLS
  1876. WOLFSSL_LOCAL int wolfSSL_dtls_export_state_internal(WOLFSSL* ssl,
  1877. byte* buf, word32 sz);
  1878. WOLFSSL_LOCAL int wolfSSL_dtls_import_state_internal(WOLFSSL* ssl,
  1879. const byte* buf, word32 sz);
  1880. WOLFSSL_LOCAL int wolfSSL_send_session(WOLFSSL* ssl);
  1881. #endif
  1882. #endif
  1883. struct WOLFSSL_BY_DIR_HASH {
  1884. unsigned long hash_value;
  1885. int last_suffix;
  1886. };
  1887. struct WOLFSSL_BY_DIR_entry {
  1888. char* dir_name;
  1889. int dir_type;
  1890. WOLF_STACK_OF(WOLFSSL_BY_DIR_HASH) *hashes;
  1891. };
  1892. struct WOLFSSL_BY_DIR {
  1893. WOLF_STACK_OF(WOLFSSL_BY_DIR_entry) *dir_entry;
  1894. wolfSSL_Mutex lock; /* dir list lock */
  1895. };
  1896. /* wolfSSL method type */
  1897. struct WOLFSSL_METHOD {
  1898. ProtocolVersion version;
  1899. byte side; /* connection side, server or client */
  1900. byte downgrade; /* whether to downgrade version, default no */
  1901. };
  1902. /* wolfSSL buffer type - internal uses "buffer" type */
  1903. typedef WOLFSSL_BUFFER_INFO buffer;
  1904. typedef struct Suites Suites;
  1905. /* Declare opaque struct for API to use */
  1906. #ifndef WOLFSSL_CLIENT_SESSION_DEFINED
  1907. typedef struct ClientSession ClientSession;
  1908. #define WOLFSSL_CLIENT_SESSION_DEFINED
  1909. #endif
  1910. /* defaults to client */
  1911. WOLFSSL_LOCAL void InitSSL_Method(WOLFSSL_METHOD* method, ProtocolVersion pv);
  1912. WOLFSSL_LOCAL void InitSSL_CTX_Suites(WOLFSSL_CTX* ctx);
  1913. WOLFSSL_LOCAL int InitSSL_Suites(WOLFSSL* ssl);
  1914. WOLFSSL_LOCAL int InitSSL_Side(WOLFSSL* ssl, word16 side);
  1915. WOLFSSL_LOCAL int DoHandShakeMsgType(WOLFSSL* ssl, byte* input,
  1916. word32* inOutIdx, byte type, word32 size, word32 totalSz);
  1917. /* for sniffer */
  1918. WOLFSSL_LOCAL int DoFinished(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
  1919. word32 size, word32 totalSz, int sniff);
  1920. #ifdef WOLFSSL_TLS13
  1921. WOLFSSL_LOCAL int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
  1922. word32 size, word32 totalSz, int sniff);
  1923. #endif
  1924. WOLFSSL_LOCAL int DoApplicationData(WOLFSSL* ssl, byte* input, word32* inOutIdx,
  1925. int sniff);
  1926. /* TLS v1.3 needs these */
  1927. WOLFSSL_LOCAL int HandleTlsResumption(WOLFSSL* ssl, Suites* clSuites);
  1928. #ifdef WOLFSSL_TLS13
  1929. WOLFSSL_LOCAL byte SuiteMac(const byte* suite);
  1930. #endif
  1931. WOLFSSL_LOCAL int DoClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
  1932. word32 helloSz);
  1933. #ifdef WOLFSSL_TLS13
  1934. WOLFSSL_LOCAL int DoTls13ClientHello(WOLFSSL* ssl, const byte* input,
  1935. word32* inOutIdx, word32 helloSz);
  1936. #endif
  1937. WOLFSSL_LOCAL int DoServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
  1938. word32 helloSz);
  1939. WOLFSSL_LOCAL int CompleteServerHello(WOLFSSL *ssl);
  1940. WOLFSSL_LOCAL int CheckVersion(WOLFSSL *ssl, ProtocolVersion pv);
  1941. WOLFSSL_LOCAL int PickHashSigAlgo(WOLFSSL* ssl, const byte* hashSigAlgo,
  1942. word32 hashSigAlgoSz);
  1943. #if defined(WOLF_PRIVATE_KEY_ID) && !defined(NO_CHECK_PRIVATE_KEY)
  1944. WOLFSSL_LOCAL int CreateDevPrivateKey(void** pkey, byte* data, word32 length,
  1945. int hsType, int label, int id,
  1946. void* heap, int devId);
  1947. #endif
  1948. WOLFSSL_LOCAL int DecodePrivateKey(WOLFSSL *ssl, word16* length);
  1949. #ifdef WOLF_PRIVATE_KEY_ID
  1950. WOLFSSL_LOCAL int GetPrivateKeySigSize(WOLFSSL* ssl);
  1951. #ifndef NO_ASN
  1952. WOLFSSL_LOCAL int InitSigPkCb(WOLFSSL* ssl, SignatureCtx* sigCtx);
  1953. #endif
  1954. #endif
  1955. WOLFSSL_LOCAL int CreateSigData(WOLFSSL* ssl, byte* sigData, word16* sigDataSz,
  1956. int check);
  1957. WOLFSSL_LOCAL int CreateRSAEncodedSig(byte* sig, byte* sigData, int sigDataSz,
  1958. int sigAlgo, int hashAlgo);
  1959. #ifdef WOLFSSL_ASYNC_IO
  1960. WOLFSSL_LOCAL void FreeAsyncCtx(WOLFSSL* ssl, byte freeAsync);
  1961. #endif
  1962. WOLFSSL_LOCAL void FreeKeyExchange(WOLFSSL* ssl);
  1963. WOLFSSL_LOCAL void FreeSuites(WOLFSSL* ssl);
  1964. WOLFSSL_LOCAL int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, word32 totalSz);
  1965. WOLFSSL_LOCAL int MatchDomainName(const char* pattern, int len, const char* str);
  1966. #ifndef NO_CERTS
  1967. WOLFSSL_LOCAL int CheckForAltNames(DecodedCert* dCert, const char* domain, int* checkCN);
  1968. WOLFSSL_LOCAL int CheckIPAddr(DecodedCert* dCert, const char* ipasc);
  1969. WOLFSSL_LOCAL void CopyDecodedName(WOLFSSL_X509_NAME* name, DecodedCert* dCert, int nameType);
  1970. #endif
  1971. WOLFSSL_LOCAL int SetupTicket(WOLFSSL* ssl);
  1972. WOLFSSL_LOCAL int CreateTicket(WOLFSSL* ssl);
  1973. WOLFSSL_LOCAL int HashRaw(WOLFSSL* ssl, const byte* output, int sz);
  1974. WOLFSSL_LOCAL int HashOutput(WOLFSSL* ssl, const byte* output, int sz,
  1975. int ivSz);
  1976. WOLFSSL_LOCAL int HashInput(WOLFSSL* ssl, const byte* input, int sz);
  1977. #ifdef HAVE_SNI
  1978. #ifndef NO_WOLFSSL_SERVER
  1979. WOLFSSL_LOCAL int SNI_Callback(WOLFSSL* ssl);
  1980. #endif
  1981. #endif
  1982. #ifdef HAVE_ALPN
  1983. WOLFSSL_LOCAL int ALPN_Select(WOLFSSL* ssl);
  1984. #endif
  1985. WOLFSSL_LOCAL int ChachaAEADEncrypt(WOLFSSL* ssl, byte* out, const byte* input,
  1986. word16 sz); /* needed by sniffer */
  1987. WOLFSSL_LOCAL int ChachaAEADDecrypt(WOLFSSL* ssl, byte* plain, const byte* input,
  1988. word16 sz); /* needed by sniffer */
  1989. #ifdef WOLFSSL_TLS13
  1990. WOLFSSL_LOCAL int DecryptTls13(WOLFSSL* ssl, byte* output, const byte* input,
  1991. word16 sz, const byte* aad, word16 aadSz);
  1992. WOLFSSL_LOCAL int DoTls13HandShakeMsgType(WOLFSSL* ssl, byte* input,
  1993. word32* inOutIdx, byte type,
  1994. word32 size, word32 totalSz);
  1995. WOLFSSL_LOCAL int DoTls13HandShakeMsg(WOLFSSL* ssl, byte* input,
  1996. word32* inOutIdx, word32 totalSz);
  1997. WOLFSSL_LOCAL int DoTls13ServerHello(WOLFSSL* ssl, const byte* input,
  1998. word32* inOutIdx, word32 helloSz,
  1999. byte* extMsgType);
  2000. WOLFSSL_LOCAL int RestartHandshakeHash(WOLFSSL* ssl);
  2001. WOLFSSL_LOCAL int Tls13DeriveKey(WOLFSSL *ssl, byte *output, int outputLen,
  2002. const byte *secret, const byte *label, word32 labelLen, int hashAlgo,
  2003. int includeMsgs, int side);
  2004. #endif
  2005. int TimingPadVerify(WOLFSSL* ssl, const byte* input, int padLen, int macSz,
  2006. int pLen, int content);
  2007. enum {
  2008. FORCED_FREE = 1,
  2009. NO_FORCED_FREE = 0
  2010. };
  2011. /* only use compression extra if using compression */
  2012. #ifdef HAVE_LIBZ
  2013. #define COMP_EXTRA MAX_COMP_EXTRA
  2014. #else
  2015. #define COMP_EXTRA 0
  2016. #endif
  2017. /* only the sniffer needs space in the buffer for extra MTU record(s) */
  2018. #ifdef WOLFSSL_SNIFFER
  2019. #define MTU_EXTRA MAX_MTU * 3
  2020. #else
  2021. #define MTU_EXTRA 0
  2022. #endif
  2023. /* embedded callbacks require large static buffers, make sure on */
  2024. #ifdef WOLFSSL_CALLBACKS
  2025. #undef LARGE_STATIC_BUFFERS
  2026. #define LARGE_STATIC_BUFFERS
  2027. #endif
  2028. /* determine maximum record size */
  2029. #ifdef RECORD_SIZE
  2030. /* user supplied value */
  2031. #if RECORD_SIZE < 128 || RECORD_SIZE > MAX_RECORD_SIZE
  2032. #error Invalid record size
  2033. #endif
  2034. #else
  2035. /* give user option to use 16K static buffers */
  2036. #if defined(LARGE_STATIC_BUFFERS)
  2037. #define RECORD_SIZE MAX_RECORD_SIZE
  2038. #else
  2039. #ifdef WOLFSSL_DTLS
  2040. #define RECORD_SIZE MAX_MTU
  2041. #else
  2042. #define RECORD_SIZE 128
  2043. #endif
  2044. #endif
  2045. #endif
  2046. /* user option to turn off 16K output option */
  2047. /* if using small static buffers (default) and SSL_write tries to write data
  2048. larger than the record we have, dynamically get it, unless user says only
  2049. write in static buffer chunks */
  2050. #ifndef STATIC_CHUNKS_ONLY
  2051. #define OUTPUT_RECORD_SIZE MAX_RECORD_SIZE
  2052. #else
  2053. #define OUTPUT_RECORD_SIZE RECORD_SIZE
  2054. #endif
  2055. /* wolfSSL input buffer
  2056. RFC 2246:
  2057. length
  2058. The length (in bytes) of the following TLSPlaintext.fragment.
  2059. The length should not exceed 2^14.
  2060. */
  2061. #ifdef STATIC_BUFFER_LEN
  2062. /* user supplied option */
  2063. #if STATIC_BUFFER_LEN < 5 || STATIC_BUFFER_LEN > (RECORD_HEADER_SZ + \
  2064. RECORD_SIZE + COMP_EXTRA + MTU_EXTRA + MAX_MSG_EXTRA))
  2065. #error Invalid static buffer length
  2066. #endif
  2067. #elif defined(LARGE_STATIC_BUFFERS)
  2068. #define STATIC_BUFFER_LEN (RECORD_HEADER_SZ + RECORD_SIZE + COMP_EXTRA + \
  2069. MTU_EXTRA + MAX_MSG_EXTRA)
  2070. #else
  2071. /* don't fragment memory from the record header */
  2072. #define STATIC_BUFFER_LEN RECORD_HEADER_SZ
  2073. #endif
  2074. typedef struct {
  2075. ALIGN16 byte staticBuffer[STATIC_BUFFER_LEN];
  2076. byte* buffer; /* place holder for static or dynamic buffer */
  2077. word32 length; /* total buffer length used */
  2078. word32 idx; /* idx to part of length already consumed */
  2079. word32 bufferSize; /* current buffer size */
  2080. byte dynamicFlag; /* dynamic memory currently in use */
  2081. byte offset; /* alignment offset attempt */
  2082. } bufferStatic;
  2083. /* Cipher Suites holder */
  2084. struct Suites {
  2085. word16 suiteSz; /* suite length in bytes */
  2086. word16 hashSigAlgoSz; /* SigAlgo extension length in bytes */
  2087. byte suites[WOLFSSL_MAX_SUITE_SZ];
  2088. byte hashSigAlgo[WOLFSSL_MAX_SIGALGO]; /* sig/algo to offer */
  2089. byte setSuites; /* user set suites from default */
  2090. };
  2091. typedef struct CipherSuite {
  2092. byte cipherSuite0;
  2093. byte cipherSuite;
  2094. word32 ecdhCurveOID;
  2095. struct KeyShareEntry* clientKSE;
  2096. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  2097. int doHelloRetry;
  2098. #endif
  2099. } CipherSuite;
  2100. WOLFSSL_LOCAL void InitSuitesHashSigAlgo(Suites* suites, int haveECDSAsig,
  2101. int haveRSAsig, int haveFalconSig,
  2102. int haveDilithiumSig, int haveAnon,
  2103. int tls1_2, int keySz);
  2104. WOLFSSL_LOCAL void InitSuitesHashSigAlgo_ex(byte* hashSigAlgo, int haveECDSAsig,
  2105. int haveRSAsig, int haveFalconSig,
  2106. int haveDilithiumSig, int haveAnon,
  2107. int tls1_2, int keySz, word16* len);
  2108. WOLFSSL_LOCAL void InitSuitesHashSigAlgo_ex2(byte* hashSigAlgo, int have,
  2109. int tls1_2, int keySz,
  2110. word16* len);
  2111. WOLFSSL_LOCAL int AllocateCtxSuites(WOLFSSL_CTX* ctx);
  2112. WOLFSSL_LOCAL int AllocateSuites(WOLFSSL* ssl);
  2113. WOLFSSL_LOCAL void InitSuites(Suites* suites, ProtocolVersion pv, int keySz,
  2114. word16 haveRSA, word16 havePSK, word16 haveDH,
  2115. word16 haveECDSAsig, word16 haveECC,
  2116. word16 haveStaticRSA, word16 haveStaticECC,
  2117. word16 haveFalconSig, word16 haveDilithiumSig,
  2118. word16 haveAnon, word16 haveNull, int side);
  2119. typedef struct TLSX TLSX;
  2120. WOLFSSL_LOCAL int MatchSuite_ex(const WOLFSSL* ssl, Suites* peerSuites,
  2121. CipherSuite* cs, TLSX* extensions);
  2122. WOLFSSL_LOCAL int MatchSuite(WOLFSSL* ssl, Suites* peerSuites);
  2123. WOLFSSL_LOCAL int SetCipherList(WOLFSSL_CTX* ctx, Suites* suites,
  2124. const char* list);
  2125. WOLFSSL_LOCAL int SetCipherListFromBytes(WOLFSSL_CTX* ctx, Suites* suites,
  2126. const byte* list, const int listSz);
  2127. WOLFSSL_LOCAL int SetSuitesHashSigAlgo(Suites* suites, const char* list);
  2128. #ifndef PSK_TYPES_DEFINED
  2129. typedef unsigned int (*wc_psk_client_callback)(WOLFSSL*, const char*, char*,
  2130. unsigned int, unsigned char*, unsigned int);
  2131. typedef unsigned int (*wc_psk_server_callback)(WOLFSSL*, const char*,
  2132. unsigned char*, unsigned int);
  2133. #ifdef WOLFSSL_TLS13
  2134. typedef unsigned int (*wc_psk_client_cs_callback)(WOLFSSL*, const char*,
  2135. char*, unsigned int, unsigned char*, unsigned int,
  2136. const char* cipherName);
  2137. typedef unsigned int (*wc_psk_client_tls13_callback)(WOLFSSL*, const char*,
  2138. char*, unsigned int, unsigned char*, unsigned int,
  2139. const char** cipherName);
  2140. typedef unsigned int (*wc_psk_server_tls13_callback)(WOLFSSL*, const char*,
  2141. unsigned char*, unsigned int,
  2142. const char** cipherName);
  2143. #endif
  2144. #endif /* PSK_TYPES_DEFINED */
  2145. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  2146. !defined(WOLFSSL_DTLS_EXPORT_TYPES)
  2147. typedef int (*wc_dtls_export)(WOLFSSL* ssl,
  2148. #define WOLFSSL_DTLS_EXPORT_TYPES
  2149. #endif /* WOLFSSL_DTLS_EXPORT_TYPES */
  2150. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  2151. #define MAX_DESCRIPTION_SZ 255
  2152. #endif
  2153. struct WOLFSSL_CIPHER {
  2154. byte cipherSuite0;
  2155. byte cipherSuite;
  2156. const WOLFSSL* ssl;
  2157. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  2158. char description[MAX_DESCRIPTION_SZ];
  2159. unsigned long offset;
  2160. unsigned int in_stack; /* TRUE if added to stack in wolfSSL_get_ciphers_compat */
  2161. int bits;
  2162. #endif
  2163. };
  2164. #ifdef NO_ASN
  2165. /* no_asn won't have */
  2166. typedef struct CertStatus CertStatus;
  2167. #endif
  2168. #ifndef HAVE_OCSP
  2169. typedef struct WOLFSSL_OCSP WOLFSSL_OCSP;
  2170. #endif
  2171. /* wolfSSL OCSP controller */
  2172. #ifdef HAVE_OCSP
  2173. struct WOLFSSL_OCSP {
  2174. WOLFSSL_CERT_MANAGER* cm; /* pointer back to cert manager */
  2175. OcspEntry* ocspList; /* OCSP response list */
  2176. wolfSSL_Mutex ocspLock; /* OCSP list lock */
  2177. int error;
  2178. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  2179. defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
  2180. int(*statusCb)(WOLFSSL*, void*);
  2181. #endif
  2182. };
  2183. #endif
  2184. #ifndef MAX_DATE_SIZE
  2185. #define MAX_DATE_SIZE 32
  2186. #endif
  2187. typedef struct CRL_Entry CRL_Entry;
  2188. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)
  2189. #define CRL_DIGEST_SIZE WC_SM3_DIGEST_SIZE
  2190. #elif defined(NO_SHA)
  2191. #define CRL_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  2192. #else
  2193. #define CRL_DIGEST_SIZE WC_SHA_DIGEST_SIZE
  2194. #endif
  2195. #ifdef NO_ASN
  2196. typedef struct RevokedCert RevokedCert;
  2197. #endif
  2198. #ifdef CRL_STATIC_REVOKED_LIST
  2199. #ifndef CRL_MAX_REVOKED_CERTS
  2200. #define CRL_MAX_REVOKED_CERTS 4
  2201. #elif CRL_MAX_REVOKED_CERTS > 22000
  2202. #error CRL_MAX_REVOKED_CERTS too big, max is 22000
  2203. #endif
  2204. #endif
  2205. /* Complete CRL */
  2206. struct CRL_Entry {
  2207. byte* toBeSigned;
  2208. byte* signature;
  2209. #if defined(OPENSSL_EXTRA)
  2210. WOLFSSL_X509_NAME* issuer; /* X509_NAME type issuer */
  2211. #endif
  2212. CRL_Entry* next; /* next entry */
  2213. wolfSSL_Mutex verifyMutex;
  2214. /* DupCRL_Entry copies data after the `verifyMutex` member. Using the mutex
  2215. * as the marker because clang-tidy doesn't like taking the sizeof a
  2216. * pointer. */
  2217. byte issuerHash[CRL_DIGEST_SIZE]; /* issuer hash */
  2218. /* byte crlHash[CRL_DIGEST_SIZE]; raw crl data hash */
  2219. /* restore the hash here if needed for optimized comparisons */
  2220. byte lastDate[MAX_DATE_SIZE]; /* last date updated */
  2221. byte nextDate[MAX_DATE_SIZE]; /* next update date */
  2222. byte lastDateFormat; /* last date format */
  2223. byte nextDateFormat; /* next date format */
  2224. #if defined(OPENSSL_EXTRA)
  2225. WOLFSSL_ASN1_TIME lastDateAsn1; /* last date updated */
  2226. WOLFSSL_ASN1_TIME nextDateAsn1; /* next update date */
  2227. #endif
  2228. #ifdef CRL_STATIC_REVOKED_LIST
  2229. RevokedCert certs[CRL_MAX_REVOKED_CERTS];
  2230. #else
  2231. RevokedCert* certs; /* revoked cert list */
  2232. #endif
  2233. int totalCerts; /* number on list */
  2234. int version; /* version of certificate */
  2235. int verified;
  2236. word32 tbsSz;
  2237. word32 signatureSz;
  2238. word32 signatureOID;
  2239. #if !defined(NO_SKID) && !defined(NO_ASN)
  2240. byte extAuthKeyIdSet;
  2241. byte extAuthKeyId[KEYID_SIZE];
  2242. #endif
  2243. int crlNumber; /* CRL number extension */
  2244. };
  2245. typedef struct CRL_Monitor CRL_Monitor;
  2246. /* CRL directory monitor */
  2247. struct CRL_Monitor {
  2248. char* path; /* full dir path, if valid pointer we're using */
  2249. int type; /* PEM or ASN1 type */
  2250. };
  2251. #if defined(HAVE_CRL) && defined(NO_FILESYSTEM)
  2252. #undef HAVE_CRL_MONITOR
  2253. #endif
  2254. /* PEM and DER possible */
  2255. #define WOLFSSL_CRL_MONITORS_LEN (2)
  2256. #if defined(__MACH__) || defined(__FreeBSD__) || defined(__linux__)
  2257. typedef int wolfSSL_CRL_mfd_t; /* monitor fd, -1 if no init yet */
  2258. /* mfd for bsd is kqueue fd, eventfd for linux */
  2259. #define WOLFSSL_CRL_MFD_INIT_VAL (-1)
  2260. #elif defined(_MSC_VER)
  2261. typedef HANDLE wolfSSL_CRL_mfd_t; /* monitor fd, INVALID_HANDLE_VALUE if
  2262. * no init yet */
  2263. #define WOLFSSL_CRL_MFD_INIT_VAL (INVALID_HANDLE_VALUE)
  2264. #endif
  2265. /* wolfSSL CRL controller */
  2266. struct WOLFSSL_CRL {
  2267. WOLFSSL_CERT_MANAGER* cm; /* pointer back to cert manager */
  2268. CRL_Entry* currentEntry; /* Current CRL entry being processed */
  2269. CRL_Entry* crlList; /* our CRL list */
  2270. #ifdef HAVE_CRL_IO
  2271. CbCrlIO crlIOCb;
  2272. #endif
  2273. wolfSSL_RwLock crlLock; /* CRL list lock */
  2274. CRL_Monitor monitors[WOLFSSL_CRL_MONITORS_LEN];
  2275. #ifdef HAVE_CRL_MONITOR
  2276. COND_TYPE cond; /* condition to signal setup */
  2277. THREAD_TYPE tid; /* monitoring thread */
  2278. wolfSSL_CRL_mfd_t mfd;
  2279. int setup; /* thread is setup predicate */
  2280. #endif
  2281. void* heap; /* heap hint for dynamic memory */
  2282. };
  2283. #ifdef NO_ASN
  2284. typedef struct Signer Signer;
  2285. #ifdef WOLFSSL_TRUST_PEER_CERT
  2286. typedef struct TrustedPeerCert TrustedPeerCert;
  2287. #endif
  2288. #endif
  2289. #ifndef CA_TABLE_SIZE
  2290. #define CA_TABLE_SIZE 11
  2291. #endif
  2292. #ifdef WOLFSSL_TRUST_PEER_CERT
  2293. #define TP_TABLE_SIZE 11
  2294. #endif
  2295. /* wolfSSL Certificate Manager */
  2296. struct WOLFSSL_CERT_MANAGER {
  2297. Signer* caTable[CA_TABLE_SIZE]; /* the CA signer table */
  2298. void* heap; /* heap helper */
  2299. #ifdef WOLFSSL_TRUST_PEER_CERT
  2300. TrustedPeerCert* tpTable[TP_TABLE_SIZE]; /* table of trusted peer certs */
  2301. wolfSSL_Mutex tpLock; /* trusted peer list lock */
  2302. #endif
  2303. WOLFSSL_CRL* crl; /* CRL checker */
  2304. WOLFSSL_OCSP* ocsp; /* OCSP checker */
  2305. #if !defined(NO_WOLFSSL_SERVER) && (defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  2306. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2))
  2307. WOLFSSL_OCSP* ocsp_stapling; /* OCSP checker for OCSP stapling */
  2308. #endif
  2309. char* ocspOverrideURL; /* use this responder */
  2310. void* ocspIOCtx; /* I/O callback CTX */
  2311. #ifndef NO_WOLFSSL_CM_VERIFY
  2312. VerifyCallback verifyCallback; /* Verify callback */
  2313. #endif
  2314. CallbackCACache caCacheCallback; /* CA cache addition callback */
  2315. CbMissingCRL cbMissingCRL; /* notify thru cb of missing crl */
  2316. CbOCSPIO ocspIOCb; /* I/O callback for OCSP lookup */
  2317. CbOCSPRespFree ocspRespFreeCb; /* Frees OCSP Response from IO Cb */
  2318. wolfSSL_Mutex caLock; /* CA list lock */
  2319. byte crlEnabled:1; /* is CRL on ? */
  2320. byte crlCheckAll:1; /* always leaf, but all ? */
  2321. byte ocspEnabled:1; /* is OCSP on ? */
  2322. byte ocspCheckAll:1; /* always leaf, but all ? */
  2323. byte ocspSendNonce:1; /* send the OCSP nonce ? */
  2324. byte ocspUseOverrideURL:1; /* ignore cert responder, override */
  2325. byte ocspStaplingEnabled:1; /* is OCSP Stapling on ? */
  2326. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  2327. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2328. byte ocspMustStaple:1; /* server must respond with staple */
  2329. #endif
  2330. #ifndef NO_RSA
  2331. short minRsaKeySz; /* minimum allowed RSA key size */
  2332. #endif
  2333. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  2334. short minEccKeySz; /* minimum allowed ECC key size */
  2335. #endif
  2336. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  2337. WOLFSSL_X509_STORE *x509_store_p; /* a pointer back to CTX x509 store */
  2338. /* CTX has ownership and free this */
  2339. /* with CTX free. */
  2340. #endif
  2341. wolfSSL_Ref ref;
  2342. #ifdef HAVE_PQC
  2343. short minFalconKeySz; /* minimum allowed Falcon key size */
  2344. short minDilithiumKeySz; /* minimum allowed Dilithium key size */
  2345. #endif
  2346. };
  2347. WOLFSSL_LOCAL int CM_SaveCertCache(WOLFSSL_CERT_MANAGER* cm,
  2348. const char* fname);
  2349. WOLFSSL_LOCAL int CM_RestoreCertCache(WOLFSSL_CERT_MANAGER* cm,
  2350. const char* fname);
  2351. WOLFSSL_LOCAL int CM_MemSaveCertCache(WOLFSSL_CERT_MANAGER* cm, void* mem,
  2352. int sz, int* used);
  2353. WOLFSSL_LOCAL int CM_MemRestoreCertCache(WOLFSSL_CERT_MANAGER* cm,
  2354. const void* mem, int sz);
  2355. WOLFSSL_LOCAL int CM_GetCertCacheMemSize(WOLFSSL_CERT_MANAGER* cm);
  2356. WOLFSSL_LOCAL int CM_VerifyBuffer_ex(WOLFSSL_CERT_MANAGER* cm, const byte* buff,
  2357. long sz, int format, int prev_err);
  2358. #ifndef NO_CERTS
  2359. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2360. typedef struct ProcPeerCertArgs {
  2361. buffer* certs;
  2362. #ifdef WOLFSSL_TLS13
  2363. buffer* exts; /* extensions */
  2364. #endif
  2365. DecodedCert* dCert;
  2366. word32 idx;
  2367. word32 begin;
  2368. int totalCerts; /* number of certs in certs buffer */
  2369. int count;
  2370. int certIdx;
  2371. int lastErr;
  2372. #ifdef WOLFSSL_TLS13
  2373. byte ctxSz;
  2374. #endif
  2375. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  2376. char untrustedDepth;
  2377. #endif
  2378. word16 fatal:1;
  2379. word16 verifyErr:1;
  2380. word16 dCertInit:1;
  2381. #ifdef WOLFSSL_TRUST_PEER_CERT
  2382. word16 haveTrustPeer:1; /* was cert verified by loaded trusted peer cert */
  2383. #endif
  2384. } ProcPeerCertArgs;
  2385. WOLFSSL_LOCAL int DoVerifyCallback(WOLFSSL_CERT_MANAGER* cm, WOLFSSL* ssl,
  2386. int ret, ProcPeerCertArgs* args);
  2387. #endif /* !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH) */
  2388. #endif /* !defined NO_CERTS */
  2389. /* wolfSSL Sock Addr */
  2390. struct WOLFSSL_SOCKADDR {
  2391. unsigned int sz; /* sockaddr size */
  2392. unsigned int bufSz; /* size of allocated buffer */
  2393. void* sa; /* pointer to the sockaddr_in or sockaddr_in6 */
  2394. };
  2395. typedef struct WOLFSSL_DTLS_CTX {
  2396. WOLFSSL_SOCKADDR peer;
  2397. int rfd;
  2398. int wfd;
  2399. byte userSet:1;
  2400. byte connected:1; /* When set indicates rfd and wfd sockets are
  2401. * connected (connect() and bind() both called).
  2402. * This means that sendto and recvfrom do not need to
  2403. * specify and store the peer address. */
  2404. } WOLFSSL_DTLS_CTX;
  2405. typedef struct WOLFSSL_DTLS_PEERSEQ {
  2406. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  2407. /* Sliding window for current epoch */
  2408. word16 nextEpoch; /* Expected epoch in next record */
  2409. word16 nextSeq_hi; /* Expected sequence in next record */
  2410. word32 nextSeq_lo;
  2411. word32 prevWindow[WOLFSSL_DTLS_WINDOW_WORDS];
  2412. /* Sliding window for old epoch */
  2413. word32 prevSeq_lo;
  2414. word16 prevSeq_hi; /* Next sequence in allowed old epoch */
  2415. #ifdef WOLFSSL_MULTICAST
  2416. word16 peerId;
  2417. word32 highwaterMark;
  2418. #endif
  2419. } WOLFSSL_DTLS_PEERSEQ;
  2420. #define MAX_WRITE_IV_SZ 16 /* max size of client/server write_IV */
  2421. /* keys and secrets
  2422. * keep as a constant size (no additional ifdefs) for session export */
  2423. typedef struct Keys {
  2424. #if !defined(WOLFSSL_AEAD_ONLY) || defined(WOLFSSL_TLS13)
  2425. byte client_write_MAC_secret[WC_MAX_DIGEST_SIZE]; /* max sizes */
  2426. byte server_write_MAC_secret[WC_MAX_DIGEST_SIZE];
  2427. #endif
  2428. byte client_write_key[MAX_SYM_KEY_SIZE]; /* max sizes */
  2429. byte server_write_key[MAX_SYM_KEY_SIZE];
  2430. byte client_write_IV[MAX_WRITE_IV_SZ]; /* max sizes */
  2431. byte server_write_IV[MAX_WRITE_IV_SZ];
  2432. #if defined(HAVE_AEAD) || defined(WOLFSSL_SESSION_EXPORT)
  2433. byte aead_exp_IV[AEAD_MAX_EXP_SZ];
  2434. byte aead_enc_imp_IV[AEAD_MAX_IMP_SZ];
  2435. byte aead_dec_imp_IV[AEAD_MAX_IMP_SZ];
  2436. #endif
  2437. #ifdef WOLFSSL_DTLS13
  2438. byte client_sn_key[MAX_SYM_KEY_SIZE];
  2439. byte server_sn_key[MAX_SYM_KEY_SIZE];
  2440. #endif /* WOLFSSL_DTLS13 */
  2441. word32 peer_sequence_number_hi;
  2442. word32 peer_sequence_number_lo;
  2443. word32 sequence_number_hi;
  2444. word32 sequence_number_lo;
  2445. #ifdef WOLFSSL_DTLS
  2446. word16 curEpoch; /* Received epoch in current record */
  2447. word16 curSeq_hi; /* Received sequence in current record */
  2448. word32 curSeq_lo;
  2449. #ifdef WOLFSSL_DTLS13
  2450. w64wrapper curEpoch64; /* Received epoch in current record */
  2451. w64wrapper curSeq;
  2452. #endif /* WOLFSSL_DTLS13 */
  2453. #ifdef WOLFSSL_MULTICAST
  2454. byte curPeerId; /* Received peer group ID in current record */
  2455. #endif
  2456. WOLFSSL_DTLS_PEERSEQ peerSeq[WOLFSSL_DTLS_PEERSEQ_SZ];
  2457. word16 dtls_peer_handshake_number;
  2458. word16 dtls_expected_peer_handshake_number;
  2459. word16 dtls_epoch; /* Current epoch */
  2460. word16 dtls_sequence_number_hi; /* Current epoch */
  2461. word32 dtls_sequence_number_lo;
  2462. word16 dtls_prev_sequence_number_hi; /* Previous epoch */
  2463. word32 dtls_prev_sequence_number_lo;
  2464. word16 dtls_handshake_number; /* Current tx handshake seq */
  2465. #endif
  2466. word32 encryptSz; /* last size of encrypted data */
  2467. word32 padSz; /* how much to advance after decrypt part */
  2468. byte encryptionOn; /* true after change cipher spec */
  2469. byte decryptedCur; /* only decrypt current record once */
  2470. #ifdef WOLFSSL_TLS13
  2471. byte updateResponseReq:1; /* KeyUpdate response from peer required. */
  2472. byte keyUpdateRespond:1; /* KeyUpdate is to be responded to. */
  2473. #endif
  2474. #ifdef WOLFSSL_RENESAS_TSIP_TLS
  2475. tsip_hmac_sha_key_index_t tsip_client_write_MAC_secret;
  2476. tsip_hmac_sha_key_index_t tsip_server_write_MAC_secret;
  2477. #endif
  2478. #ifdef WOLFSSL_RENESAS_FSPSM_TLS
  2479. FSPSM_HMAC_WKEY fspsm_client_write_MAC_secret;
  2480. FSPSM_HMAC_WKEY fspsm_server_write_MAC_secret;
  2481. #endif
  2482. } Keys;
  2483. /* Forward declare opaque pointer to make available for func def */
  2484. typedef struct Options Options;
  2485. /** TLS Extensions - RFC 6066 */
  2486. #ifdef HAVE_TLS_EXTENSIONS
  2487. typedef enum {
  2488. #ifdef HAVE_SNI
  2489. TLSX_SERVER_NAME = 0x0000, /* a.k.a. SNI */
  2490. #endif
  2491. TLSX_MAX_FRAGMENT_LENGTH = 0x0001,
  2492. TLSX_TRUSTED_CA_KEYS = 0x0003,
  2493. TLSX_TRUNCATED_HMAC = 0x0004,
  2494. TLSX_STATUS_REQUEST = 0x0005, /* a.k.a. OCSP stapling */
  2495. TLSX_SUPPORTED_GROUPS = 0x000a, /* a.k.a. Supported Curves */
  2496. TLSX_EC_POINT_FORMATS = 0x000b,
  2497. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  2498. TLSX_SIGNATURE_ALGORITHMS = 0x000d, /* HELLO_EXT_SIG_ALGO */
  2499. #endif
  2500. #ifdef WOLFSSL_SRTP
  2501. TLSX_USE_SRTP = 0x000e, /* 14 */
  2502. #endif
  2503. TLSX_APPLICATION_LAYER_PROTOCOL = 0x0010, /* a.k.a. ALPN */
  2504. TLSX_STATUS_REQUEST_V2 = 0x0011, /* a.k.a. OCSP stapling v2 */
  2505. #ifdef HAVE_RPK
  2506. TLSX_CLIENT_CERTIFICATE_TYPE = 0x0013, /* RFC8446 */
  2507. TLSX_SERVER_CERTIFICATE_TYPE = 0x0014, /* RFC8446 */
  2508. #endif
  2509. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  2510. TLSX_ENCRYPT_THEN_MAC = 0x0016, /* RFC 7366 */
  2511. #endif
  2512. TLSX_EXTENDED_MASTER_SECRET = 0x0017, /* HELLO_EXT_EXTMS */
  2513. TLSX_SESSION_TICKET = 0x0023,
  2514. #ifdef WOLFSSL_TLS13
  2515. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  2516. TLSX_PRE_SHARED_KEY = 0x0029,
  2517. #endif
  2518. #ifdef WOLFSSL_EARLY_DATA
  2519. TLSX_EARLY_DATA = 0x002a,
  2520. #endif
  2521. TLSX_SUPPORTED_VERSIONS = 0x002b,
  2522. #ifdef WOLFSSL_SEND_HRR_COOKIE
  2523. TLSX_COOKIE = 0x002c,
  2524. #endif
  2525. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  2526. TLSX_PSK_KEY_EXCHANGE_MODES = 0x002d,
  2527. #endif
  2528. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES)
  2529. TLSX_CERTIFICATE_AUTHORITIES = 0x002f,
  2530. #endif
  2531. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  2532. TLSX_POST_HANDSHAKE_AUTH = 0x0031,
  2533. #endif
  2534. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  2535. TLSX_SIGNATURE_ALGORITHMS_CERT = 0x0032,
  2536. #endif
  2537. TLSX_KEY_SHARE = 0x0033,
  2538. #if defined(WOLFSSL_DTLS_CID)
  2539. TLSX_CONNECTION_ID = 0x0036,
  2540. #endif /* defined(WOLFSSL_DTLS_CID) */
  2541. #ifdef WOLFSSL_QUIC
  2542. TLSX_KEY_QUIC_TP_PARAMS = 0x0039, /* RFC 9001, ch. 8.2 */
  2543. #endif
  2544. #endif
  2545. TLSX_RENEGOTIATION_INFO = 0xff01,
  2546. #ifdef WOLFSSL_QUIC
  2547. TLSX_KEY_QUIC_TP_PARAMS_DRAFT = 0xffa5, /* from draft-ietf-quic-tls-27 */
  2548. #endif
  2549. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  2550. TLSX_ECH = 0xfe0d, /* from draft-ietf-tls-esni-13 */
  2551. #endif
  2552. } TLSX_Type;
  2553. /* TLS Certificate type defined RFC7250
  2554. * https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#tls-extensiontype-values-3
  2555. */
  2556. #if defined(HAVE_RPK)
  2557. typedef struct RpkConfig {
  2558. /* user's preference */
  2559. byte preferred_ClientCertTypeCnt;
  2560. byte preferred_ClientCertTypes[MAX_CLIENT_CERT_TYPE_CNT];
  2561. byte preferred_ServerCertTypeCnt;
  2562. byte preferred_ServerCertTypes[MAX_CLIENT_CERT_TYPE_CNT];
  2563. /* reflect to client_certificate_type extension in xxxHello */
  2564. } RpkConfig;
  2565. typedef struct RpkState {
  2566. byte sending_ClientCertTypeCnt;
  2567. byte sending_ClientCertTypes[MAX_CLIENT_CERT_TYPE_CNT];
  2568. /* reflect to server_certificate_type extension in xxxHello */
  2569. byte sending_ServerCertTypeCnt;
  2570. byte sending_ServerCertTypes[MAX_SERVER_CERT_TYPE_CNT];
  2571. /* client_certificate_type extension in received yyyHello */
  2572. byte received_ClientCertTypeCnt;
  2573. byte received_ClientCertTypes[MAX_CLIENT_CERT_TYPE_CNT];
  2574. /* server_certificate_type extension in received yyyHello */
  2575. byte received_ServerCertTypeCnt;
  2576. byte received_ServerCertTypes[MAX_SERVER_CERT_TYPE_CNT];
  2577. /* set if Raw-public-key cert is loaded as own certificate */
  2578. int isRPKLoaded;
  2579. } RpkState;
  2580. #endif /* HAVE_RPK */
  2581. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  2582. typedef enum {
  2583. ECH_TYPE_OUTER = 0,
  2584. ECH_TYPE_INNER = 1
  2585. } EchType;
  2586. typedef enum {
  2587. ECH_WRITE_GREASE,
  2588. ECH_WRITE_REAL,
  2589. ECH_WRITE_RETRY_CONFIGS,
  2590. ECH_WRITE_NONE,
  2591. ECH_PARSED_INTERNAL,
  2592. } EchState;
  2593. typedef struct EchCipherSuite {
  2594. word16 kdfId;
  2595. word16 aeadId;
  2596. } EchCipherSuite;
  2597. typedef struct WOLFSSL_EchConfig {
  2598. byte* raw;
  2599. char* publicName;
  2600. void* receiverPrivkey;
  2601. struct WOLFSSL_EchConfig* next;
  2602. EchCipherSuite* cipherSuites;
  2603. word32 rawLen;
  2604. word16 kemId;
  2605. byte configId;
  2606. byte numCipherSuites;
  2607. byte receiverPubkey[HPKE_Npk_MAX];
  2608. } WOLFSSL_EchConfig;
  2609. typedef struct WOLFSSL_ECH {
  2610. Hpke* hpke;
  2611. const byte* aad;
  2612. void* ephemeralKey;
  2613. WOLFSSL_EchConfig* echConfig;
  2614. byte* innerClientHello;
  2615. byte* outerClientPayload;
  2616. EchCipherSuite cipherSuite;
  2617. word16 aadLen;
  2618. word16 paddingLen;
  2619. word16 innerClientHelloLen;
  2620. word16 kemId;
  2621. word16 encLen;
  2622. EchState state;
  2623. byte type;
  2624. byte configId;
  2625. byte enc[HPKE_Npk_MAX];
  2626. } WOLFSSL_ECH;
  2627. WOLFSSL_LOCAL int EchConfigGetSupportedCipherSuite(WOLFSSL_EchConfig* config);
  2628. WOLFSSL_LOCAL int TLSX_FinalizeEch(WOLFSSL_ECH* ech, byte* aad, word32 aadLen);
  2629. WOLFSSL_LOCAL int GetEchConfig(WOLFSSL_EchConfig* config, byte* output,
  2630. word32* outputLen);
  2631. WOLFSSL_LOCAL int GetEchConfigsEx(WOLFSSL_EchConfig* configs,
  2632. byte* output, word32* outputLen);
  2633. #endif
  2634. struct TLSX {
  2635. TLSX_Type type; /* Extension Type */
  2636. void* data; /* Extension Data */
  2637. word32 val; /* Extension Value */
  2638. byte resp; /* IsResponse Flag */
  2639. struct TLSX* next; /* List Behavior */
  2640. };
  2641. WOLFSSL_LOCAL TLSX* TLSX_Find(TLSX* list, TLSX_Type type);
  2642. WOLFSSL_LOCAL void TLSX_Remove(TLSX** list, TLSX_Type type, void* heap);
  2643. WOLFSSL_LOCAL void TLSX_FreeAll(TLSX* list, void* heap);
  2644. WOLFSSL_LOCAL int TLSX_SupportExtensions(WOLFSSL* ssl);
  2645. WOLFSSL_LOCAL int TLSX_PopulateExtensions(WOLFSSL* ssl, byte isRequest);
  2646. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  2647. WOLFSSL_LOCAL int TLSX_GetRequestSize(WOLFSSL* ssl, byte msgType,
  2648. word16* pLength);
  2649. WOLFSSL_LOCAL int TLSX_WriteRequest(WOLFSSL* ssl, byte* output,
  2650. byte msgType, word16* pOffset);
  2651. #endif
  2652. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_SERVER)
  2653. /* TLS 1.3 Certificate messages have extensions. */
  2654. WOLFSSL_LOCAL int TLSX_GetResponseSize(WOLFSSL* ssl, byte msgType,
  2655. word16* pLength);
  2656. WOLFSSL_LOCAL int TLSX_WriteResponse(WOLFSSL *ssl, byte* output, byte msgType,
  2657. word16* pOffset);
  2658. #endif
  2659. WOLFSSL_LOCAL int TLSX_ParseVersion(WOLFSSL* ssl, const byte* input,
  2660. word16 length, byte msgType, int* found);
  2661. WOLFSSL_LOCAL int TLSX_SupportedVersions_Parse(const WOLFSSL* ssl,
  2662. const byte* input, word16 length, byte msgType, ProtocolVersion* pv,
  2663. Options* opts, TLSX** exts);
  2664. WOLFSSL_LOCAL int TLSX_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2665. byte msgType, Suites *suites);
  2666. WOLFSSL_LOCAL int TLSX_Push(TLSX** list, TLSX_Type type,
  2667. const void* data, void* heap);
  2668. WOLFSSL_LOCAL int TLSX_Append(TLSX** list, TLSX_Type type,
  2669. const void* data, void* heap);
  2670. #elif defined(HAVE_SNI) \
  2671. || defined(HAVE_MAX_FRAGMENT) \
  2672. || defined(HAVE_TRUSTED_CA) \
  2673. || defined(HAVE_TRUNCATED_HMAC) \
  2674. || defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  2675. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) \
  2676. || defined(HAVE_SUPPORTED_CURVES) \
  2677. || defined(HAVE_ALPN) \
  2678. || defined(HAVE_SESSION_TICKET) \
  2679. || defined(HAVE_SECURE_RENEGOTIATION) \
  2680. || defined(HAVE_SERVER_RENEGOTIATION_INFO)
  2681. #error Using TLS extensions requires HAVE_TLS_EXTENSIONS to be defined.
  2682. #endif /* HAVE_TLS_EXTENSIONS */
  2683. /** Server Name Indication - RFC 6066 (session 3) */
  2684. #ifdef HAVE_SNI
  2685. typedef struct SNI {
  2686. byte type; /* SNI Type */
  2687. union { char* host_name; } data; /* SNI Data */
  2688. struct SNI* next; /* List Behavior */
  2689. byte status; /* Matching result */
  2690. #ifndef NO_WOLFSSL_SERVER
  2691. byte options; /* Behavior options */
  2692. #endif
  2693. } SNI;
  2694. WOLFSSL_LOCAL int TLSX_UseSNI(TLSX** extensions, byte type, const void* data,
  2695. word16 size, void* heap);
  2696. WOLFSSL_LOCAL byte TLSX_SNI_Status(TLSX* extensions, byte type);
  2697. WOLFSSL_LOCAL word16 TLSX_SNI_GetRequest(TLSX* extensions, byte type,
  2698. void** data);
  2699. #ifndef NO_WOLFSSL_SERVER
  2700. WOLFSSL_LOCAL void TLSX_SNI_SetOptions(TLSX* extensions, byte type,
  2701. byte options);
  2702. WOLFSSL_LOCAL int TLSX_SNI_GetFromBuffer(const byte* clientHello,
  2703. word32 helloSz, byte type, byte* sni, word32* inOutSz);
  2704. #endif
  2705. #endif /* HAVE_SNI */
  2706. /* Trusted CA Key Indication - RFC 6066 (section 6) */
  2707. #ifdef HAVE_TRUSTED_CA
  2708. typedef struct TCA {
  2709. byte type; /* TCA Type */
  2710. byte* id; /* TCA identifier */
  2711. word16 idSz; /* TCA identifier size */
  2712. struct TCA* next; /* List Behavior */
  2713. } TCA;
  2714. WOLFSSL_LOCAL int TLSX_UseTrustedCA(TLSX** extensions, byte type,
  2715. const byte* id, word16 idSz, void* heap);
  2716. #endif /* HAVE_TRUSTED_CA */
  2717. /* Application-Layer Protocol Negotiation - RFC 7301 */
  2718. #ifdef HAVE_ALPN
  2719. typedef struct ALPN {
  2720. char* protocol_name; /* ALPN protocol name */
  2721. struct ALPN* next; /* List Behavior */
  2722. byte options; /* Behavior options */
  2723. byte negotiated; /* ALPN protocol negotiated or not */
  2724. } ALPN;
  2725. WOLFSSL_LOCAL int TLSX_ALPN_GetRequest(TLSX* extensions,
  2726. void** data, word16 *dataSz);
  2727. WOLFSSL_LOCAL int TLSX_UseALPN(TLSX** extensions, const void* data,
  2728. word16 size, byte options, void* heap);
  2729. WOLFSSL_LOCAL int TLSX_ALPN_SetOptions(TLSX** extensions, byte option);
  2730. #endif /* HAVE_ALPN */
  2731. /** Maximum Fragment Length Negotiation - RFC 6066 (session 4) */
  2732. #ifdef HAVE_MAX_FRAGMENT
  2733. WOLFSSL_LOCAL int TLSX_UseMaxFragment(TLSX** extensions, byte mfl, void* heap);
  2734. #endif /* HAVE_MAX_FRAGMENT */
  2735. /** Truncated HMAC - RFC 6066 (session 7) */
  2736. #ifdef HAVE_TRUNCATED_HMAC
  2737. WOLFSSL_LOCAL int TLSX_UseTruncatedHMAC(TLSX** extensions, void* heap);
  2738. #endif /* HAVE_TRUNCATED_HMAC */
  2739. /** Certificate Status Request - RFC 6066 (session 8) */
  2740. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  2741. typedef struct {
  2742. byte status_type;
  2743. byte options;
  2744. WOLFSSL* ssl;
  2745. union {
  2746. OcspRequest ocsp;
  2747. } request;
  2748. #ifdef WOLFSSL_TLS13
  2749. buffer response;
  2750. #endif
  2751. } CertificateStatusRequest;
  2752. WOLFSSL_LOCAL int TLSX_UseCertificateStatusRequest(TLSX** extensions,
  2753. byte status_type, byte options, WOLFSSL* ssl, void* heap, int devId);
  2754. #ifndef NO_CERTS
  2755. WOLFSSL_LOCAL int TLSX_CSR_InitRequest(TLSX* extensions, DecodedCert* cert,
  2756. void* heap);
  2757. #endif
  2758. WOLFSSL_LOCAL void* TLSX_CSR_GetRequest(TLSX* extensions);
  2759. WOLFSSL_LOCAL int TLSX_CSR_ForceRequest(WOLFSSL* ssl);
  2760. #endif
  2761. /** Certificate Status Request v2 - RFC 6961 */
  2762. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2763. typedef struct CSRIv2 {
  2764. byte status_type;
  2765. byte options;
  2766. word16 requests;
  2767. union {
  2768. OcspRequest ocsp[1 + MAX_CHAIN_DEPTH];
  2769. } request;
  2770. struct CSRIv2* next;
  2771. } CertificateStatusRequestItemV2;
  2772. WOLFSSL_LOCAL int TLSX_UseCertificateStatusRequestV2(TLSX** extensions,
  2773. byte status_type, byte options, void* heap, int devId);
  2774. #ifndef NO_CERTS
  2775. WOLFSSL_LOCAL int TLSX_CSR2_InitRequests(TLSX* extensions, DecodedCert* cert,
  2776. byte isPeer, void* heap);
  2777. #endif
  2778. WOLFSSL_LOCAL void* TLSX_CSR2_GetRequest(TLSX* extensions, byte status_type,
  2779. byte idx);
  2780. WOLFSSL_LOCAL int TLSX_CSR2_ForceRequest(WOLFSSL* ssl);
  2781. #endif
  2782. #if defined(WOLFSSL_PUBLIC_ASN) && defined(HAVE_PK_CALLBACKS)
  2783. /* Internal callback guarded by WOLFSSL_PUBLIC_ASN because of DecodedCert. */
  2784. typedef int (*CallbackProcessPeerCert)(WOLFSSL* ssl, DecodedCert* p_cert);
  2785. WOLFSSL_API void wolfSSL_CTX_SetProcessPeerCertCb(WOLFSSL_CTX* ctx,
  2786. CallbackProcessPeerCert cb);
  2787. #endif /* DecodedCert && HAVE_PK_CALLBACKS */
  2788. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  2789. typedef struct SignatureAlgorithms {
  2790. /* Not const since it is modified in TLSX_SignatureAlgorithms_MapPss */
  2791. WOLFSSL* ssl;
  2792. word16 hashSigAlgoSz; /* SigAlgo extension length in bytes */
  2793. /* Ignore "nonstandard extension used : zero-sized array in struct/union"
  2794. * MSVC warning */
  2795. #ifdef _MSC_VER
  2796. #pragma warning(disable: 4200)
  2797. #endif
  2798. byte hashSigAlgo[]; /* sig/algo to offer */
  2799. } SignatureAlgorithms;
  2800. WOLFSSL_LOCAL SignatureAlgorithms* TLSX_SignatureAlgorithms_New(
  2801. WOLFSSL* ssl, word16 hashSigAlgoSz, void* heap);
  2802. WOLFSSL_LOCAL void TLSX_SignatureAlgorithms_FreeAll(SignatureAlgorithms* sa,
  2803. void* heap);
  2804. #endif
  2805. /** Supported Elliptic Curves - RFC 4492 (session 4) */
  2806. #ifdef HAVE_SUPPORTED_CURVES
  2807. typedef struct SupportedCurve {
  2808. word16 name; /* Curve Names */
  2809. struct SupportedCurve* next; /* List Behavior */
  2810. } SupportedCurve;
  2811. typedef struct PointFormat {
  2812. byte format; /* PointFormat */
  2813. struct PointFormat* next; /* List Behavior */
  2814. } PointFormat;
  2815. WOLFSSL_LOCAL int TLSX_SupportedCurve_Copy(TLSX* src, TLSX** dst, void* heap);
  2816. WOLFSSL_LOCAL int TLSX_UseSupportedCurve(TLSX** extensions, word16 name,
  2817. void* heap);
  2818. WOLFSSL_LOCAL int TLSX_UsePointFormat(TLSX** extensions, byte point,
  2819. void* heap);
  2820. #ifndef NO_WOLFSSL_SERVER
  2821. WOLFSSL_LOCAL int TLSX_ValidateSupportedCurves(const WOLFSSL* ssl, byte first,
  2822. byte second, word32* ecdhCurveOID);
  2823. WOLFSSL_LOCAL int TLSX_SupportedCurve_CheckPriority(WOLFSSL* ssl);
  2824. WOLFSSL_LOCAL int TLSX_SupportedFFDHE_Set(WOLFSSL* ssl);
  2825. #endif
  2826. WOLFSSL_LOCAL int TLSX_SupportedCurve_Preferred(WOLFSSL* ssl,
  2827. int checkSupported);
  2828. WOLFSSL_LOCAL int TLSX_SupportedCurve_Parse(const WOLFSSL* ssl,
  2829. const byte* input, word16 length, byte isRequest, TLSX** extensions);
  2830. #endif /* HAVE_SUPPORTED_CURVES */
  2831. /** Renegotiation Indication - RFC 5746 */
  2832. #if defined(HAVE_SECURE_RENEGOTIATION) \
  2833. || defined(HAVE_SERVER_RENEGOTIATION_INFO)
  2834. enum key_cache_state {
  2835. SCR_CACHE_NULL = 0, /* empty / begin state */
  2836. SCR_CACHE_NEEDED, /* need to cache keys */
  2837. SCR_CACHE_COPY, /* we have a cached copy */
  2838. SCR_CACHE_PARTIAL, /* partial restore to real keys */
  2839. SCR_CACHE_COMPLETE /* complete restore to real keys */
  2840. };
  2841. /* Additional Connection State according to rfc5746 section 3.1 */
  2842. typedef struct SecureRenegotiation {
  2843. byte enabled; /* secure_renegotiation flag in rfc */
  2844. byte verifySet;
  2845. byte startScr; /* server requested client to start scr */
  2846. enum key_cache_state cache_status; /* track key cache state */
  2847. byte client_verify_data[TLS_FINISHED_SZ]; /* cached */
  2848. byte server_verify_data[TLS_FINISHED_SZ]; /* cached */
  2849. byte subject_hash_set; /* if peer cert hash is set */
  2850. byte subject_hash[KEYID_SIZE]; /* peer cert hash */
  2851. Keys tmp_keys; /* can't overwrite real keys yet */
  2852. } SecureRenegotiation;
  2853. WOLFSSL_LOCAL int TLSX_UseSecureRenegotiation(TLSX** extensions, void* heap);
  2854. #ifdef HAVE_SERVER_RENEGOTIATION_INFO
  2855. WOLFSSL_LOCAL int TLSX_AddEmptyRenegotiationInfo(TLSX** extensions, void* heap);
  2856. #endif
  2857. #endif /* HAVE_SECURE_RENEGOTIATION */
  2858. #ifdef HAVE_SESSION_TICKET
  2859. /* Our ticket format. All members need to be a byte or array of byte to
  2860. * avoid alignment issues */
  2861. typedef struct InternalTicket {
  2862. ProtocolVersion pv; /* version when ticket created */
  2863. byte suite[SUITE_LEN]; /* cipher suite when created */
  2864. byte msecret[SECRET_LEN]; /* master secret */
  2865. byte timestamp[TIMESTAMP_LEN]; /* born on */
  2866. byte haveEMS; /* have extended master secret */
  2867. #ifdef WOLFSSL_TLS13
  2868. byte ageAdd[AGEADD_LEN]; /* Obfuscation of age */
  2869. byte namedGroup[NAMEDGROUP_LEN]; /* Named group used */
  2870. byte ticketNonceLen;
  2871. byte ticketNonce[MAX_TICKET_NONCE_STATIC_SZ];
  2872. #ifdef WOLFSSL_EARLY_DATA
  2873. byte maxEarlyDataSz[MAXEARLYDATASZ_LEN]; /* Max size of
  2874. * early data */
  2875. #endif
  2876. #endif
  2877. #ifdef WOLFSSL_TICKET_HAVE_ID
  2878. byte id[ID_LEN];
  2879. #endif
  2880. #ifdef OPENSSL_EXTRA
  2881. byte sessionCtxSz; /* sessionCtx length */
  2882. byte sessionCtx[ID_LEN]; /* app specific context id */
  2883. #endif /* OPENSSL_EXTRA */
  2884. } InternalTicket;
  2885. #ifndef WOLFSSL_TICKET_EXTRA_PADDING_SZ
  2886. #define WOLFSSL_TICKET_EXTRA_PADDING_SZ 32
  2887. #endif
  2888. #define WOLFSSL_TICKET_ENC_SZ \
  2889. (sizeof(InternalTicket) + WOLFSSL_TICKET_EXTRA_PADDING_SZ)
  2890. /* RFC 5077 defines this for session tickets. All members need to be a byte or
  2891. * array of byte to avoid alignment issues */
  2892. typedef struct ExternalTicket {
  2893. byte key_name[WOLFSSL_TICKET_NAME_SZ]; /* key context name - 16 */
  2894. byte iv[WOLFSSL_TICKET_IV_SZ]; /* this ticket's iv - 16 */
  2895. byte enc_len[OPAQUE16_LEN]; /* encrypted length - 2 */
  2896. byte enc_ticket[WOLFSSL_TICKET_ENC_SZ];
  2897. /* encrypted internal ticket */
  2898. byte mac[WOLFSSL_TICKET_MAC_SZ]; /* total mac - 32 */
  2899. } ExternalTicket;
  2900. /* Cast to int to reduce amount of casts in code */
  2901. #define SESSION_TICKET_LEN ((int)sizeof(ExternalTicket))
  2902. #define WOLFSSL_TICKET_FIXED_SZ (SESSION_TICKET_LEN - WOLFSSL_TICKET_ENC_SZ)
  2903. typedef struct SessionTicket {
  2904. word32 lifetime;
  2905. #ifdef WOLFSSL_TLS13
  2906. word64 seen;
  2907. word32 ageAdd;
  2908. #endif
  2909. byte* data;
  2910. word16 size;
  2911. } SessionTicket;
  2912. #if !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_WOLFSSL_SERVER)
  2913. /* Data passed to default SessionTicket enc/dec callback. */
  2914. typedef struct TicketEncCbCtx {
  2915. /* Name for this context. */
  2916. byte name[WOLFSSL_TICKET_NAME_SZ];
  2917. /* Current keys - current and next. */
  2918. byte key[2][WOLFSSL_TICKET_KEY_SZ];
  2919. /* Expirary date of keys. */
  2920. word32 expirary[2];
  2921. /* Random number generator to use for generating name, keys and IV. */
  2922. WC_RNG rng;
  2923. #ifndef SINGLE_THREADED
  2924. /* Mutex for access to changing keys. */
  2925. wolfSSL_Mutex mutex;
  2926. #endif
  2927. /* Pointer back to SSL_CTX. */
  2928. WOLFSSL_CTX* ctx;
  2929. } TicketEncCbCtx;
  2930. #endif /* !WOLFSSL_NO_DEF_TICKET_ENC_CB && !NO_WOLFSSL_SERVER */
  2931. WOLFSSL_LOCAL int TLSX_UseSessionTicket(TLSX** extensions,
  2932. SessionTicket* ticket, void* heap);
  2933. WOLFSSL_LOCAL SessionTicket* TLSX_SessionTicket_Create(word32 lifetime,
  2934. byte* data, word16 size, void* heap);
  2935. WOLFSSL_LOCAL void TLSX_SessionTicket_Free(SessionTicket* ticket, void* heap);
  2936. #endif /* HAVE_SESSION_TICKET */
  2937. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  2938. int TLSX_EncryptThenMac_Respond(WOLFSSL* ssl);
  2939. #endif
  2940. #ifdef WOLFSSL_TLS13
  2941. /* Cookie extension information - cookie data. */
  2942. typedef struct Cookie {
  2943. word16 len;
  2944. /* Ignore "nonstandard extension used : zero-sized array in struct/union"
  2945. * MSVC warning */
  2946. #ifdef _MSC_VER
  2947. #pragma warning(disable: 4200)
  2948. #endif
  2949. byte data[];
  2950. } Cookie;
  2951. WOLFSSL_LOCAL int TLSX_Cookie_Use(const WOLFSSL* ssl, const byte* data,
  2952. word16 len, byte* mac, byte macSz, int resp, TLSX** exts);
  2953. WOLFSSL_LOCAL int TlsCheckCookie(const WOLFSSL* ssl, const byte* cookie,
  2954. word16 cookieSz);
  2955. /* Key Share - TLS v1.3 Specification */
  2956. /* The KeyShare extension information - entry in a linked list. */
  2957. typedef struct KeyShareEntry {
  2958. word16 group; /* NamedGroup */
  2959. byte* ke; /* Key exchange data */
  2960. word32 keLen; /* Key exchange data length */
  2961. void* key; /* Key struct */
  2962. word32 keyLen; /* Key size (bytes) */
  2963. byte* pubKey; /* Public key */
  2964. word32 pubKeyLen; /* Public key length */
  2965. #if !defined(NO_DH) || defined(HAVE_PQC)
  2966. byte* privKey; /* Private key - DH and PQ KEMs only */
  2967. #endif
  2968. #ifdef WOLFSSL_ASYNC_CRYPT
  2969. int lastRet;
  2970. #endif
  2971. struct KeyShareEntry* next; /* List pointer */
  2972. } KeyShareEntry;
  2973. WOLFSSL_LOCAL int TLSX_KeyShare_Use(const WOLFSSL* ssl, word16 group,
  2974. word16 len, byte* data, KeyShareEntry **kse, TLSX** extensions);
  2975. WOLFSSL_LOCAL int TLSX_KeyShare_Empty(WOLFSSL* ssl);
  2976. WOLFSSL_LOCAL int TLSX_KeyShare_SetSupported(const WOLFSSL* ssl,
  2977. TLSX** extensions);
  2978. WOLFSSL_LOCAL int TLSX_KeyShare_GenKey(WOLFSSL *ssl, KeyShareEntry *kse);
  2979. WOLFSSL_LOCAL int TLSX_KeyShare_Choose(const WOLFSSL *ssl, TLSX* extensions,
  2980. byte cipherSuite0, byte cipherSuite, KeyShareEntry** kse,
  2981. byte* searched);
  2982. WOLFSSL_LOCAL int TLSX_KeyShare_Setup(WOLFSSL *ssl, KeyShareEntry* clientKSE);
  2983. WOLFSSL_LOCAL int TLSX_KeyShare_Establish(WOLFSSL* ssl, int* doHelloRetry);
  2984. WOLFSSL_LOCAL int TLSX_KeyShare_DeriveSecret(WOLFSSL* sclientKSEclientKSEsl);
  2985. WOLFSSL_LOCAL int TLSX_KeyShare_Parse(WOLFSSL* ssl, const byte* input,
  2986. word16 length, byte msgType);
  2987. WOLFSSL_LOCAL int TLSX_KeyShare_Parse_ClientHello(const WOLFSSL* ssl,
  2988. const byte* input, word16 length, TLSX** extensions);
  2989. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  2990. enum PskDecryptReturn {
  2991. PSK_DECRYPT_NONE = 0,
  2992. PSK_DECRYPT_OK,
  2993. PSK_DECRYPT_CREATE,
  2994. PSK_DECRYPT_FAIL,
  2995. };
  2996. #ifdef HAVE_SESSION_TICKET
  2997. typedef struct psk_sess_free_cb_ctx {
  2998. word32 row;
  2999. #ifdef HAVE_EXT_CACHE
  3000. int extCache;
  3001. int freeSess;
  3002. #endif
  3003. } psk_sess_free_cb_ctx;
  3004. typedef void (psk_sess_free_cb)(const WOLFSSL* ssl, const WOLFSSL_SESSION* sess,
  3005. psk_sess_free_cb_ctx* freeCtx);
  3006. #endif
  3007. /* The PreSharedKey extension information - entry in a linked list. */
  3008. typedef struct PreSharedKey {
  3009. word16 identityLen; /* Length of identity */
  3010. byte* identity; /* PSK identity */
  3011. word32 ticketAge; /* Age of the ticket */
  3012. byte cipherSuite0; /* Cipher Suite */
  3013. byte cipherSuite; /* Cipher Suite */
  3014. word32 binderLen; /* Length of HMAC */
  3015. byte binder[WC_MAX_DIGEST_SIZE]; /* HMAC of handshake */
  3016. byte hmac; /* HMAC algorithm */
  3017. #ifdef HAVE_SESSION_TICKET
  3018. InternalTicket* it; /* ptr to ticket */
  3019. const WOLFSSL_SESSION* sess; /* ptr to session either from external cache or
  3020. * into SessionCache. Work around so that we
  3021. * don't call into the cache more than once */
  3022. psk_sess_free_cb* sess_free_cb; /* callback to free sess */
  3023. psk_sess_free_cb_ctx sess_free_cb_ctx; /* info for sess_free_cb */
  3024. #endif
  3025. byte resumption:1; /* Resumption PSK */
  3026. byte chosen:1; /* Server's choice */
  3027. byte decryptRet:3; /* Ticket decrypt return */
  3028. struct PreSharedKey* next; /* List pointer */
  3029. } PreSharedKey;
  3030. WOLFSSL_LOCAL int TLSX_PreSharedKey_WriteBinders(PreSharedKey* list,
  3031. byte* output, byte msgType,
  3032. word16* pSz);
  3033. WOLFSSL_LOCAL int TLSX_PreSharedKey_GetSizeBinders(PreSharedKey* list,
  3034. byte msgType, word16* pSz);
  3035. WOLFSSL_LOCAL int TLSX_PreSharedKey_Use(TLSX** extensions, const byte* identity,
  3036. word16 len, word32 age, byte hmac,
  3037. byte cipherSuite0, byte cipherSuite,
  3038. byte resumption,
  3039. PreSharedKey **preSharedKey,
  3040. void* heap);
  3041. WOLFSSL_LOCAL int TLSX_PreSharedKey_Parse_ClientHello(TLSX** extensions,
  3042. const byte* input, word16 length, void* heap);
  3043. /* The possible Pre-Shared Key key exchange modes. */
  3044. enum PskKeyExchangeMode {
  3045. PSK_KE,
  3046. PSK_DHE_KE
  3047. };
  3048. /* User can define this. */
  3049. #ifndef WOLFSSL_DEF_PSK_CIPHER
  3050. #define WOLFSSL_DEF_PSK_CIPHER TLS_AES_128_GCM_SHA256
  3051. #endif
  3052. WOLFSSL_LOCAL int TLSX_PskKeyModes_Use(WOLFSSL* ssl, byte modes);
  3053. WOLFSSL_LOCAL int TLSX_PskKeyModes_Parse_Modes(const byte* input, word16 length,
  3054. byte msgType, byte* modes);
  3055. #ifdef WOLFSSL_EARLY_DATA
  3056. WOLFSSL_LOCAL int TLSX_EarlyData_Use(WOLFSSL* ssl, word32 max, int is_response);
  3057. #endif
  3058. #endif /* HAVE_SESSION_TICKET || !NO_PSK */
  3059. /* The types of keys to derive for. */
  3060. enum DeriveKeyType {
  3061. no_key,
  3062. early_data_key,
  3063. handshake_key,
  3064. traffic_key,
  3065. update_traffic_key
  3066. };
  3067. WOLFSSL_LOCAL int DeriveEarlySecret(WOLFSSL* ssl);
  3068. WOLFSSL_LOCAL int DeriveHandshakeSecret(WOLFSSL* ssl);
  3069. WOLFSSL_LOCAL int DeriveTls13Keys(WOLFSSL* ssl, int secret, int side, int store);
  3070. WOLFSSL_LOCAL int DeriveMasterSecret(WOLFSSL* ssl);
  3071. WOLFSSL_LOCAL int DeriveResumptionPSK(WOLFSSL* ssl, byte* nonce, byte nonceLen, byte* secret);
  3072. WOLFSSL_LOCAL int DeriveResumptionSecret(WOLFSSL* ssl, byte* key);
  3073. WOLFSSL_LOCAL int Tls13_Exporter(WOLFSSL* ssl, unsigned char *out, size_t outLen,
  3074. const char *label, size_t labelLen,
  3075. const unsigned char *context, size_t contextLen);
  3076. /* The key update request values for KeyUpdate message. */
  3077. enum KeyUpdateRequest {
  3078. update_not_requested,
  3079. update_requested
  3080. };
  3081. #endif /* WOLFSSL_TLS13 */
  3082. #ifdef WOLFSSL_DTLS_CID
  3083. WOLFSSL_LOCAL void TLSX_ConnectionID_Free(byte* ext, void* heap);
  3084. WOLFSSL_LOCAL word16 TLSX_ConnectionID_Write(byte* ext, byte* output);
  3085. WOLFSSL_LOCAL word16 TLSX_ConnectionID_GetSize(byte* ext);
  3086. WOLFSSL_LOCAL int TLSX_ConnectionID_Use(WOLFSSL* ssl);
  3087. WOLFSSL_LOCAL int TLSX_ConnectionID_Parse(WOLFSSL* ssl, const byte* input,
  3088. word16 length, byte isRequest);
  3089. WOLFSSL_LOCAL void DtlsCIDOnExtensionsParsed(WOLFSSL* ssl);
  3090. WOLFSSL_LOCAL byte DtlsCIDCheck(WOLFSSL* ssl, const byte* input,
  3091. word16 inputSize);
  3092. #endif /* WOLFSSL_DTLS_CID */
  3093. #ifdef OPENSSL_EXTRA
  3094. enum SetCBIO {
  3095. WOLFSSL_CBIO_NONE = 0,
  3096. WOLFSSL_CBIO_RECV = 0x1,
  3097. WOLFSSL_CBIO_SEND = 0x2,
  3098. };
  3099. #endif
  3100. #ifdef WOLFSSL_STATIC_EPHEMERAL
  3101. /* contains static ephemeral keys */
  3102. typedef struct {
  3103. #ifndef NO_DH
  3104. DerBuffer* dhKey;
  3105. #endif
  3106. #ifdef HAVE_ECC
  3107. DerBuffer* ecKey;
  3108. #endif
  3109. #ifdef HAVE_CURVE25519
  3110. DerBuffer* x25519Key;
  3111. #endif
  3112. #ifdef HAVE_CURVE448
  3113. DerBuffer* x448Key;
  3114. #endif
  3115. } StaticKeyExchangeInfo_t;
  3116. #endif /* WOLFSSL_STATIC_EPHEMERAL */
  3117. /* wolfSSL context type */
  3118. struct WOLFSSL_CTX {
  3119. WOLFSSL_METHOD* method;
  3120. #ifdef SINGLE_THREADED
  3121. WC_RNG* rng; /* to be shared with WOLFSSL w/o locking */
  3122. #endif
  3123. wolfSSL_Ref ref;
  3124. int err; /* error code in case of mutex not created */
  3125. #ifndef NO_DH
  3126. buffer serverDH_P;
  3127. buffer serverDH_G;
  3128. #endif
  3129. #ifndef NO_CERTS
  3130. DerBuffer* certificate;
  3131. DerBuffer* certChain;
  3132. /* chain after self, in DER, with leading size for each cert */
  3133. #ifndef WOLFSSL_NO_CA_NAMES
  3134. WOLF_STACK_OF(WOLFSSL_X509_NAME)* client_ca_names;
  3135. #endif
  3136. #ifdef OPENSSL_EXTRA
  3137. WOLF_STACK_OF(WOLFSSL_X509)* x509Chain;
  3138. client_cert_cb CBClientCert; /* client certificate callback */
  3139. CertSetupCallback certSetupCb;
  3140. void* certSetupCbArg;
  3141. #endif
  3142. #ifdef WOLFSSL_TLS13
  3143. int certChainCnt;
  3144. #endif
  3145. DerBuffer* privateKey;
  3146. byte privateKeyType:6;
  3147. byte privateKeyId:1;
  3148. byte privateKeyLabel:1;
  3149. int privateKeySz;
  3150. int privateKeyDevId;
  3151. #ifdef OPENSSL_ALL
  3152. WOLFSSL_EVP_PKEY* privateKeyPKey;
  3153. #endif
  3154. WOLFSSL_CERT_MANAGER* cm; /* our cert manager, ctx owns SSL will use */
  3155. #endif
  3156. #ifdef KEEP_OUR_CERT
  3157. WOLFSSL_X509* ourCert; /* keep alive a X509 struct of cert */
  3158. int ownOurCert; /* Dispose of certificate if we own */
  3159. #endif
  3160. Suites* suites; /* make dynamic, user may not need/set */
  3161. void* heap; /* for user memory overrides */
  3162. byte verifyDepth;
  3163. byte verifyPeer:1;
  3164. byte verifyNone:1;
  3165. byte failNoCert:1;
  3166. byte failNoCertxPSK:1; /* fail if no cert with the exception of PSK*/
  3167. byte sessionCacheOff:1;
  3168. byte sessionCacheFlushOff:1;
  3169. #ifdef HAVE_EXT_CACHE
  3170. byte internalCacheOff:1;
  3171. byte internalCacheLookupOff:1;
  3172. #endif
  3173. byte sendVerify:2; /* for client side (can not be single bit) */
  3174. byte haveRSA:1; /* RSA available */
  3175. byte haveECC:1; /* ECC available */
  3176. byte haveDH:1; /* server DH parms set by user */
  3177. byte haveECDSAsig:1; /* server cert signed w/ ECDSA */
  3178. byte haveFalconSig:1; /* server cert signed w/ Falcon */
  3179. byte haveDilithiumSig:1;/* server cert signed w/ Dilithium */
  3180. byte haveStaticECC:1; /* static server ECC private key */
  3181. byte partialWrite:1; /* only one msg per write call */
  3182. byte autoRetry:1; /* retry read/write on a WANT_{READ|WRITE} */
  3183. byte quietShutdown:1; /* don't send close notify */
  3184. byte groupMessages:1; /* group handshake messages before sending */
  3185. byte minDowngrade; /* minimum downgrade version */
  3186. byte haveEMS:1; /* have extended master secret extension */
  3187. byte useClientOrder:1; /* Use client's cipher preference order */
  3188. #if defined(HAVE_SESSION_TICKET)
  3189. byte noTicketTls12:1; /* TLS 1.2 server won't send ticket */
  3190. #endif
  3191. #ifdef WOLFSSL_TLS13
  3192. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  3193. unsigned int maxTicketTls13; /* maximum number of tickets to send */
  3194. #endif
  3195. byte noTicketTls13:1; /* TLS 1.3 Server won't create new Ticket */
  3196. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  3197. byte noPskDheKe:1; /* Don't use (EC)DHE with PSK */
  3198. #ifdef HAVE_SUPPORTED_CURVES
  3199. byte onlyPskDheKe:1; /* Only use (EC)DHE with PSK */
  3200. #endif
  3201. #endif
  3202. #endif /* WOLFSSL_TLS13 */
  3203. byte mutualAuth:1; /* Mutual authentication required */
  3204. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  3205. byte postHandshakeAuth:1; /* Post-handshake auth supported. */
  3206. byte verifyPostHandshake:1; /* Only send client cert req post
  3207. * handshake, not also during */
  3208. #endif
  3209. #ifndef NO_DH
  3210. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \
  3211. !defined(HAVE_SELFTEST)
  3212. byte dhKeyTested:1; /* Set when key has been tested. */
  3213. #endif
  3214. #endif
  3215. #if defined(HAVE_SECURE_RENEGOTIATION) || defined(HAVE_SERVER_RENEGOTIATION_INFO)
  3216. byte useSecureReneg:1; /* when set will set WOLFSSL objects generated to enable */
  3217. #endif
  3218. #ifdef HAVE_ENCRYPT_THEN_MAC
  3219. byte disallowEncThenMac:1; /* Don't do Encrypt-Then-MAC */
  3220. #endif
  3221. #ifdef WOLFSSL_STATIC_MEMORY
  3222. byte onHeapHint:1; /* whether the ctx/method is put on heap hint */
  3223. #endif
  3224. #if defined(WOLFSSL_STATIC_EPHEMERAL) && !defined(SINGLE_THREADED)
  3225. byte staticKELockInit:1;
  3226. #endif
  3227. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SCTP)
  3228. byte dtlsSctp:1; /* DTLS-over-SCTP mode */
  3229. #endif
  3230. word16 minProto:1; /* sets min to min available */
  3231. word16 maxProto:1; /* sets max to max available */
  3232. #if defined(HAVE_RPK)
  3233. RpkConfig rpkConfig;
  3234. RpkState rpkState;
  3235. #endif /* HAVE_RPK */
  3236. #ifdef WOLFSSL_SRTP
  3237. word16 dtlsSrtpProfiles; /* DTLS-with-SRTP mode
  3238. * (list of selected profiles - up to 16) */
  3239. #endif
  3240. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST)
  3241. byte haveMcast; /* multicast requested */
  3242. byte mcastID; /* multicast group ID */
  3243. #endif
  3244. #if defined(WOLFSSL_DTLS) && \
  3245. (defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU))
  3246. word16 dtlsMtuSz; /* DTLS MTU size */
  3247. #endif
  3248. #ifndef NO_DH
  3249. word16 minDhKeySz; /* minimum DH key size */
  3250. word16 maxDhKeySz; /* maximum DH key size */
  3251. #endif
  3252. #ifndef NO_RSA
  3253. short minRsaKeySz; /* minimum RSA key size */
  3254. #endif
  3255. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  3256. short minEccKeySz; /* minimum ECC key size */
  3257. #endif
  3258. #ifdef HAVE_PQC
  3259. short minFalconKeySz; /* minimum Falcon key size */
  3260. short minDilithiumKeySz;/* minimum Dilithium key size */
  3261. #endif
  3262. unsigned long mask; /* store SSL_OP_ flags */
  3263. #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
  3264. word32 disabledCurves; /* curves disabled by user */
  3265. #endif
  3266. #ifdef OPENSSL_EXTRA
  3267. byte sessionCtx[ID_LEN]; /* app session context ID */
  3268. const unsigned char *alpn_cli_protos;/* ALPN client protocol list */
  3269. unsigned int alpn_cli_protos_len;
  3270. byte sessionCtxSz;
  3271. byte cbioFlag; /* WOLFSSL_CBIO_RECV/SEND: CBIORecv/Send is set */
  3272. CallbackInfoState* CBIS; /* used to get info about SSL state */
  3273. WOLFSSL_X509_VERIFY_PARAM* param; /* verification parameters*/
  3274. #endif
  3275. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  3276. NetworkFilterCallback_t AcceptFilter;
  3277. void *AcceptFilter_arg;
  3278. NetworkFilterCallback_t ConnectFilter;
  3279. void *ConnectFilter_arg;
  3280. #endif /* WOLFSSL_WOLFSENTRY_HOOKS */
  3281. CallbackIORecv CBIORecv;
  3282. CallbackIOSend CBIOSend;
  3283. #ifdef WOLFSSL_DTLS
  3284. CallbackGenCookie CBIOCookie; /* gen cookie callback */
  3285. #endif /* WOLFSSL_DTLS */
  3286. #ifdef WOLFSSL_SESSION_EXPORT
  3287. #ifdef WOLFSSL_DTLS
  3288. wc_dtls_export dtls_export; /* export function for DTLS session */
  3289. #endif
  3290. CallbackGetPeer CBGetPeer;
  3291. CallbackSetPeer CBSetPeer;
  3292. #endif
  3293. VerifyCallback verifyCallback; /* cert verification callback */
  3294. void* verifyCbCtx; /* cert verify callback user ctx*/
  3295. #ifdef OPENSSL_ALL
  3296. CertVerifyCallback verifyCertCb;
  3297. void* verifyCertCbArg;
  3298. #endif /* OPENSSL_ALL */
  3299. #ifdef OPENSSL_EXTRA
  3300. SSL_Msg_Cb protoMsgCb; /* inspect protocol message callback */
  3301. void* protoMsgCtx; /* user set context with msg callback */
  3302. #endif
  3303. word32 timeout; /* session timeout */
  3304. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_CURVE25519) || \
  3305. defined(HAVE_ED448)
  3306. word32 ecdhCurveOID; /* curve Ecc_Sum */
  3307. #endif
  3308. #ifdef HAVE_ECC
  3309. word16 eccTempKeySz; /* in octets 20 - 66 */
  3310. #endif
  3311. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  3312. word32 pkCurveOID; /* curve Ecc_Sum */
  3313. #endif
  3314. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  3315. byte havePSK; /* psk key set by user */
  3316. wc_psk_client_callback client_psk_cb; /* client callback */
  3317. wc_psk_server_callback server_psk_cb; /* server callback */
  3318. #ifdef WOLFSSL_TLS13
  3319. wc_psk_client_cs_callback client_psk_cs_cb; /* client callback */
  3320. wc_psk_client_tls13_callback client_psk_tls13_cb; /* client callback */
  3321. wc_psk_server_tls13_callback server_psk_tls13_cb; /* server callback */
  3322. #endif
  3323. void* psk_ctx;
  3324. char server_hint[MAX_PSK_ID_LEN + NULL_TERM_LEN];
  3325. #endif /* HAVE_SESSION_TICKET || !NO_PSK */
  3326. #ifdef WOLFSSL_TLS13
  3327. word16 group[WOLFSSL_MAX_GROUP_COUNT];
  3328. byte numGroups;
  3329. #endif
  3330. #ifdef WOLFSSL_EARLY_DATA
  3331. word32 maxEarlyDataSz;
  3332. #endif
  3333. #ifdef HAVE_ANON
  3334. byte haveAnon; /* User wants to allow Anon suites */
  3335. #endif /* HAVE_ANON */
  3336. #ifdef WOLFSSL_ENCRYPTED_KEYS
  3337. wc_pem_password_cb* passwd_cb;
  3338. void* passwd_userdata;
  3339. #endif
  3340. #ifdef WOLFSSL_LOCAL_X509_STORE
  3341. WOLFSSL_X509_STORE x509_store; /* points to ctx->cm */
  3342. WOLFSSL_X509_STORE* x509_store_pt; /* take ownership of external store */
  3343. #endif
  3344. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || defined(WOLFSSL_WPAS_SMALL)
  3345. byte readAhead;
  3346. void* userPRFArg; /* passed to prf callback */
  3347. #endif
  3348. #ifdef HAVE_EX_DATA
  3349. WOLFSSL_CRYPTO_EX_DATA ex_data;
  3350. #endif
  3351. #if defined(HAVE_ALPN) && (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  3352. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY) || defined(WOLFSSL_QUIC))
  3353. CallbackALPNSelect alpnSelect;
  3354. void* alpnSelectArg;
  3355. #endif
  3356. #ifdef HAVE_SNI
  3357. CallbackSniRecv sniRecvCb;
  3358. void* sniRecvCbArg;
  3359. #endif
  3360. #if defined(WOLFSSL_MULTICAST) && defined(WOLFSSL_DTLS)
  3361. CallbackMcastHighwater mcastHwCb; /* Sequence number highwater callback */
  3362. word32 mcastFirstSeq; /* first trigger level */
  3363. word32 mcastSecondSeq; /* second trigger level */
  3364. word32 mcastMaxSeq; /* max level */
  3365. #endif
  3366. #ifdef HAVE_OCSP
  3367. WOLFSSL_OCSP ocsp;
  3368. #endif
  3369. int devId; /* async device id to use */
  3370. #ifdef HAVE_TLS_EXTENSIONS
  3371. TLSX* extensions; /* RFC 6066 TLS Extensions data */
  3372. #ifndef NO_WOLFSSL_SERVER
  3373. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  3374. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3375. OcspRequest* certOcspRequest;
  3376. #endif
  3377. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3378. OcspRequest* chainOcspRequest[MAX_CHAIN_DEPTH];
  3379. #endif
  3380. #endif
  3381. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  3382. SessionTicketEncCb ticketEncCb; /* enc/dec session ticket Cb */
  3383. void* ticketEncCtx; /* session encrypt context */
  3384. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) \
  3385. || defined(OPENSSL_EXTRA) || defined(HAVE_LIGHTY)
  3386. ticketCompatCb ticketEncWrapCb; /* callback for OpenSSL ticket key callback */
  3387. #endif
  3388. int ticketHint; /* ticket hint in seconds */
  3389. #ifndef WOLFSSL_NO_DEF_TICKET_ENC_CB
  3390. TicketEncCbCtx ticketKeyCtx;
  3391. #endif
  3392. #endif
  3393. #endif
  3394. #ifdef HAVE_SUPPORTED_CURVES
  3395. byte userCurves; /* indicates user called wolfSSL_CTX_UseSupportedCurve */
  3396. #endif
  3397. #ifdef ATOMIC_USER
  3398. CallbackMacEncrypt MacEncryptCb; /* Atomic User Mac/Encrypt Cb */
  3399. CallbackDecryptVerify DecryptVerifyCb; /* Atomic User Decrypt/Verify Cb */
  3400. #ifdef HAVE_ENCRYPT_THEN_MAC
  3401. CallbackEncryptMac EncryptMacCb; /* Atomic User Mac/Enc Cb */
  3402. CallbackVerifyDecrypt VerifyDecryptCb; /* Atomic User Dec/Verify Cb */
  3403. #endif
  3404. #endif
  3405. #ifdef HAVE_PK_CALLBACKS
  3406. #ifdef HAVE_ECC
  3407. CallbackEccKeyGen EccKeyGenCb; /* User EccKeyGen Callback Handler */
  3408. CallbackEccSign EccSignCb; /* User EccSign Callback handler */
  3409. void* EccSignCtx; /* Ecc Sign Callback Context */
  3410. CallbackEccVerify EccVerifyCb; /* User EccVerify Callback handler */
  3411. CallbackEccSharedSecret EccSharedSecretCb; /* User EccVerify Callback handler */
  3412. #endif /* HAVE_ECC */
  3413. #ifdef HAVE_HKDF
  3414. CallbackHKDFExtract HkdfExtractCb; /* User hkdf Extract Callback handler */
  3415. #endif
  3416. #ifdef HAVE_ED25519
  3417. /* User Ed25519Sign Callback handler */
  3418. CallbackEd25519Sign Ed25519SignCb;
  3419. /* User Ed25519Verify Callback handler */
  3420. CallbackEd25519Verify Ed25519VerifyCb;
  3421. #endif
  3422. #ifdef HAVE_CURVE25519
  3423. /* User X25519 KeyGen Callback Handler */
  3424. CallbackX25519KeyGen X25519KeyGenCb;
  3425. /* User X25519 SharedSecret Callback handler */
  3426. CallbackX25519SharedSecret X25519SharedSecretCb;
  3427. #endif
  3428. #ifdef HAVE_ED448
  3429. /* User Ed448Sign Callback handler */
  3430. CallbackEd448Sign Ed448SignCb;
  3431. /* User Ed448Verify Callback handler */
  3432. CallbackEd448Verify Ed448VerifyCb;
  3433. #endif
  3434. #ifdef HAVE_CURVE448
  3435. /* User X448 KeyGen Callback Handler */
  3436. CallbackX448KeyGen X448KeyGenCb;
  3437. /* User X448 SharedSecret Callback handler */
  3438. CallbackX448SharedSecret X448SharedSecretCb;
  3439. #endif
  3440. #ifndef NO_DH
  3441. /* User DH KeyGen Callback handler*/
  3442. CallbackDhGenerateKeyPair DhGenerateKeyPairCb;
  3443. /* User DH Agree Callback handler */
  3444. CallbackDhAgree DhAgreeCb;
  3445. #endif
  3446. #ifndef NO_RSA
  3447. /* User RsaSign Callback handler (priv key) */
  3448. CallbackRsaSign RsaSignCb;
  3449. /* User RsaVerify Callback handler (pub key) */
  3450. CallbackRsaVerify RsaVerifyCb;
  3451. /* User VerifyRsaSign Callback handler (priv key) */
  3452. CallbackRsaVerify RsaSignCheckCb;
  3453. #ifdef WC_RSA_PSS
  3454. /* User RsaSign (priv key) */
  3455. CallbackRsaPssSign RsaPssSignCb;
  3456. /* User RsaVerify (pub key) */
  3457. CallbackRsaPssVerify RsaPssVerifyCb;
  3458. /* User VerifyRsaSign (priv key) */
  3459. CallbackRsaPssVerify RsaPssSignCheckCb;
  3460. #endif
  3461. CallbackRsaEnc RsaEncCb; /* User Rsa Public Encrypt handler */
  3462. CallbackRsaDec RsaDecCb; /* User Rsa Private Decrypt handler */
  3463. #endif /* NO_RSA */
  3464. /* User generate pre-master handler */
  3465. CallbackGenPreMaster GenPreMasterCb;
  3466. /* User generate master secret handler */
  3467. CallbackGenMasterSecret GenMasterCb;
  3468. /* User generate session key handler */
  3469. CallbackGenSessionKey GenSessionKeyCb;
  3470. /* User setting encrypt keys handler */
  3471. CallbackEncryptKeys EncryptKeysCb;
  3472. /* User Tls finished handler */
  3473. CallbackTlsFinished TlsFinishedCb;
  3474. #if !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_AEAD_ONLY)
  3475. /* User Verify mac handler */
  3476. CallbackVerifyMac VerifyMacCb;
  3477. #endif
  3478. #if defined(WOLFSSL_PUBLIC_ASN)
  3479. /* User handler to process a certificate */
  3480. CallbackProcessPeerCert ProcessPeerCertCb;
  3481. #endif
  3482. /* User handler to process the server's key exchange public key */
  3483. CallbackProcessServerSigKex ProcessServerSigKexCb;
  3484. /* User handler to process the TLS record */
  3485. CallbackPerformTlsRecordProcessing PerformTlsRecordProcessingCb;
  3486. /* User handler to do HKDF expansions */
  3487. CallbackHKDFExpandLabel HKDFExpandLabelCb;
  3488. #endif /* HAVE_PK_CALLBACKS */
  3489. #ifdef HAVE_WOLF_EVENT
  3490. WOLF_EVENT_QUEUE event_queue;
  3491. #endif /* HAVE_WOLF_EVENT */
  3492. #ifdef HAVE_EXT_CACHE
  3493. WOLFSSL_SESSION*(*get_sess_cb)(WOLFSSL*, const unsigned char*, int, int*);
  3494. int (*new_sess_cb)(WOLFSSL*, WOLFSSL_SESSION*);
  3495. #endif
  3496. #if defined(HAVE_EXT_CACHE) || defined(HAVE_EX_DATA)
  3497. Rem_Sess_Cb rem_sess_cb;
  3498. #endif
  3499. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && !defined(NO_SHA256)
  3500. Srp* srp; /* TLS Secure Remote Password Protocol*/
  3501. byte* srp_password;
  3502. #endif
  3503. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  3504. wolfSSL_CTX_keylog_cb_func keyLogCb;
  3505. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  3506. #ifdef WOLFSSL_STATIC_EPHEMERAL
  3507. StaticKeyExchangeInfo_t staticKE;
  3508. #ifndef SINGLE_THREADED
  3509. wolfSSL_Mutex staticKELock;
  3510. #endif
  3511. #endif
  3512. #ifdef WOLFSSL_QUIC
  3513. struct {
  3514. const WOLFSSL_QUIC_METHOD *method;
  3515. } quic;
  3516. #endif
  3517. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  3518. WOLFSSL_EchConfig* echConfigs;
  3519. #endif
  3520. };
  3521. WOLFSSL_LOCAL
  3522. int InitSSL_Ctx(WOLFSSL_CTX* ctx, WOLFSSL_METHOD* method, void* heap);
  3523. WOLFSSL_LOCAL
  3524. void FreeSSL_Ctx(WOLFSSL_CTX* ctx);
  3525. WOLFSSL_LOCAL
  3526. void SSL_CtxResourceFree(WOLFSSL_CTX* ctx);
  3527. #ifdef HAVE_EX_DATA_CLEANUP_HOOKS
  3528. #ifndef HAVE_EX_DATA
  3529. #error "HAVE_EX_DATA_CLEANUP_HOOKS requires HAVE_EX_DATA to be defined"
  3530. #endif
  3531. void wolfSSL_CRYPTO_cleanup_ex_data(WOLFSSL_CRYPTO_EX_DATA* ex_data);
  3532. #endif
  3533. WOLFSSL_LOCAL
  3534. int DeriveTlsKeys(WOLFSSL* ssl);
  3535. WOLFSSL_LOCAL
  3536. int ProcessOldClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
  3537. word32 inSz, word16 sz);
  3538. #ifndef NO_CERTS
  3539. WOLFSSL_LOCAL
  3540. int AddCA(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer, int type, int verify);
  3541. WOLFSSL_LOCAL
  3542. int AlreadySigner(WOLFSSL_CERT_MANAGER* cm, byte* hash);
  3543. #ifdef WOLFSSL_TRUST_PEER_CERT
  3544. WOLFSSL_LOCAL
  3545. int AddTrustedPeer(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer, int verify);
  3546. WOLFSSL_LOCAL
  3547. int AlreadyTrustedPeer(WOLFSSL_CERT_MANAGER* cm, DecodedCert* cert);
  3548. #endif
  3549. #endif
  3550. /* All cipher suite related info
  3551. * Keep as a constant size (no ifdefs) for session export */
  3552. typedef struct CipherSpecs {
  3553. word16 key_size;
  3554. word16 iv_size;
  3555. word16 block_size;
  3556. word16 aead_mac_size;
  3557. byte bulk_cipher_algorithm;
  3558. byte cipher_type; /* block, stream, or aead */
  3559. byte mac_algorithm;
  3560. byte kea; /* key exchange algo */
  3561. byte sig_algo;
  3562. byte hash_size;
  3563. byte pad_size;
  3564. byte static_ecdh;
  3565. } CipherSpecs;
  3566. void InitCipherSpecs(CipherSpecs* cs);
  3567. /* Supported Key Exchange Protocols */
  3568. enum KeyExchangeAlgorithm {
  3569. no_kea,
  3570. rsa_kea,
  3571. diffie_hellman_kea,
  3572. fortezza_kea,
  3573. psk_kea,
  3574. dhe_psk_kea,
  3575. ecdhe_psk_kea,
  3576. ecc_diffie_hellman_kea,
  3577. ecc_static_diffie_hellman_kea /* for verify suite only */
  3578. };
  3579. /* Used with InitSuitesHashSigAlgo_ex2 */
  3580. #define SIG_ECDSA 0x01
  3581. #define SIG_RSA 0x02
  3582. #define SIG_SM2 0x04
  3583. #define SIG_FALCON 0x08
  3584. #define SIG_DILITHIUM 0x10
  3585. #define SIG_ANON 0x20
  3586. /* Supported Authentication Schemes */
  3587. enum SignatureAlgorithm {
  3588. anonymous_sa_algo = 0,
  3589. rsa_sa_algo = 1,
  3590. dsa_sa_algo = 2,
  3591. ecc_dsa_sa_algo = 3,
  3592. rsa_pss_sa_algo = 8,
  3593. ed25519_sa_algo = 9,
  3594. rsa_pss_pss_algo = 10,
  3595. ed448_sa_algo = 11,
  3596. falcon_level1_sa_algo = 12,
  3597. falcon_level5_sa_algo = 13,
  3598. dilithium_level2_sa_algo = 14,
  3599. dilithium_level3_sa_algo = 15,
  3600. dilithium_level5_sa_algo = 16,
  3601. sm2_sa_algo = 17,
  3602. invalid_sa_algo = 255
  3603. };
  3604. #define PSS_RSAE_TO_PSS_PSS(macAlgo) \
  3605. ((macAlgo) + (pss_sha256 - sha256_mac))
  3606. #define PSS_PSS_HASH_TO_MAC(macAlgo) \
  3607. ((macAlgo) - (pss_sha256 - sha256_mac))
  3608. enum SigAlgRsaPss {
  3609. pss_sha256 = 0x09,
  3610. pss_sha384 = 0x0a,
  3611. pss_sha512 = 0x0b,
  3612. };
  3613. #ifdef WOLFSSL_SM2
  3614. /* Default SM2 signature ID. */
  3615. #define TLS12_SM2_SIG_ID ((byte*)"1234567812345678")
  3616. /* Length of default SM2 signature ID. */
  3617. #define TLS12_SM2_SIG_ID_SZ 16
  3618. /* https://www.rfc-editor.org/rfc/rfc8998.html#name-sm2-signature-scheme */
  3619. /* ID to use when signing/verifying TLS v1.3 data. */
  3620. #define TLS13_SM2_SIG_ID ((byte*)"TLSv1.3+GM+Cipher+Suite")
  3621. /* Length of ID to use when signing/verifying TLS v1.3 data. */
  3622. #define TLS13_SM2_SIG_ID_SZ 23
  3623. #endif
  3624. /* Supported ECC Curve Types */
  3625. enum EccCurves {
  3626. named_curve = 3
  3627. };
  3628. /* Valid client certificate request types from page 27 */
  3629. enum ClientCertificateType {
  3630. rsa_sign = 1,
  3631. dss_sign = 2,
  3632. rsa_fixed_dh = 3,
  3633. dss_fixed_dh = 4,
  3634. rsa_ephemeral_dh = 5,
  3635. dss_ephemeral_dh = 6,
  3636. fortezza_kea_cert = 20,
  3637. ecdsa_sign = 64,
  3638. rsa_fixed_ecdh = 65,
  3639. ecdsa_fixed_ecdh = 66,
  3640. falcon_sign = 67,
  3641. dilithium_sign = 68,
  3642. };
  3643. #ifndef WOLFSSL_AEAD_ONLY
  3644. enum CipherType { stream, block, aead };
  3645. #else
  3646. enum CipherType { aead };
  3647. #endif
  3648. #if defined(BUILD_AES) || defined(BUILD_AESGCM) || defined(HAVE_ARIA) || \
  3649. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(WOLFSSL_TLS13)
  3650. #define CIPHER_NONCE
  3651. #endif
  3652. #if defined(WOLFSSL_DTLS) && defined(HAVE_SECURE_RENEGOTIATION)
  3653. enum CipherSrc {
  3654. KEYS_NOT_SET = 0,
  3655. KEYS, /* keys from ssl->keys are loaded */
  3656. SCR /* keys from ssl->secure_renegotiation->tmp_keys are loaded */
  3657. };
  3658. #endif
  3659. #ifdef WOLFSSL_CIPHER_TEXT_CHECK
  3660. #ifndef WOLFSSL_CIPHER_CHECK_SZ
  3661. /* 64-bits to confirm encrypt operation worked */
  3662. #define WOLFSSL_CIPHER_CHECK_SZ 8
  3663. #endif
  3664. #endif
  3665. /* cipher for now */
  3666. typedef struct Ciphers {
  3667. #ifdef BUILD_ARC4
  3668. Arc4* arc4;
  3669. #endif
  3670. #ifdef BUILD_DES3
  3671. Des3* des3;
  3672. #endif
  3673. #if defined(BUILD_AES) || defined(BUILD_AESGCM)
  3674. Aes* aes;
  3675. #endif
  3676. #if (defined(BUILD_AESGCM) || defined(HAVE_AESCCM)) && !defined(WOLFSSL_NO_TLS12)
  3677. byte* additional;
  3678. #endif
  3679. #ifdef HAVE_ARIA
  3680. wc_Aria* aria;
  3681. #endif
  3682. #ifdef CIPHER_NONCE
  3683. byte* nonce;
  3684. #endif
  3685. #ifdef HAVE_CAMELLIA
  3686. Camellia* cam;
  3687. #endif
  3688. #ifdef HAVE_CHACHA
  3689. ChaCha* chacha;
  3690. #endif
  3691. #ifdef WOLFSSL_SM4
  3692. wc_Sm4* sm4;
  3693. #endif
  3694. #if defined(WOLFSSL_TLS13) && defined(HAVE_NULL_CIPHER) && !defined(NO_HMAC)
  3695. Hmac* hmac;
  3696. #endif
  3697. #ifdef WOLFSSL_CIPHER_TEXT_CHECK
  3698. word32 sanityCheck[WOLFSSL_CIPHER_CHECK_SZ/sizeof(word32)];
  3699. #endif
  3700. byte state;
  3701. byte setup; /* have we set it up flag for detection */
  3702. #if defined(WOLFSSL_DTLS) && defined(HAVE_SECURE_RENEGOTIATION)
  3703. enum CipherSrc src; /* DTLS uses this to determine which keys
  3704. * are currently loaded */
  3705. #endif
  3706. } Ciphers;
  3707. #ifdef WOLFSSL_DTLS13
  3708. typedef struct RecordNumberCiphers {
  3709. #if defined(BUILD_AES) || defined(BUILD_AESGCM)
  3710. Aes *aes;
  3711. #endif /* BUILD_AES || BUILD_AESGCM */
  3712. #ifdef HAVE_CHACHA
  3713. ChaCha *chacha;
  3714. #endif
  3715. } RecordNumberCiphers;
  3716. #endif /* WOLFSSL_DTLS13 */
  3717. #ifdef HAVE_ONE_TIME_AUTH
  3718. /* Ciphers for one time authentication such as poly1305 */
  3719. typedef struct OneTimeAuth {
  3720. #ifdef HAVE_POLY1305
  3721. Poly1305* poly1305;
  3722. #endif
  3723. byte setup; /* flag for if a cipher has been set */
  3724. } OneTimeAuth;
  3725. #endif
  3726. WOLFSSL_LOCAL void InitCiphers(WOLFSSL* ssl);
  3727. WOLFSSL_LOCAL void FreeCiphers(WOLFSSL* ssl);
  3728. /* hashes type */
  3729. typedef struct Hashes {
  3730. #if !defined(NO_MD5) && !defined(NO_OLD_TLS)
  3731. byte md5[WC_MD5_DIGEST_SIZE];
  3732. #endif
  3733. #if !defined(NO_SHA)
  3734. byte sha[WC_SHA_DIGEST_SIZE];
  3735. #endif
  3736. #ifndef NO_SHA256
  3737. byte sha256[WC_SHA256_DIGEST_SIZE];
  3738. #endif
  3739. #ifdef WOLFSSL_SHA384
  3740. byte sha384[WC_SHA384_DIGEST_SIZE];
  3741. #endif
  3742. #ifdef WOLFSSL_SHA512
  3743. byte sha512[WC_SHA512_DIGEST_SIZE];
  3744. #endif
  3745. #ifdef WOLFSSL_SM3
  3746. byte sm3[WC_SM3_DIGEST_SIZE];
  3747. #endif
  3748. } Hashes;
  3749. WOLFSSL_LOCAL int BuildCertHashes(const WOLFSSL* ssl, Hashes* hashes);
  3750. #ifdef WOLFSSL_TLS13
  3751. typedef union Digest {
  3752. #ifndef NO_WOLFSSL_SHA256
  3753. wc_Sha256 sha256;
  3754. #endif
  3755. #ifdef WOLFSSL_SHA384
  3756. wc_Sha384 sha384;
  3757. #endif
  3758. #ifdef WOLFSSL_SHA512
  3759. wc_Sha512 sha512;
  3760. #endif
  3761. #ifdef WOLFSSL_SM3
  3762. wc_Sm3 sm3;
  3763. #endif
  3764. } Digest;
  3765. #endif
  3766. /* Static x509 buffer */
  3767. typedef struct x509_buffer {
  3768. int length; /* actual size */
  3769. byte buffer[MAX_X509_SIZE]; /* max static cert size */
  3770. } x509_buffer;
  3771. /* wolfSSL X509_CHAIN, for no dynamic memory SESSION_CACHE */
  3772. struct WOLFSSL_X509_CHAIN {
  3773. int count; /* total number in chain */
  3774. x509_buffer certs[MAX_CHAIN_DEPTH]; /* only allow max depth 4 for now */
  3775. };
  3776. typedef enum WOLFSSL_SESSION_TYPE {
  3777. WOLFSSL_SESSION_TYPE_UNKNOWN,
  3778. WOLFSSL_SESSION_TYPE_SSL, /* in ssl->session */
  3779. WOLFSSL_SESSION_TYPE_CACHE, /* pointer to internal cache */
  3780. WOLFSSL_SESSION_TYPE_HEAP /* allocated from heap SESSION_new */
  3781. } WOLFSSL_SESSION_TYPE;
  3782. #ifdef WOLFSSL_QUIC
  3783. typedef struct QuicRecord QuicRecord;
  3784. typedef struct QuicRecord {
  3785. struct QuicRecord *next;
  3786. uint8_t *data;
  3787. word32 capacity;
  3788. word32 len;
  3789. word32 start;
  3790. word32 end;
  3791. WOLFSSL_ENCRYPTION_LEVEL level;
  3792. word32 rec_hdr_remain;
  3793. } QuicEncData;
  3794. typedef struct QuicTransportParam QuicTransportParam;
  3795. struct QuicTransportParam {
  3796. const uint8_t *data;
  3797. word16 len;
  3798. };
  3799. WOLFSSL_LOCAL const QuicTransportParam *QuicTransportParam_new(const uint8_t *data, size_t len, void *heap);
  3800. WOLFSSL_LOCAL const QuicTransportParam *QuicTransportParam_dup(const QuicTransportParam *tp, void *heap);
  3801. WOLFSSL_LOCAL void QuicTransportParam_free(const QuicTransportParam *tp, void *heap);
  3802. WOLFSSL_LOCAL int TLSX_QuicTP_Use(WOLFSSL* ssl, TLSX_Type ext_type, int is_response);
  3803. WOLFSSL_LOCAL int wolfSSL_quic_add_transport_extensions(WOLFSSL *ssl, int msg_type);
  3804. #define QTP_FREE QuicTransportParam_free
  3805. #endif /* WOLFSSL_QUIC */
  3806. /** Session Ticket - RFC 5077 (session 3.2) */
  3807. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  3808. /* Ticket nonce - for deriving PSK.
  3809. Length allowed to be: 1..255. Only support
  3810. * TLS13_TICKET_NONCE_STATIC_SZ length bytes.
  3811. */
  3812. typedef struct TicketNonce {
  3813. byte len;
  3814. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && \
  3815. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  3816. byte *data;
  3817. byte dataStatic[MAX_TICKET_NONCE_STATIC_SZ];
  3818. #else
  3819. byte data[MAX_TICKET_NONCE_STATIC_SZ];
  3820. #endif /* WOLFSSL_TICKET_NONCE_MALLOC && FIPS_VERSION_GE(5,3) */
  3821. } TicketNonce;
  3822. #endif
  3823. /* wolfSSL session type */
  3824. struct WOLFSSL_SESSION {
  3825. /* WARNING Do not add fields here. They will be ignored in
  3826. * wolfSSL_DupSession. */
  3827. WOLFSSL_SESSION_TYPE type;
  3828. #ifndef NO_SESSION_CACHE
  3829. int cacheRow; /* row in session cache */
  3830. #endif
  3831. wolfSSL_Ref ref;
  3832. byte altSessionID[ID_LEN];
  3833. byte haveAltSessionID:1;
  3834. #ifdef HAVE_EX_DATA
  3835. byte ownExData:1;
  3836. #endif
  3837. #if defined(HAVE_EXT_CACHE) || defined(HAVE_EX_DATA)
  3838. Rem_Sess_Cb rem_sess_cb;
  3839. #endif
  3840. void* heap;
  3841. /* WARNING The above fields (up to and including the heap) are not copied
  3842. * in wolfSSL_DupSession. Place new fields after the heap
  3843. * member */
  3844. byte side; /* Either WOLFSSL_CLIENT_END or
  3845. WOLFSSL_SERVER_END */
  3846. word32 bornOn; /* create time in seconds */
  3847. word32 timeout; /* timeout in seconds */
  3848. byte sessionID[ID_LEN]; /* id for protocol or bogus
  3849. * ID for TLS 1.3 */
  3850. byte sessionIDSz;
  3851. byte masterSecret[SECRET_LEN]; /* stored secret */
  3852. word16 haveEMS; /* ext master secret flag */
  3853. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  3854. WOLFSSL_X509* peer; /* peer cert */
  3855. #endif
  3856. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  3857. defined(HAVE_SESSION_TICKET))
  3858. ProtocolVersion version; /* which version was used */
  3859. #endif
  3860. #if defined(SESSION_CERTS) || !defined(NO_RESUME_SUITE_CHECK) || \
  3861. (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET))
  3862. byte cipherSuite0; /* first byte, normally 0 */
  3863. byte cipherSuite; /* 2nd byte, actual suite */
  3864. #endif
  3865. #ifndef NO_CLIENT_CACHE
  3866. word16 idLen; /* serverID length */
  3867. byte serverID[SERVER_ID_LEN]; /* for easier client lookup */
  3868. #endif
  3869. #ifdef OPENSSL_EXTRA
  3870. byte sessionCtxSz; /* sessionCtx length */
  3871. byte sessionCtx[ID_LEN]; /* app specific context id */
  3872. #endif /* OPENSSL_EXTRA */
  3873. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  3874. byte peerVerifyRet; /* cert verify error */
  3875. #endif
  3876. #ifdef WOLFSSL_TLS13
  3877. word16 namedGroup;
  3878. #endif
  3879. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  3880. #ifdef WOLFSSL_TLS13
  3881. #ifdef WOLFSSL_32BIT_MILLI_TIME
  3882. word32 ticketSeen; /* Time ticket seen (ms) */
  3883. #else
  3884. sword64 ticketSeen; /* Time ticket seen (ms) */
  3885. #endif
  3886. word32 ticketAdd; /* Added by client */
  3887. TicketNonce ticketNonce; /* Nonce used to derive PSK */
  3888. #endif
  3889. #ifdef WOLFSSL_EARLY_DATA
  3890. word32 maxEarlyDataSz;
  3891. #endif
  3892. #endif
  3893. #ifdef HAVE_SESSION_TICKET
  3894. byte staticTicket[SESSION_TICKET_LEN];
  3895. byte* ticket;
  3896. word16 ticketLen;
  3897. word16 ticketLenAlloc; /* is dynamic */
  3898. #endif
  3899. #ifdef SESSION_CERTS
  3900. WOLFSSL_X509_CHAIN chain; /* peer cert chain, static */
  3901. #ifdef WOLFSSL_ALT_CERT_CHAINS
  3902. WOLFSSL_X509_CHAIN altChain; /* peer alt cert chain, static */
  3903. #endif
  3904. #endif
  3905. #ifdef HAVE_EX_DATA
  3906. WOLFSSL_CRYPTO_EX_DATA ex_data;
  3907. #endif
  3908. byte isSetup:1;
  3909. };
  3910. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) && \
  3911. defined(WOLFSSL_TICKET_NONCE_MALLOC) && \
  3912. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  3913. WOLFSSL_LOCAL int SessionTicketNoncePopulate(WOLFSSL_SESSION *session,
  3914. const byte* nonce, byte len);
  3915. #endif /* WOLFSSL_TLS13 && */
  3916. WOLFSSL_LOCAL int wolfSSL_RAND_Init(void);
  3917. WOLFSSL_LOCAL WOLFSSL_SESSION* wolfSSL_NewSession(void* heap);
  3918. WOLFSSL_LOCAL WOLFSSL_SESSION* wolfSSL_GetSession(
  3919. WOLFSSL* ssl, byte* masterSecret, byte restoreSessionCerts);
  3920. WOLFSSL_LOCAL void SetupSession(WOLFSSL* ssl);
  3921. WOLFSSL_LOCAL void AddSession(WOLFSSL* ssl);
  3922. /* use wolfSSL_API visibility to be able to test in tests/api.c */
  3923. WOLFSSL_API int AddSessionToCache(WOLFSSL_CTX* ctx,
  3924. WOLFSSL_SESSION* addSession, const byte* id, byte idSz, int* sessionIndex,
  3925. int side, word16 useTicket, ClientSession** clientCacheEntry);
  3926. #ifndef NO_CLIENT_CACHE
  3927. WOLFSSL_LOCAL ClientSession* AddSessionToClientCache(int side, int row, int idx,
  3928. byte* serverID, word16 idLen, const byte* sessionID,
  3929. word16 useTicket);
  3930. #endif
  3931. WOLFSSL_LOCAL
  3932. WOLFSSL_SESSION* ClientSessionToSession(const WOLFSSL_SESSION* session);
  3933. WOLFSSL_LOCAL void TlsSessionCacheUnlockRow(word32 row);
  3934. WOLFSSL_LOCAL int TlsSessionCacheGetAndRdLock(const byte *id,
  3935. const WOLFSSL_SESSION **sess, word32 *lockedRow, byte side);
  3936. WOLFSSL_LOCAL int TlsSessionCacheGetAndWrLock(const byte *id,
  3937. WOLFSSL_SESSION **sess, word32 *lockedRow, byte side);
  3938. WOLFSSL_LOCAL void EvictSessionFromCache(WOLFSSL_SESSION* session);
  3939. /* WOLFSSL_API to test it in tests/api.c */
  3940. WOLFSSL_API int wolfSSL_GetSessionFromCache(WOLFSSL* ssl, WOLFSSL_SESSION* output);
  3941. WOLFSSL_LOCAL int wolfSSL_SetSession(WOLFSSL* ssl, WOLFSSL_SESSION* session);
  3942. WOLFSSL_LOCAL void wolfSSL_FreeSession(WOLFSSL_CTX* ctx,
  3943. WOLFSSL_SESSION* session);
  3944. WOLFSSL_LOCAL int wolfSSL_DupSession(const WOLFSSL_SESSION* input,
  3945. WOLFSSL_SESSION* output, int avoidSysCalls);
  3946. typedef int (*hmacfp) (WOLFSSL*, byte*, const byte*, word32, int, int, int, int);
  3947. #ifndef NO_CLIENT_CACHE
  3948. WOLFSSL_LOCAL WOLFSSL_SESSION* wolfSSL_GetSessionClient(
  3949. WOLFSSL* ssl, const byte* id, int len);
  3950. #endif
  3951. /* client connect state for nonblocking restart */
  3952. enum ConnectState {
  3953. CONNECT_BEGIN = 0,
  3954. CLIENT_HELLO_SENT,
  3955. HELLO_AGAIN, /* HELLO_AGAIN s for DTLS case */
  3956. HELLO_AGAIN_REPLY,
  3957. FIRST_REPLY_DONE,
  3958. FIRST_REPLY_FIRST,
  3959. FIRST_REPLY_SECOND,
  3960. FIRST_REPLY_THIRD,
  3961. FIRST_REPLY_FOURTH,
  3962. FINISHED_DONE,
  3963. SECOND_REPLY_DONE,
  3964. #ifdef WOLFSSL_DTLS13
  3965. WAIT_FINISHED_ACK
  3966. #endif /* WOLFSSL_DTLS13 */
  3967. };
  3968. /* server accept state for nonblocking restart */
  3969. enum AcceptState {
  3970. ACCEPT_BEGIN = 0,
  3971. ACCEPT_BEGIN_RENEG,
  3972. ACCEPT_CLIENT_HELLO_DONE,
  3973. ACCEPT_HELLO_RETRY_REQUEST_DONE,
  3974. ACCEPT_FIRST_REPLY_DONE,
  3975. SERVER_HELLO_SENT,
  3976. CERT_SENT,
  3977. CERT_VERIFY_SENT,
  3978. CERT_STATUS_SENT,
  3979. KEY_EXCHANGE_SENT,
  3980. CERT_REQ_SENT,
  3981. SERVER_HELLO_DONE,
  3982. ACCEPT_SECOND_REPLY_DONE,
  3983. TICKET_SENT,
  3984. CHANGE_CIPHER_SENT,
  3985. ACCEPT_FINISHED_DONE,
  3986. ACCEPT_THIRD_REPLY_DONE
  3987. };
  3988. /* TLS 1.3 server accept state for nonblocking restart */
  3989. enum AcceptStateTls13 {
  3990. TLS13_ACCEPT_BEGIN = 0,
  3991. TLS13_ACCEPT_BEGIN_RENEG,
  3992. TLS13_ACCEPT_CLIENT_HELLO_DONE,
  3993. TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE,
  3994. TLS13_ACCEPT_FIRST_REPLY_DONE,
  3995. TLS13_ACCEPT_SECOND_REPLY_DONE,
  3996. TLS13_SERVER_HELLO_SENT,
  3997. TLS13_ACCEPT_THIRD_REPLY_DONE,
  3998. TLS13_SERVER_EXTENSIONS_SENT,
  3999. TLS13_CERT_REQ_SENT,
  4000. TLS13_CERT_SENT,
  4001. TLS13_CERT_VERIFY_SENT,
  4002. TLS13_ACCEPT_FINISHED_SENT,
  4003. TLS13_PRE_TICKET_SENT,
  4004. TLS13_ACCEPT_FINISHED_DONE,
  4005. TLS13_TICKET_SENT
  4006. };
  4007. /* buffers for struct WOLFSSL */
  4008. typedef struct Buffers {
  4009. bufferStatic inputBuffer;
  4010. bufferStatic outputBuffer;
  4011. buffer domainName; /* for client check */
  4012. buffer clearOutputBuffer;
  4013. buffer sig; /* signature data */
  4014. buffer digest; /* digest data */
  4015. int prevSent; /* previous plain text bytes sent
  4016. when got WANT_WRITE */
  4017. int plainSz; /* plain text bytes in buffer to send
  4018. when got WANT_WRITE */
  4019. byte weOwnCert; /* SSL own cert flag */
  4020. byte weOwnCertChain; /* SSL own cert chain flag */
  4021. byte weOwnKey; /* SSL own key flag */
  4022. byte weOwnDH; /* SSL own dh (p,g) flag */
  4023. #ifndef NO_DH
  4024. buffer serverDH_P; /* WOLFSSL_CTX owns, unless we own */
  4025. buffer serverDH_G; /* WOLFSSL_CTX owns, unless we own */
  4026. buffer serverDH_Pub;
  4027. buffer serverDH_Priv;
  4028. DhKey* serverDH_Key;
  4029. #endif
  4030. #ifndef NO_CERTS
  4031. DerBuffer* certificate; /* WOLFSSL_CTX owns, unless we own */
  4032. DerBuffer* key; /* WOLFSSL_CTX owns, unless we own */
  4033. byte keyType:6; /* Type of key: RSA, ECC, Ed25519 */
  4034. byte keyId:1; /* Key data is an id not data */
  4035. byte keyLabel:1; /* Key data is a label not data */
  4036. int keySz; /* Size of RSA key */
  4037. int keyDevId; /* Device Id for key */
  4038. DerBuffer* certChain; /* WOLFSSL_CTX owns, unless we own */
  4039. /* chain after self, in DER, with leading size for each cert */
  4040. #ifdef WOLFSSL_TLS13
  4041. int certChainCnt;
  4042. DerBuffer* certExts;
  4043. #endif
  4044. #endif
  4045. #ifdef WOLFSSL_SEND_HRR_COOKIE
  4046. buffer tls13CookieSecret; /* HRR cookie secret */
  4047. #endif
  4048. #ifdef WOLFSSL_DTLS
  4049. WOLFSSL_DTLS_CTX dtlsCtx; /* DTLS connection context */
  4050. #ifndef NO_WOLFSSL_SERVER
  4051. buffer dtlsCookieSecret; /* DTLS cookie secret */
  4052. #endif /* NO_WOLFSSL_SERVER */
  4053. #endif
  4054. #ifdef HAVE_PK_CALLBACKS
  4055. #ifdef HAVE_ECC
  4056. buffer peerEccDsaKey; /* we own for Ecc Verify Callbacks */
  4057. #endif /* HAVE_ECC */
  4058. #ifdef HAVE_ED25519
  4059. buffer peerEd25519Key; /* for Ed25519 Verify Callbacks */
  4060. #endif /* HAVE_ED25519 */
  4061. #ifdef HAVE_ED448
  4062. buffer peerEd448Key; /* for Ed448 Verify Callbacks */
  4063. #endif /* HAVE_ED448 */
  4064. #ifndef NO_RSA
  4065. buffer peerRsaKey; /* we own for Rsa Verify Callbacks */
  4066. #endif /* NO_RSA */
  4067. #endif /* HAVE_PK_CALLBACKS */
  4068. } Buffers;
  4069. /* sub-states for send/do key share (key exchange) */
  4070. enum asyncState {
  4071. TLS_ASYNC_BEGIN = 0,
  4072. TLS_ASYNC_BUILD,
  4073. TLS_ASYNC_DO,
  4074. TLS_ASYNC_VERIFY,
  4075. TLS_ASYNC_FINALIZE,
  4076. TLS_ASYNC_END
  4077. };
  4078. /* sub-states for build message */
  4079. enum buildMsgState {
  4080. BUILD_MSG_BEGIN = 0,
  4081. BUILD_MSG_SIZE,
  4082. BUILD_MSG_HASH,
  4083. BUILD_MSG_VERIFY_MAC,
  4084. BUILD_MSG_ENCRYPT,
  4085. BUILD_MSG_ENCRYPTED_VERIFY_MAC,
  4086. };
  4087. /* sub-states for cipher operations */
  4088. enum cipherState {
  4089. CIPHER_STATE_BEGIN = 0,
  4090. CIPHER_STATE_DO,
  4091. CIPHER_STATE_END,
  4092. };
  4093. struct Options {
  4094. #ifndef NO_PSK
  4095. wc_psk_client_callback client_psk_cb;
  4096. wc_psk_server_callback server_psk_cb;
  4097. #ifdef OPENSSL_EXTRA
  4098. wc_psk_use_session_cb_func session_psk_cb;
  4099. #endif
  4100. #ifdef WOLFSSL_TLS13
  4101. wc_psk_client_cs_callback client_psk_cs_cb; /* client callback */
  4102. wc_psk_client_tls13_callback client_psk_tls13_cb; /* client callback */
  4103. wc_psk_server_tls13_callback server_psk_tls13_cb; /* server callback */
  4104. #endif
  4105. void* psk_ctx;
  4106. #endif /* NO_PSK */
  4107. unsigned long mask; /* store SSL_OP_ flags */
  4108. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || defined(WOLFSSL_WPAS_SMALL)
  4109. word16 minProto:1; /* sets min to min available */
  4110. word16 maxProto:1; /* sets max to max available */
  4111. #endif
  4112. #if defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TLS13)
  4113. unsigned int maxTicketTls13; /* maximum number of tickets to send */
  4114. unsigned int ticketsSent; /* keep track of the total sent */
  4115. #endif
  4116. /* on/off or small bit flags, optimize layout */
  4117. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  4118. word16 havePSK:1; /* psk key set by user */
  4119. #endif /* HAVE_SESSION_TICKET || !NO_PSK */
  4120. word16 sendVerify:2; /* false = 0, true = 1, sendBlank = 2 */
  4121. word16 sessionCacheOff:1;
  4122. word16 sessionCacheFlushOff:1;
  4123. #ifdef HAVE_EXT_CACHE
  4124. word16 internalCacheOff:1;
  4125. word16 internalCacheLookupOff:1;
  4126. #endif
  4127. word16 side:2; /* client, server or neither end */
  4128. word16 verifyPeer:1;
  4129. word16 verifyNone:1;
  4130. word16 failNoCert:1;
  4131. word16 failNoCertxPSK:1; /* fail for no cert except with PSK */
  4132. word16 downgrade:1; /* allow downgrade of versions */
  4133. word16 resuming:1;
  4134. #ifdef HAVE_SECURE_RENEGOTIATION
  4135. word16 resumed:1; /* resuming may be reset on SCR */
  4136. #endif
  4137. word16 isPSK:1;
  4138. word16 haveSessionId:1; /* server may not send */
  4139. word16 tls:1; /* using TLS ? */
  4140. word16 tls1_1:1; /* using TLSv1.1+ ? */
  4141. word16 tls1_3:1; /* using TLSv1.3+ ? */
  4142. word16 seenUnifiedHdr:1; /* received msg with unified header */
  4143. word16 dtls:1; /* using datagrams ? */
  4144. word16 dtlsStateful:1; /* allow stateful processing ? */
  4145. word16 connReset:1; /* has the peer reset */
  4146. word16 isClosed:1; /* if we consider conn closed */
  4147. word16 closeNotify:1; /* we've received a close notify */
  4148. word16 sentNotify:1; /* we've sent a close notify */
  4149. word16 shutdownDone:1; /* we've completed a shutdown */
  4150. word16 usingCompression:1; /* are we using compression */
  4151. word16 haveRSA:1; /* RSA available */
  4152. word16 haveECC:1; /* ECC available */
  4153. word16 haveDH:1; /* server DH parms set by user */
  4154. word16 haveECDSAsig:1; /* server ECDSA signed cert */
  4155. word16 haveStaticECC:1; /* static server ECC private key */
  4156. word16 haveFalconSig:1; /* server Falcon signed cert */
  4157. word16 haveDilithiumSig:1; /* server Dilithium signed cert */
  4158. word16 havePeerCert:1; /* do we have peer's cert */
  4159. word16 havePeerVerify:1; /* and peer's cert verify */
  4160. word16 usingPSK_cipher:1; /* are using psk as cipher */
  4161. word16 usingAnon_cipher:1; /* are we using an anon cipher */
  4162. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  4163. word16 noPskDheKe:1; /* Don't use (EC)DHE with PSK */
  4164. #ifdef HAVE_SUPPORTED_CURVES
  4165. word16 onlyPskDheKe:1; /* Only use (EC)DHE with PSK */
  4166. #endif
  4167. #endif
  4168. word16 partialWrite:1; /* only one msg per write call */
  4169. word16 quietShutdown:1; /* don't send close notify */
  4170. word16 certOnly:1; /* stop once we get cert */
  4171. word16 groupMessages:1; /* group handshake messages */
  4172. word16 saveArrays:1; /* save array Memory for user get keys
  4173. or psk */
  4174. word16 weOwnRng:1; /* will be true unless CTX owns */
  4175. word16 dontFreeDigest:1; /* when true, we used SetDigest */
  4176. word16 haveEMS:1; /* using extended master secret */
  4177. #ifdef HAVE_POLY1305
  4178. word16 oldPoly:1; /* set when to use old rfc way of poly*/
  4179. #endif
  4180. word16 haveAnon:1; /* User wants to allow Anon suites */
  4181. #ifdef HAVE_SESSION_TICKET
  4182. word16 createTicket:1; /* Server to create new Ticket */
  4183. word16 useTicket:1; /* Use Ticket not session cache */
  4184. word16 rejectTicket:1; /* Callback rejected ticket */
  4185. word16 noTicketTls12:1; /* TLS 1.2 server won't send ticket */
  4186. #ifdef WOLFSSL_TLS13
  4187. word16 noTicketTls13:1; /* Server won't create new Ticket */
  4188. #endif
  4189. #endif
  4190. #ifdef WOLFSSL_DTLS
  4191. #ifdef HAVE_SECURE_RENEGOTIATION
  4192. word16 dtlsDoSCR:1; /* Enough packets were dropped. We
  4193. * need to re-key. */
  4194. #endif
  4195. word16 dtlsUseNonblock:1; /* are we using nonblocking socket */
  4196. word16 dtlsHsRetain:1; /* DTLS retaining HS data */
  4197. word16 haveMcast:1; /* using multicast ? */
  4198. #ifdef WOLFSSL_SCTP
  4199. word16 dtlsSctp:1; /* DTLS-over-SCTP mode */
  4200. #endif
  4201. #endif /* WOLFSSL_DTLS */
  4202. #if defined(HAVE_TLS_EXTENSIONS) && defined(HAVE_SUPPORTED_CURVES)
  4203. word16 userCurves:1; /* indicates user called wolfSSL_UseSupportedCurve */
  4204. #endif
  4205. word16 keepResources:1; /* Keep resources after handshake */
  4206. word16 useClientOrder:1; /* Use client's cipher order */
  4207. word16 mutualAuth:1; /* Mutual authentication is required */
  4208. word16 peerAuthGood:1; /* Any required peer auth done */
  4209. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  4210. word16 pskNegotiated:1; /* Session Ticket/PSK negotiated. */
  4211. #endif
  4212. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  4213. word16 postHandshakeAuth:1;/* Client send post_handshake_auth
  4214. * extension */
  4215. word16 verifyPostHandshake:1; /* Only send client cert req post
  4216. * handshake, not also during */
  4217. #endif
  4218. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  4219. word16 sendCookie:1; /* Server creates a Cookie in HRR */
  4220. #endif
  4221. #ifdef WOLFSSL_ALT_CERT_CHAINS
  4222. word16 usingAltCertChain:1;/* Alternate cert chain was used */
  4223. #endif
  4224. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  4225. word16 sentChangeCipher:1; /* Change Cipher Spec sent */
  4226. #endif
  4227. #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \
  4228. ((defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)) || \
  4229. (defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \
  4230. (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)))
  4231. word16 cacheMessages:1; /* Cache messages for sign/verify */
  4232. #endif
  4233. #ifndef NO_DH
  4234. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  4235. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  4236. word16 dhDoKeyTest:1; /* Need to do the DH Key prime test */
  4237. word16 dhKeyTested:1; /* Set when key has been tested. */
  4238. #endif
  4239. #endif
  4240. #ifdef HAVE_ENCRYPT_THEN_MAC
  4241. word16 disallowEncThenMac:1; /* Don't do Encrypt-Then-MAC */
  4242. word16 encThenMac:1; /* Doing Encrypt-Then-MAC */
  4243. word16 startedETMRead:1; /* Doing Encrypt-Then-MAC read */
  4244. word16 startedETMWrite:1; /* Doing Encrypt-Then-MAC write */
  4245. #endif
  4246. #ifdef WOLFSSL_ASYNC_CRYPT
  4247. word16 buildArgsSet:1; /* buildArgs are set and need to
  4248. * be free'd */
  4249. #endif
  4250. word16 buildingMsg:1; /* If set then we need to re-enter the
  4251. * handshake logic. */
  4252. #ifdef WOLFSSL_DTLS13
  4253. word16 dtls13SendMoreAcks:1; /* Send more acks during the
  4254. * handshake process */
  4255. #endif
  4256. #ifdef WOLFSSL_TLS13
  4257. word16 tls13MiddleBoxCompat:1; /* TLSv1.3 middlebox compatibility */
  4258. #endif
  4259. #ifdef WOLFSSL_DTLS_CID
  4260. word16 useDtlsCID:1;
  4261. #endif /* WOLFSSL_DTLS_CID */
  4262. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  4263. word16 useEch:1;
  4264. #endif
  4265. #ifdef WOLFSSL_SEND_HRR_COOKIE
  4266. word16 cookieGood:1;
  4267. #endif
  4268. #if defined(HAVE_DANE)
  4269. word16 useDANE:1;
  4270. #endif /* HAVE_DANE */
  4271. #if defined(HAVE_RPK)
  4272. RpkConfig rpkConfig;
  4273. RpkState rpkState;
  4274. #endif /* HAVE_RPK */
  4275. /* need full byte values for this section */
  4276. byte processReply; /* nonblocking resume */
  4277. byte cipherSuite0; /* first byte, normally 0 */
  4278. byte cipherSuite; /* second byte, actual suite */
  4279. byte hashAlgo; /* selected hash algorithm */
  4280. byte sigAlgo; /* selected sig algorithm */
  4281. byte serverState;
  4282. byte clientState;
  4283. byte handShakeState;
  4284. byte handShakeDone; /* at least one handshake complete */
  4285. byte minDowngrade; /* minimum downgrade version */
  4286. byte connectState; /* nonblocking resume */
  4287. byte acceptState; /* nonblocking resume */
  4288. byte asyncState; /* sub-state for enum asyncState */
  4289. byte buildMsgState; /* sub-state for enum buildMsgState */
  4290. byte alertCount; /* detect warning dos attempt */
  4291. #ifdef WOLFSSL_MULTICAST
  4292. word16 mcastID; /* Multicast group ID */
  4293. #endif
  4294. #ifndef NO_DH
  4295. word16 minDhKeySz; /* minimum DH key size */
  4296. word16 maxDhKeySz; /* minimum DH key size */
  4297. word16 dhKeySz; /* actual DH key size */
  4298. #endif
  4299. #ifndef NO_RSA
  4300. short minRsaKeySz; /* minimum RSA key size */
  4301. #endif
  4302. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  4303. short minEccKeySz; /* minimum ECC key size */
  4304. #endif
  4305. #if defined(HAVE_PQC)
  4306. short minFalconKeySz; /* minimum Falcon key size */
  4307. short minDilithiumKeySz;/* minimum Dilithium key size */
  4308. #endif
  4309. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  4310. byte verifyDepth; /* maximum verification depth */
  4311. #endif
  4312. #ifdef WOLFSSL_EARLY_DATA
  4313. word16 pskIdIndex;
  4314. word32 maxEarlyDataSz;
  4315. #endif
  4316. #ifdef WOLFSSL_TLS13
  4317. byte oldMinor; /* client preferred version < TLS 1.3 */
  4318. #endif
  4319. };
  4320. typedef struct Arrays {
  4321. byte* pendingMsg; /* defrag buffer */
  4322. byte* preMasterSecret;
  4323. word32 preMasterSz; /* differs for DH, actual size */
  4324. word32 pendingMsgSz; /* defrag buffer size */
  4325. word32 pendingMsgOffset; /* current offset into defrag buffer */
  4326. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  4327. word32 psk_keySz; /* actual size */
  4328. char client_identity[MAX_PSK_ID_LEN + NULL_TERM_LEN];
  4329. char server_hint[MAX_PSK_ID_LEN + NULL_TERM_LEN];
  4330. byte psk_key[MAX_PSK_KEY_LEN];
  4331. #endif
  4332. byte clientRandom[RAN_LEN];
  4333. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  4334. byte clientRandomInner[RAN_LEN];
  4335. #endif
  4336. byte serverRandom[RAN_LEN];
  4337. byte sessionID[ID_LEN];
  4338. byte sessionIDSz;
  4339. #ifdef WOLFSSL_TLS13
  4340. byte secret[SECRET_LEN];
  4341. #endif
  4342. #ifdef HAVE_KEYING_MATERIAL
  4343. byte exporterSecret[WC_MAX_DIGEST_SIZE];
  4344. #endif
  4345. byte masterSecret[SECRET_LEN];
  4346. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
  4347. !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
  4348. byte tsip_masterSecret[TSIP_TLS_MASTERSECRET_SIZE];
  4349. #endif
  4350. #if defined(WOLFSSL_RENESAS_FSPSM_TLS)
  4351. byte fspsm_masterSecret[FSPSM_TLS_MASTERSECRET_SIZE];
  4352. #endif
  4353. #ifdef WOLFSSL_DTLS
  4354. byte cookie[MAX_COOKIE_LEN];
  4355. byte cookieSz;
  4356. #endif
  4357. byte pendingMsgType; /* defrag buffer message type */
  4358. } Arrays;
  4359. #ifndef ASN_NAME_MAX
  4360. #ifndef NO_ASN
  4361. /* use value from asn.h */
  4362. #define ASN_NAME_MAX WC_ASN_NAME_MAX
  4363. #else
  4364. /* calculate for WOLFSSL_X509 */
  4365. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  4366. defined(WOLFSSL_CERT_EXT)
  4367. #define ASN_NAME_MAX 330
  4368. #else
  4369. #define ASN_NAME_MAX 256
  4370. #endif
  4371. #endif
  4372. #endif
  4373. #ifndef MAX_DATE_SZ
  4374. #define MAX_DATE_SZ 32
  4375. #endif
  4376. typedef enum {
  4377. STACK_TYPE_X509 = 0,
  4378. STACK_TYPE_GEN_NAME = 1,
  4379. STACK_TYPE_BIO = 2,
  4380. STACK_TYPE_OBJ = 3,
  4381. STACK_TYPE_STRING = 4,
  4382. STACK_TYPE_CIPHER = 5,
  4383. STACK_TYPE_ACCESS_DESCRIPTION = 6,
  4384. STACK_TYPE_X509_EXT = 7,
  4385. STACK_TYPE_NULL = 8,
  4386. STACK_TYPE_X509_NAME = 9,
  4387. STACK_TYPE_CONF_VALUE = 10,
  4388. STACK_TYPE_X509_INFO = 11,
  4389. STACK_TYPE_BY_DIR_entry = 12,
  4390. STACK_TYPE_BY_DIR_hash = 13,
  4391. STACK_TYPE_X509_OBJ = 14,
  4392. STACK_TYPE_DIST_POINT = 15,
  4393. STACK_TYPE_X509_CRL = 16,
  4394. STACK_TYPE_X509_NAME_ENTRY = 17,
  4395. STACK_TYPE_X509_REQ_ATTR = 18,
  4396. } WOLF_STACK_TYPE;
  4397. struct WOLFSSL_STACK {
  4398. unsigned long num; /* number of nodes in stack
  4399. * (safety measure for freeing and shortcut for count) */
  4400. #if defined(OPENSSL_ALL)
  4401. wolf_sk_hash_cb hash_fn;
  4402. unsigned long hash;
  4403. #endif
  4404. union {
  4405. WOLFSSL_X509* x509;
  4406. WOLFSSL_X509_NAME* name;
  4407. WOLFSSL_X509_NAME_ENTRY* name_entry;
  4408. WOLFSSL_X509_INFO* info;
  4409. WOLFSSL_BIO* bio;
  4410. WOLFSSL_ASN1_OBJECT* obj;
  4411. WOLFSSL_CIPHER cipher;
  4412. WOLFSSL_ACCESS_DESCRIPTION* access;
  4413. WOLFSSL_X509_EXTENSION* ext;
  4414. #ifdef OPENSSL_EXTRA
  4415. WOLFSSL_CONF_VALUE* conf;
  4416. #endif
  4417. void* generic;
  4418. char* string;
  4419. WOLFSSL_GENERAL_NAME* gn;
  4420. WOLFSSL_BY_DIR_entry* dir_entry;
  4421. WOLFSSL_BY_DIR_HASH* dir_hash;
  4422. WOLFSSL_X509_OBJECT* x509_obj;
  4423. WOLFSSL_DIST_POINT* dp;
  4424. WOLFSSL_X509_CRL* crl;
  4425. } data;
  4426. void* heap; /* memory heap hint */
  4427. WOLFSSL_STACK* next;
  4428. WOLF_STACK_TYPE type; /* Identifies type of stack. */
  4429. };
  4430. struct WOLFSSL_X509_NAME {
  4431. char *name;
  4432. int dynamicName;
  4433. int sz;
  4434. char staticName[ASN_NAME_MAX];
  4435. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  4436. !defined(NO_ASN)
  4437. DecodedName fullName;
  4438. int entrySz; /* number of entries */
  4439. WOLFSSL_X509_NAME_ENTRY entry[MAX_NAME_ENTRIES]; /* all entries i.e. CN */
  4440. WOLFSSL_X509* x509; /* x509 that struct belongs to */
  4441. #endif /* OPENSSL_EXTRA */
  4442. #ifndef WOLFSSL_NO_CA_NAMES
  4443. byte raw[ASN_NAME_MAX];
  4444. int rawLen;
  4445. WOLF_STACK_OF(WOLFSSL_X509_NAME_ENTRY)* entries;
  4446. #endif
  4447. void* heap;
  4448. };
  4449. #ifndef EXTERNAL_SERIAL_SIZE
  4450. #define EXTERNAL_SERIAL_SIZE 32
  4451. #endif
  4452. #ifdef NO_ASN
  4453. typedef struct DNS_entry DNS_entry;
  4454. #endif
  4455. struct WOLFSSL_X509 {
  4456. int version;
  4457. int serialSz;
  4458. #ifdef WOLFSSL_SEP
  4459. int deviceTypeSz;
  4460. int hwTypeSz;
  4461. byte deviceType[EXTERNAL_SERIAL_SIZE];
  4462. byte hwType[EXTERNAL_SERIAL_SIZE];
  4463. int hwSerialNumSz;
  4464. byte hwSerialNum[EXTERNAL_SERIAL_SIZE];
  4465. #endif /* WOLFSSL_SEP */
  4466. #if (defined(WOLFSSL_SEP) || defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  4467. defined (OPENSSL_EXTRA)) && \
  4468. (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
  4469. byte certPolicySet;
  4470. byte certPolicyCrit;
  4471. #endif /* (WOLFSSL_SEP || WOLFSSL_QT) && (OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL) */
  4472. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  4473. WOLFSSL_STACK* ext_sk; /* Store X509_EXTENSIONS from wolfSSL_X509_get_ext */
  4474. WOLFSSL_STACK* ext_sk_full; /* Store X509_EXTENSIONS from wolfSSL_X509_get0_extensions */
  4475. WOLFSSL_STACK* ext_d2i;/* Store d2i extensions from wolfSSL_X509_get_ext_d2i */
  4476. #endif /* WOLFSSL_QT || OPENSSL_ALL */
  4477. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  4478. WOLFSSL_ASN1_INTEGER* serialNumber; /* Stores SN from wolfSSL_X509_get_serialNumber */
  4479. #endif
  4480. WOLFSSL_ASN1_TIME notBefore;
  4481. WOLFSSL_ASN1_TIME notAfter;
  4482. buffer sig;
  4483. int sigOID;
  4484. DNS_entry* altNames; /* alt names list */
  4485. buffer pubKey;
  4486. int pubKeyOID;
  4487. DNS_entry* altNamesNext; /* hint for retrieval */
  4488. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448) || \
  4489. defined(HAVE_PQC)
  4490. word32 pkCurveOID;
  4491. #endif /* HAVE_ECC || HAVE_PQC */
  4492. #ifndef NO_CERTS
  4493. DerBuffer* derCert; /* may need */
  4494. #endif
  4495. void* heap; /* heap hint */
  4496. byte dynamicMemory; /* dynamic memory flag */
  4497. byte isCa:1;
  4498. #ifdef WOLFSSL_CERT_EXT
  4499. char certPolicies[MAX_CERTPOL_NB][MAX_CERTPOL_SZ];
  4500. int certPoliciesNb;
  4501. #endif /* WOLFSSL_CERT_EXT */
  4502. #if defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_EXTRA)
  4503. wolfSSL_Ref ref;
  4504. #endif
  4505. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  4506. #ifdef HAVE_EX_DATA
  4507. WOLFSSL_CRYPTO_EX_DATA ex_data;
  4508. #endif
  4509. byte* authKeyId; /* Points into authKeyIdSrc */
  4510. byte* authKeyIdSrc;
  4511. byte* subjKeyId;
  4512. byte* extKeyUsageSrc;
  4513. #ifdef OPENSSL_ALL
  4514. byte* subjAltNameSrc;
  4515. #endif
  4516. byte* rawCRLInfo;
  4517. byte* CRLInfo;
  4518. byte* authInfo;
  4519. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || defined(WOLFSSL_QT)
  4520. byte* authInfoCaIssuer;
  4521. int authInfoCaIssuerSz;
  4522. #endif
  4523. word32 pathLength;
  4524. word16 keyUsage;
  4525. int rawCRLInfoSz;
  4526. int CRLInfoSz;
  4527. int authInfoSz;
  4528. word32 authKeyIdSz;
  4529. word32 authKeyIdSrcSz;
  4530. word32 subjKeyIdSz;
  4531. byte extKeyUsage;
  4532. word32 extKeyUsageSz;
  4533. word32 extKeyUsageCount;
  4534. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  4535. byte nsCertType;
  4536. #endif
  4537. #ifdef OPENSSL_ALL
  4538. word32 subjAltNameSz;
  4539. #endif
  4540. byte CRLdistSet:1;
  4541. byte CRLdistCrit:1;
  4542. byte authInfoSet:1;
  4543. byte authInfoCrit:1;
  4544. byte keyUsageSet:1;
  4545. byte keyUsageCrit:1;
  4546. byte extKeyUsageCrit:1;
  4547. byte subjKeyIdSet:1;
  4548. byte subjKeyIdCrit:1;
  4549. byte basicConstSet:1;
  4550. byte basicConstCrit:1;
  4551. byte basicConstPlSet:1;
  4552. byte subjAltNameSet:1;
  4553. byte subjAltNameCrit:1;
  4554. byte authKeyIdSet:1;
  4555. byte authKeyIdCrit:1;
  4556. byte issuerSet:1;
  4557. #ifdef WOLFSSL_CUSTOM_OID
  4558. CertExtension custom_exts[NUM_CUSTOM_EXT];
  4559. int customExtCount;
  4560. #endif /* WOLFSSL_CUSTOM_OID */
  4561. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  4562. #ifdef WOLFSSL_CERT_REQ
  4563. byte isCSR:1;
  4564. #endif
  4565. byte serial[EXTERNAL_SERIAL_SIZE];
  4566. char subjectCN[ASN_NAME_MAX]; /* common name short cut */
  4567. #if defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_GEN)
  4568. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  4569. /* stack of CSR attributes */
  4570. WOLF_STACK_OF(WOLFSSL_X509_ATRIBUTE)* reqAttributes;
  4571. #endif
  4572. #if defined(WOLFSSL_CERT_REQ)
  4573. char challengePw[CTC_NAME_SIZE]; /* for REQ certs */
  4574. char contentType[CTC_NAME_SIZE];
  4575. #endif
  4576. #endif /* WOLFSSL_CERT_REQ || WOLFSSL_CERT_GEN */
  4577. WOLFSSL_X509_NAME issuer;
  4578. WOLFSSL_X509_NAME subject;
  4579. #if defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  4580. WOLFSSL_X509_ALGOR algor;
  4581. WOLFSSL_X509_PUBKEY key;
  4582. #endif
  4583. #if defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_EXTRA) || \
  4584. defined(OPENSSL_ALL) || defined(KEEP_OUR_CERT) || \
  4585. defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)
  4586. byte notBeforeData[CTC_DATE_SIZE];
  4587. byte notAfterData[CTC_DATE_SIZE];
  4588. #endif
  4589. };
  4590. /* record layer header for PlainText, Compressed, and CipherText */
  4591. typedef struct RecordLayerHeader {
  4592. byte type;
  4593. byte pvMajor;
  4594. byte pvMinor;
  4595. byte length[2];
  4596. } RecordLayerHeader;
  4597. /* record layer header for DTLS PlainText, Compressed, and CipherText */
  4598. typedef struct DtlsRecordLayerHeader {
  4599. byte type;
  4600. byte pvMajor;
  4601. byte pvMinor;
  4602. byte sequence_number[8]; /* per record */
  4603. byte length[2];
  4604. } DtlsRecordLayerHeader;
  4605. typedef struct DtlsFragBucket {
  4606. /* m stands for meta */
  4607. union {
  4608. struct {
  4609. struct DtlsFragBucket* next;
  4610. word32 offset;
  4611. word32 sz;
  4612. } m;
  4613. /* Make sure we have at least DTLS_HANDSHAKE_HEADER_SZ bytes before the
  4614. * buf so that we can reconstruct the header in the allocated
  4615. * DtlsFragBucket buffer. */
  4616. byte padding[DTLS_HANDSHAKE_HEADER_SZ];
  4617. } m;
  4618. /* Ignore "nonstandard extension used : zero-sized array in struct/union"
  4619. * MSVC warning */
  4620. #ifdef _MSC_VER
  4621. #pragma warning(disable: 4200)
  4622. #endif
  4623. byte buf[];
  4624. } DtlsFragBucket;
  4625. typedef struct DtlsMsg {
  4626. struct DtlsMsg* next;
  4627. byte* raw;
  4628. byte* fullMsg; /* for TX fullMsg == raw. For RX this points to
  4629. * the start of the message after headers. */
  4630. DtlsFragBucket* fragBucketList;
  4631. word32 bytesReceived;
  4632. word16 epoch; /* Epoch that this message belongs to */
  4633. word32 seq; /* Handshake sequence number */
  4634. word32 sz; /* Length of whole message */
  4635. byte type;
  4636. byte fragBucketListCount;
  4637. byte ready:1;
  4638. } DtlsMsg;
  4639. #ifdef HAVE_NETX
  4640. /* NETX I/O Callback default */
  4641. typedef struct NetX_Ctx {
  4642. NX_TCP_SOCKET* nxSocket; /* send/recv socket handle */
  4643. NX_PACKET* nxPacket; /* incoming packet handle for short reads */
  4644. ULONG nxOffset; /* offset already read from nxPacket */
  4645. ULONG nxWait; /* wait option flag */
  4646. } NetX_Ctx;
  4647. #endif
  4648. /* Handshake messages received from peer (plus change cipher */
  4649. typedef struct MsgsReceived {
  4650. word16 got_hello_request:1;
  4651. word16 got_client_hello:2;
  4652. word16 got_server_hello:1;
  4653. word16 got_hello_verify_request:1;
  4654. word16 got_session_ticket:1;
  4655. word16 got_end_of_early_data:1;
  4656. word16 got_hello_retry_request:1;
  4657. word16 got_encrypted_extensions:1;
  4658. word16 got_certificate:1;
  4659. word16 got_certificate_status:1;
  4660. word16 got_server_key_exchange:1;
  4661. word16 got_certificate_request:1;
  4662. word16 got_server_hello_done:1;
  4663. word16 got_certificate_verify:1;
  4664. word16 got_client_key_exchange:1;
  4665. word16 got_finished:1;
  4666. word16 got_key_update:1;
  4667. word16 got_change_cipher:1;
  4668. } MsgsReceived;
  4669. /* Handshake hashes */
  4670. typedef struct HS_Hashes {
  4671. Hashes verifyHashes;
  4672. Hashes certHashes; /* for cert verify */
  4673. #ifndef NO_SHA
  4674. wc_Sha hashSha; /* sha hash of handshake msgs */
  4675. #endif
  4676. #if !defined(NO_MD5) && !defined(NO_OLD_TLS)
  4677. wc_Md5 hashMd5; /* md5 hash of handshake msgs */
  4678. #endif
  4679. #ifndef NO_SHA256
  4680. wc_Sha256 hashSha256; /* sha256 hash of handshake msgs */
  4681. #endif
  4682. #ifdef WOLFSSL_SHA384
  4683. wc_Sha384 hashSha384; /* sha384 hash of handshake msgs */
  4684. #endif
  4685. #ifdef WOLFSSL_SHA512
  4686. wc_Sha512 hashSha512; /* sha512 hash of handshake msgs */
  4687. #endif
  4688. #ifdef WOLFSSL_SM3
  4689. wc_Sm3 hashSm3; /* sm3 hash of handshake msgs */
  4690. #endif
  4691. #if (defined(HAVE_ED25519) || defined(HAVE_ED448) || \
  4692. (defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3))) && \
  4693. !defined(WOLFSSL_NO_CLIENT_AUTH)
  4694. byte* messages; /* handshake messages */
  4695. int length; /* length of handshake messages' data */
  4696. int prevLen; /* length of messages but last */
  4697. #endif
  4698. } HS_Hashes;
  4699. #ifndef WOLFSSL_NO_TLS12
  4700. /* Persistable BuildMessage arguments */
  4701. typedef struct BuildMsgArgs {
  4702. word32 digestSz;
  4703. word32 sz;
  4704. word32 pad;
  4705. word32 idx;
  4706. word32 headerSz;
  4707. word16 size;
  4708. word32 ivSz; /* TLSv1.1 IV */
  4709. byte* iv;
  4710. ALIGN16 byte staticIvBuffer[MAX_IV_SZ];
  4711. } BuildMsgArgs;
  4712. #endif
  4713. #ifdef WOLFSSL_ASYNC_IO
  4714. #define MAX_ASYNC_ARGS 18
  4715. typedef void (*FreeArgsCb)(struct WOLFSSL* ssl, void* pArgs);
  4716. struct WOLFSSL_ASYNC {
  4717. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WOLFSSL_NO_TLS12)
  4718. BuildMsgArgs buildArgs; /* holder for current BuildMessage args */
  4719. #endif
  4720. FreeArgsCb freeArgs; /* function pointer to cleanup args */
  4721. word32 args[MAX_ASYNC_ARGS]; /* holder for current args */
  4722. };
  4723. #endif
  4724. #ifdef HAVE_WRITE_DUP
  4725. #define WRITE_DUP_SIDE 1
  4726. #define READ_DUP_SIDE 2
  4727. typedef struct WriteDup {
  4728. wolfSSL_Mutex dupMutex; /* reference count mutex */
  4729. int dupCount; /* reference count */
  4730. int dupErr; /* under dupMutex, pass to other side */
  4731. } WriteDup;
  4732. WOLFSSL_LOCAL void FreeWriteDup(WOLFSSL* ssl);
  4733. WOLFSSL_LOCAL int NotifyWriteSide(WOLFSSL* ssl, int err);
  4734. #endif /* HAVE_WRITE_DUP */
  4735. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  4736. typedef struct CertReqCtx CertReqCtx;
  4737. struct CertReqCtx {
  4738. CertReqCtx* next;
  4739. byte len;
  4740. byte ctx;
  4741. };
  4742. #endif
  4743. #ifdef WOLFSSL_EARLY_DATA
  4744. typedef enum EarlyDataState {
  4745. no_early_data,
  4746. early_data_ext,
  4747. expecting_early_data,
  4748. process_early_data,
  4749. done_early_data
  4750. } EarlyDataState;
  4751. #endif
  4752. #ifdef WOLFSSL_DTLS13
  4753. /* size of the mask used to encrypt/decrypt Record Number */
  4754. #define DTLS13_RN_MASK_SIZE 16
  4755. typedef struct Dtls13UnifiedHdrInfo {
  4756. word16 recordLength;
  4757. byte seqLo;
  4758. byte seqHi;
  4759. byte seqHiPresent:1;
  4760. byte epochBits;
  4761. } Dtls13UnifiedHdrInfo;
  4762. enum {
  4763. DTLS13_EPOCH_EARLYDATA = 1,
  4764. DTLS13_EPOCH_HANDSHAKE = 2,
  4765. DTLS13_EPOCH_TRAFFIC0 = 3
  4766. };
  4767. typedef struct Dtls13Epoch {
  4768. w64wrapper epochNumber;
  4769. w64wrapper nextSeqNumber;
  4770. w64wrapper nextPeerSeqNumber;
  4771. #ifndef WOLFSSL_TLS13_IGNORE_AEAD_LIMITS
  4772. w64wrapper dropCount; /* Amount of records that failed decryption */
  4773. #endif
  4774. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  4775. /* key material for the epoch */
  4776. byte client_write_key[MAX_SYM_KEY_SIZE];
  4777. byte server_write_key[MAX_SYM_KEY_SIZE];
  4778. byte client_write_IV[MAX_WRITE_IV_SZ];
  4779. byte server_write_IV[MAX_WRITE_IV_SZ];
  4780. byte aead_exp_IV[AEAD_MAX_EXP_SZ];
  4781. byte aead_enc_imp_IV[AEAD_MAX_IMP_SZ];
  4782. byte aead_dec_imp_IV[AEAD_MAX_IMP_SZ];
  4783. byte client_sn_key[MAX_SYM_KEY_SIZE];
  4784. byte server_sn_key[MAX_SYM_KEY_SIZE];
  4785. byte isValid;
  4786. byte side;
  4787. } Dtls13Epoch;
  4788. #ifndef DTLS13_EPOCH_SIZE
  4789. #define DTLS13_EPOCH_SIZE 4
  4790. #endif
  4791. #ifndef DTLS13_RETRANS_RN_SIZE
  4792. #define DTLS13_RETRANS_RN_SIZE 3
  4793. #endif
  4794. enum Dtls13RtxFsmState {
  4795. DTLS13_RTX_FSM_PREPARING = 0,
  4796. DTLS13_RTX_FSM_SENDING,
  4797. DTLS13_RTX_FSM_WAITING,
  4798. DTLS13_RTX_FSM_FINISHED
  4799. };
  4800. typedef struct Dtls13RtxRecord {
  4801. struct Dtls13RtxRecord *next;
  4802. word16 length;
  4803. byte *data;
  4804. w64wrapper epoch;
  4805. w64wrapper seq[DTLS13_RETRANS_RN_SIZE];
  4806. byte rnIdx;
  4807. byte handshakeType;
  4808. } Dtls13RtxRecord;
  4809. typedef struct Dtls13RecordNumber {
  4810. struct Dtls13RecordNumber *next;
  4811. w64wrapper epoch;
  4812. w64wrapper seq;
  4813. } Dtls13RecordNumber;
  4814. typedef struct Dtls13Rtx {
  4815. enum Dtls13RtxFsmState state;
  4816. Dtls13RtxRecord *rtxRecords;
  4817. Dtls13RtxRecord **rtxRecordTailPtr;
  4818. Dtls13RecordNumber *seenRecords;
  4819. word32 lastRtx;
  4820. byte triggeredRtxs;
  4821. byte sendAcks:1;
  4822. byte retransmit:1;
  4823. } Dtls13Rtx;
  4824. #endif /* WOLFSSL_DTLS13 */
  4825. #ifdef WOLFSSL_DTLS_CID
  4826. typedef struct CIDInfo CIDInfo;
  4827. #endif /* WOLFSSL_DTLS_CID */
  4828. /* The idea is to reuse the context suites object whenever possible to save
  4829. * space. */
  4830. #define WOLFSSL_SUITES(ssl) \
  4831. ((const Suites*) ((ssl)->suites != NULL ? \
  4832. (ssl)->suites : \
  4833. (ssl)->ctx->suites))
  4834. /* wolfSSL ssl type */
  4835. struct WOLFSSL {
  4836. WOLFSSL_CTX* ctx;
  4837. #if defined(WOLFSSL_HAPROXY)
  4838. WOLFSSL_CTX* initial_ctx; /* preserve session key materials */
  4839. #endif
  4840. Suites* suites; /* Only need during handshake. Can be NULL when
  4841. * re-using the context's object. When WOLFSSL
  4842. * object needs separate instance of suites use
  4843. * AllocateSuites(). */
  4844. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
  4845. WOLF_STACK_OF(WOLFSSL_CIPHER)* suitesStack; /* stack of available cipher
  4846. * suites */
  4847. #endif
  4848. Arrays* arrays;
  4849. #ifdef WOLFSSL_TLS13
  4850. byte clientSecret[SECRET_LEN];
  4851. byte serverSecret[SECRET_LEN];
  4852. #endif
  4853. HS_Hashes* hsHashes;
  4854. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  4855. HS_Hashes* hsHashesEch;
  4856. #endif
  4857. void* IOCB_ReadCtx;
  4858. void* IOCB_WriteCtx;
  4859. WC_RNG* rng;
  4860. void* verifyCbCtx; /* cert verify callback user ctx*/
  4861. VerifyCallback verifyCallback; /* cert verification callback */
  4862. void* heap; /* for user overrides */
  4863. #ifdef HAVE_WRITE_DUP
  4864. WriteDup* dupWrite; /* valid pointer indicates ON */
  4865. /* side that decrements dupCount to zero frees overall structure */
  4866. byte dupSide; /* write side or read side */
  4867. #endif
  4868. #ifdef OPENSSL_EXTRA
  4869. byte cbioFlag; /* WOLFSSL_CBIO_RECV/SEND:
  4870. * CBIORecv/Send is set */
  4871. #endif
  4872. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  4873. NetworkFilterCallback_t AcceptFilter;
  4874. void *AcceptFilter_arg;
  4875. NetworkFilterCallback_t ConnectFilter;
  4876. void *ConnectFilter_arg;
  4877. #endif /* WOLFSSL_WOLFSENTRY_HOOKS */
  4878. CallbackIORecv CBIORecv;
  4879. CallbackIOSend CBIOSend;
  4880. #ifdef WOLFSSL_STATIC_MEMORY
  4881. WOLFSSL_HEAP_HINT heap_hint;
  4882. #endif
  4883. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  4884. ClientHelloGoodCb chGoodCb; /* notify user we parsed a verified
  4885. * ClientHello */
  4886. void* chGoodCtx; /* user ClientHello cb context */
  4887. #endif
  4888. #ifndef NO_HANDSHAKE_DONE_CB
  4889. HandShakeDoneCb hsDoneCb; /* notify user handshake done */
  4890. void* hsDoneCtx; /* user handshake cb context */
  4891. #endif
  4892. #ifdef WOLFSSL_ASYNC_IO
  4893. #ifdef WOLFSSL_ASYNC_CRYPT
  4894. WC_ASYNC_DEV* asyncDev;
  4895. #endif
  4896. /* Message building context should be stored here for functions that expect
  4897. * to encounter encryption blocking or fragment the message. */
  4898. struct WOLFSSL_ASYNC* async;
  4899. #endif
  4900. void* hsKey; /* Handshake key (RsaKey or ecc_key)
  4901. * allocated from heap */
  4902. word32 hsType; /* Type of Handshake key (hsKey) */
  4903. WOLFSSL_CIPHER cipher;
  4904. #ifndef WOLFSSL_AEAD_ONLY
  4905. hmacfp hmac;
  4906. #endif
  4907. Ciphers encrypt;
  4908. Ciphers decrypt;
  4909. Buffers buffers;
  4910. WOLFSSL_SESSION* session;
  4911. #ifndef NO_CLIENT_CACHE
  4912. ClientSession* clientSession;
  4913. #endif
  4914. WOLFSSL_ALERT_HISTORY alert_history;
  4915. WOLFSSL_ALERT pendingAlert;
  4916. int error;
  4917. int rfd; /* read file descriptor */
  4918. int wfd; /* write file descriptor */
  4919. int rflags; /* user read flags */
  4920. int wflags; /* user write flags */
  4921. word32 timeout; /* session timeout */
  4922. word32 fragOffset; /* fragment offset */
  4923. word16 curSize;
  4924. byte verifyDepth;
  4925. RecordLayerHeader curRL;
  4926. MsgsReceived msgsReceived; /* peer messages received */
  4927. ProtocolVersion version; /* negotiated version */
  4928. ProtocolVersion chVersion; /* client hello version */
  4929. CipherSpecs specs;
  4930. Keys keys;
  4931. Options options;
  4932. #ifdef OPENSSL_EXTRA
  4933. CallbackInfoState* CBIS; /* used to get info about SSL state */
  4934. int cbmode; /* read or write on info callback */
  4935. int cbtype; /* event type in info callback */
  4936. WOLFSSL_BIO* biord; /* socket bio read to free/close */
  4937. WOLFSSL_BIO* biowr; /* socket bio write to free/close */
  4938. byte sessionCtx[ID_LEN]; /* app session context ID */
  4939. WOLFSSL_X509_VERIFY_PARAM* param; /* verification parameters*/
  4940. #endif
  4941. #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
  4942. word32 disabledCurves; /* curves disabled by user */
  4943. #endif
  4944. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  4945. unsigned long peerVerifyRet;
  4946. #endif
  4947. #ifdef OPENSSL_EXTRA
  4948. byte readAhead;
  4949. byte sessionCtxSz; /* size of sessionCtx stored */
  4950. #ifdef HAVE_PK_CALLBACKS
  4951. void* loggingCtx; /* logging callback argument */
  4952. #endif
  4953. #endif /* OPENSSL_EXTRA */
  4954. #ifndef NO_RSA
  4955. RsaKey* peerRsaKey;
  4956. #if defined(WOLFSSL_RENESAS_TSIP_TLS) || defined(WOLFSSL_RENESAS_FSPSM_TLS)
  4957. void* RenesasUserCtx;
  4958. byte* peerSceTsipEncRsaKeyIndex;
  4959. #endif
  4960. byte peerRsaKeyPresent;
  4961. #endif
  4962. #if defined(WOLFSSL_TLS13) || defined(HAVE_FFDHE)
  4963. word16 namedGroup;
  4964. #endif
  4965. #ifdef WOLFSSL_TLS13
  4966. word16 group[WOLFSSL_MAX_GROUP_COUNT];
  4967. byte numGroups;
  4968. #endif
  4969. word16 pssAlgo;
  4970. #ifdef WOLFSSL_TLS13
  4971. word16 certHashSigAlgoSz; /* SigAlgoCert ext length in bytes */
  4972. byte certHashSigAlgo[WOLFSSL_MAX_SIGALGO]; /* cert sig/algo to
  4973. * offer */
  4974. #endif
  4975. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  4976. int eccVerifyRes;
  4977. #endif
  4978. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_CURVE25519) || \
  4979. defined(HAVE_ED448) || defined(HAVE_CURVE448)
  4980. word32 ecdhCurveOID; /* curve Ecc_Sum */
  4981. ecc_key* eccTempKey; /* private ECDHE key */
  4982. byte eccTempKeyPresent; /* also holds type */
  4983. byte peerEccKeyPresent;
  4984. #endif
  4985. #ifdef HAVE_ECC
  4986. ecc_key* peerEccKey; /* peer's ECDHE key */
  4987. ecc_key* peerEccDsaKey; /* peer's ECDSA key */
  4988. word16 eccTempKeySz; /* in octets 20 - 66 */
  4989. byte peerEccDsaKeyPresent;
  4990. #endif
  4991. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || \
  4992. defined(HAVE_CURVE448) || defined(HAVE_ED448)
  4993. word32 pkCurveOID; /* curve Ecc_Sum */
  4994. #endif
  4995. #ifdef HAVE_ED25519
  4996. ed25519_key* peerEd25519Key;
  4997. byte peerEd25519KeyPresent;
  4998. #endif
  4999. #ifdef HAVE_CURVE25519
  5000. curve25519_key* peerX25519Key;
  5001. byte peerX25519KeyPresent;
  5002. #endif
  5003. #ifdef HAVE_ED448
  5004. ed448_key* peerEd448Key;
  5005. byte peerEd448KeyPresent;
  5006. #endif
  5007. #ifdef HAVE_CURVE448
  5008. curve448_key* peerX448Key;
  5009. byte peerX448KeyPresent;
  5010. #endif
  5011. #ifdef HAVE_PQC
  5012. falcon_key* peerFalconKey;
  5013. byte peerFalconKeyPresent;
  5014. dilithium_key* peerDilithiumKey;
  5015. byte peerDilithiumKeyPresent;
  5016. #endif
  5017. #ifdef HAVE_LIBZ
  5018. z_stream c_stream; /* compression stream */
  5019. z_stream d_stream; /* decompression stream */
  5020. byte didStreamInit; /* for stream init and end */
  5021. #endif
  5022. #ifdef WOLFSSL_DTLS
  5023. int dtls_timeout_init; /* starting timeout value */
  5024. int dtls_timeout_max; /* maximum timeout value */
  5025. int dtls_timeout; /* current timeout value, changes */
  5026. #ifndef NO_ASN_TIME
  5027. word32 dtls_start_timeout;
  5028. #endif /* !NO_ASN_TIME */
  5029. word32 dtls_tx_msg_list_sz;
  5030. word32 dtls_rx_msg_list_sz;
  5031. DtlsMsg* dtls_tx_msg_list;
  5032. DtlsMsg* dtls_tx_msg;
  5033. DtlsMsg* dtls_rx_msg_list;
  5034. void* IOCB_CookieCtx; /* gen cookie ctx */
  5035. word32 dtls_expected_rx;
  5036. #ifdef WOLFSSL_SESSION_EXPORT
  5037. wc_dtls_export dtls_export; /* export function for session */
  5038. #endif
  5039. #if defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)
  5040. word16 dtlsMtuSz;
  5041. #endif /* WOLFSSL_SCTP || WOLFSSL_DTLS_MTU */
  5042. #ifdef WOLFSSL_MULTICAST
  5043. void* mcastHwCbCtx; /* Multicast highwater callback ctx */
  5044. #endif /* WOLFSSL_MULTICAST */
  5045. #ifdef WOLFSSL_DTLS_DROP_STATS
  5046. word32 macDropCount;
  5047. word32 replayDropCount;
  5048. #endif /* WOLFSSL_DTLS_DROP_STATS */
  5049. #ifdef WOLFSSL_SRTP
  5050. word16 dtlsSrtpProfiles; /* DTLS-with-SRTP profiles list
  5051. * (selected profiles - up to 16) */
  5052. word16 dtlsSrtpId; /* DTLS-with-SRTP profile ID selected */
  5053. #endif
  5054. #ifdef WOLFSSL_DTLS13
  5055. RecordNumberCiphers dtlsRecordNumberEncrypt;
  5056. RecordNumberCiphers dtlsRecordNumberDecrypt;
  5057. Dtls13Epoch dtls13Epochs[DTLS13_EPOCH_SIZE];
  5058. Dtls13Epoch *dtls13EncryptEpoch;
  5059. Dtls13Epoch *dtls13DecryptEpoch;
  5060. w64wrapper dtls13Epoch;
  5061. w64wrapper dtls13PeerEpoch;
  5062. w64wrapper dtls13InvalidateBefore;
  5063. byte dtls13CurRL[DTLS_RECVD_RL_HEADER_MAX_SZ];
  5064. word16 dtls13CurRlLength;
  5065. /* used to store the message if it needs to be fragmented */
  5066. buffer dtls13FragmentsBuffer;
  5067. byte dtls13SendingFragments:1;
  5068. byte dtls13SendingAckOrRtx:1;
  5069. byte dtls13FastTimeout:1;
  5070. byte dtls13WaitKeyUpdateAck:1;
  5071. byte dtls13DoKeyUpdate:1;
  5072. word32 dtls13MessageLength;
  5073. word32 dtls13FragOffset;
  5074. byte dtls13FragHandshakeType;
  5075. Dtls13Rtx dtls13Rtx;
  5076. byte *dtls13ClientHello;
  5077. word16 dtls13ClientHelloSz;
  5078. #endif /* WOLFSSL_DTLS13 */
  5079. #ifdef WOLFSSL_DTLS_CID
  5080. CIDInfo *dtlsCidInfo;
  5081. #endif /* WOLFSSL_DTLS_CID */
  5082. #endif /* WOLFSSL_DTLS */
  5083. #ifdef WOLFSSL_CALLBACKS
  5084. TimeoutInfo timeoutInfo; /* info saved during handshake */
  5085. HandShakeInfo handShakeInfo; /* info saved during handshake */
  5086. #endif
  5087. #ifdef OPENSSL_EXTRA
  5088. SSL_Msg_Cb protoMsgCb; /* inspect protocol message callback */
  5089. void* protoMsgCtx; /* user set context with msg callback */
  5090. #endif
  5091. #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
  5092. byte hsInfoOn; /* track handshake info */
  5093. byte toInfoOn; /* track timeout info */
  5094. #endif
  5095. #ifdef HAVE_FUZZER
  5096. CallbackFuzzer fuzzerCb; /* for testing with using fuzzer */
  5097. void* fuzzerCtx; /* user defined pointer */
  5098. #endif
  5099. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  5100. CertReqCtx* certReqCtx;
  5101. #endif
  5102. #ifdef WOLFSSL_LOCAL_X509_STORE
  5103. WOLFSSL_X509_STORE* x509_store_pt; /* take ownership of external store */
  5104. #endif
  5105. #ifdef KEEP_PEER_CERT
  5106. /* TODO put this on the heap so we can properly use the
  5107. * reference counter and not have to duplicate it. */
  5108. WOLFSSL_X509 peerCert; /* X509 peer cert */
  5109. #endif
  5110. #ifdef KEEP_OUR_CERT
  5111. WOLFSSL_X509* ourCert; /* keep alive a X509 struct of cert.
  5112. points to ctx if not owned (owned
  5113. flag found in buffers.weOwnCert) */
  5114. #endif
  5115. byte keepCert; /* keep certificate after handshake */
  5116. #ifdef HAVE_EX_DATA
  5117. WOLFSSL_CRYPTO_EX_DATA ex_data; /* external data, for Fortress */
  5118. #endif
  5119. int devId; /* async device id to use */
  5120. #ifdef HAVE_ONE_TIME_AUTH
  5121. OneTimeAuth auth;
  5122. #endif
  5123. #ifdef HAVE_TLS_EXTENSIONS
  5124. TLSX* extensions; /* RFC 6066 TLS Extensions data */
  5125. #ifdef HAVE_MAX_FRAGMENT
  5126. word16 max_fragment;
  5127. #endif
  5128. #ifdef HAVE_TRUNCATED_HMAC
  5129. byte truncated_hmac;
  5130. #endif
  5131. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  5132. byte status_request;
  5133. #endif
  5134. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  5135. byte status_request_v2;
  5136. #endif
  5137. #if defined(HAVE_SECURE_RENEGOTIATION) \
  5138. || defined(HAVE_SERVER_RENEGOTIATION_INFO)
  5139. int secure_rene_count; /* how many times */
  5140. SecureRenegotiation* secure_renegotiation; /* valid pointer indicates */
  5141. #endif /* user turned on */
  5142. #ifdef HAVE_ALPN
  5143. byte *alpn_peer_requested; /* the ALPN bytes requested by peer, sequence
  5144. * of length byte + chars */
  5145. word16 alpn_peer_requested_length; /* number of bytes total */
  5146. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  5147. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_QUIC)
  5148. CallbackALPNSelect alpnSelect;
  5149. void* alpnSelectArg;
  5150. #endif
  5151. #endif /* of accepted protocols */
  5152. #if !defined(NO_WOLFSSL_CLIENT) && defined(HAVE_SESSION_TICKET)
  5153. CallbackSessionTicket session_ticket_cb;
  5154. void* session_ticket_ctx;
  5155. byte expect_session_ticket;
  5156. #endif
  5157. #endif /* HAVE_TLS_EXTENSIONS */
  5158. #ifdef HAVE_OCSP
  5159. void* ocspIOCtx;
  5160. byte ocspProducedDate[MAX_DATE_SZ];
  5161. int ocspProducedDateFormat;
  5162. #ifdef OPENSSL_EXTRA
  5163. byte* ocspResp;
  5164. int ocspRespSz;
  5165. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
  5166. char* url;
  5167. #endif
  5168. #endif
  5169. #endif
  5170. #ifdef HAVE_NETX
  5171. NetX_Ctx nxCtx; /* NetX IO Context */
  5172. #endif
  5173. #if defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP)
  5174. void* mnCtx; /* mynewt mn_socket IO Context */
  5175. #endif /* defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) */
  5176. #ifdef WOLFSSL_GNRC
  5177. struct gnrc_wolfssl_ctx *gnrcCtx; /* Riot-OS GNRC UDP/IP context */
  5178. #endif
  5179. #ifdef SESSION_INDEX
  5180. int sessionIndex; /* Session's location in the cache. */
  5181. #endif
  5182. #ifdef ATOMIC_USER
  5183. void* MacEncryptCtx; /* Atomic User Mac/Encrypt Callback Context */
  5184. void* DecryptVerifyCtx; /* Atomic User Decrypt/Verify Callback Context */
  5185. #ifdef HAVE_ENCRYPT_THEN_MAC
  5186. void* EncryptMacCtx; /* Atomic User Encrypt/Mac Callback Ctx */
  5187. void* VerifyDecryptCtx; /* Atomic User Verify/Decrypt Callback Ctx */
  5188. #endif
  5189. #endif
  5190. #ifdef HAVE_PK_CALLBACKS
  5191. #ifdef HAVE_ECC
  5192. void* EccKeyGenCtx; /* EccKeyGen Callback Context */
  5193. void* EccSignCtx; /* Ecc Sign Callback Context */
  5194. void* EccVerifyCtx; /* Ecc Verify Callback Context */
  5195. void* EccSharedSecretCtx; /* Ecc Pms Callback Context */
  5196. #endif /* HAVE_ECC */
  5197. #ifdef HAVE_HKDF
  5198. void* HkdfExtractCtx; /* Hkdf extract callback context */
  5199. #endif
  5200. #ifdef HAVE_ED25519
  5201. void* Ed25519SignCtx; /* ED25519 Sign Callback Context */
  5202. void* Ed25519VerifyCtx; /* ED25519 Verify Callback Context */
  5203. #endif
  5204. #ifdef HAVE_CURVE25519
  5205. void* X25519KeyGenCtx; /* X25519 KeyGen Callback Context */
  5206. void* X25519SharedSecretCtx; /* X25519 Pms Callback Context */
  5207. #endif
  5208. #ifdef HAVE_ED448
  5209. void* Ed448SignCtx; /* ED448 Sign Callback Context */
  5210. void* Ed448VerifyCtx; /* ED448 Verify Callback Context */
  5211. #endif
  5212. #ifdef HAVE_CURVE448
  5213. void* X448KeyGenCtx; /* X448 KeyGen Callback Context */
  5214. void* X448SharedSecretCtx; /* X448 Pms Callback Context */
  5215. #endif
  5216. #ifndef NO_DH
  5217. void* DhAgreeCtx; /* DH Pms Callback Context */
  5218. #endif /* !NO_DH */
  5219. #ifndef NO_RSA
  5220. void* RsaSignCtx; /* Rsa Sign Callback Context */
  5221. void* RsaVerifyCtx; /* Rsa Verify Callback Context */
  5222. #ifdef WC_RSA_PSS
  5223. void* RsaPssSignCtx; /* Rsa PSS Sign Callback Context */
  5224. void* RsaPssVerifyCtx; /* Rsa PSS Verify Callback Context */
  5225. #endif
  5226. void* RsaEncCtx; /* Rsa Public Encrypt Callback Context */
  5227. void* RsaDecCtx; /* Rsa Private Decrypt Callback Context */
  5228. #endif /* NO_RSA */
  5229. void* GenPreMasterCtx; /* Generate Premaster Callback Context */
  5230. void* GenMasterCtx; /* Generate Master Callback Context */
  5231. void* GenSessionKeyCtx; /* Generate Session Key Callback Context */
  5232. void* EncryptKeysCtx; /* Set Encrypt keys Callback Context */
  5233. void* TlsFinishedCtx; /* Generate Tls Finished Callback Context */
  5234. void* VerifyMacCtx; /* Verify mac Callback Context */
  5235. #endif /* HAVE_PK_CALLBACKS */
  5236. #ifdef HAVE_SECRET_CALLBACK
  5237. SessionSecretCb sessionSecretCb;
  5238. void* sessionSecretCtx;
  5239. #ifdef WOLFSSL_TLS13
  5240. Tls13SecretCb tls13SecretCb;
  5241. void* tls13SecretCtx;
  5242. #endif
  5243. #ifdef OPENSSL_EXTRA
  5244. SessionSecretCb keyLogCb;
  5245. #ifdef WOLFSSL_TLS13
  5246. Tls13SecretCb tls13KeyLogCb;
  5247. #endif
  5248. #endif
  5249. #endif /* HAVE_SECRET_CALLBACK */
  5250. #ifdef WOLFSSL_JNI
  5251. void* jObjectRef; /* reference to WolfSSLSession in JNI wrapper */
  5252. #endif /* WOLFSSL_JNI */
  5253. #ifdef WOLFSSL_EARLY_DATA
  5254. EarlyDataState earlyData;
  5255. word32 earlyDataSz;
  5256. byte earlyDataStatus;
  5257. #endif
  5258. #ifdef OPENSSL_ALL
  5259. long verifyCallbackResult;
  5260. #endif
  5261. #if defined(OPENSSL_EXTRA)
  5262. WOLFSSL_STACK* supportedCiphers; /* Used in wolfSSL_get_ciphers_compat */
  5263. WOLFSSL_STACK* peerCertChain; /* Used in wolfSSL_get_peer_cert_chain */
  5264. #ifdef KEEP_OUR_CERT
  5265. WOLFSSL_STACK* ourCertChain; /* Used in wolfSSL_add1_chain_cert */
  5266. #endif
  5267. #endif
  5268. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5269. StaticKeyExchangeInfo_t staticKE;
  5270. #endif
  5271. #ifdef WOLFSSL_MAXQ10XX_TLS
  5272. maxq_ssl_t maxq_ctx;
  5273. #endif
  5274. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5275. /* Added in libest port: allow applications to get the 'tls-unique' Channel
  5276. * Binding Type (https://tools.ietf.org/html/rfc5929#section-3). This is
  5277. * used in the EST protocol to bind an enrollment to a TLS session through
  5278. * 'proof-of-possession' (https://tools.ietf.org/html/rfc7030#section-3.4
  5279. * and https://tools.ietf.org/html/rfc7030#section-3.5). */
  5280. byte clientFinished[TLS_FINISHED_SZ_MAX];
  5281. byte serverFinished[TLS_FINISHED_SZ_MAX];
  5282. byte clientFinished_len;
  5283. byte serverFinished_len;
  5284. #endif
  5285. #ifndef WOLFSSL_NO_CA_NAMES
  5286. WOLF_STACK_OF(WOLFSSL_X509_NAME)* client_ca_names;
  5287. #endif
  5288. #if defined(WOLFSSL_IOTSAFE) && defined(HAVE_PK_CALLBACKS)
  5289. IOTSAFE iotsafe;
  5290. #endif
  5291. #ifdef WOLFSSL_LWIP_NATIVE
  5292. WOLFSSL_LWIP_NATIVE_STATE lwipCtx; /* LwIP native socket IO Context */
  5293. #endif
  5294. #ifdef WOLFSSL_QUIC
  5295. struct {
  5296. const WOLFSSL_QUIC_METHOD* method;
  5297. WOLFSSL_ENCRYPTION_LEVEL enc_level_read;
  5298. WOLFSSL_ENCRYPTION_LEVEL enc_level_read_next;
  5299. WOLFSSL_ENCRYPTION_LEVEL enc_level_latest_recvd;
  5300. WOLFSSL_ENCRYPTION_LEVEL enc_level_write;
  5301. WOLFSSL_ENCRYPTION_LEVEL enc_level_write_next;
  5302. int transport_version;
  5303. const QuicTransportParam* transport_local;
  5304. const QuicTransportParam* transport_peer;
  5305. const QuicTransportParam* transport_peer_draft;
  5306. QuicRecord* input_head; /* we own, data for handshake */
  5307. QuicRecord* input_tail; /* points to last element for append */
  5308. QuicRecord* scratch; /* we own, record construction */
  5309. enum wolfssl_encryption_level_t output_rec_level;
  5310. /* encryption level of current output record */
  5311. word32 output_rec_remain; /* how many bytes of output TLS record
  5312. * content have not been handled yet by quic */
  5313. } quic;
  5314. #endif /* WOLFSSL_QUIC */
  5315. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  5316. WOLFSSL_EchConfig* echConfigs;
  5317. #endif
  5318. #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE)
  5319. SSLSnifferSecretCb snifferSecretCb;
  5320. #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */
  5321. };
  5322. /*
  5323. * wolfSSL_PEM_read_bio_X509 pushes an ASN_NO_PEM_HEADER error
  5324. * to the error queue on file end. This should not be left
  5325. * for the caller to find so we clear the last error.
  5326. */
  5327. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_HAVE_ERROR_QUEUE)
  5328. #define CLEAR_ASN_NO_PEM_HEADER_ERROR(err) \
  5329. (err) = wolfSSL_ERR_peek_last_error(); \
  5330. if (ERR_GET_LIB(err) == ERR_LIB_PEM && \
  5331. ERR_GET_REASON(err) == PEM_R_NO_START_LINE) { \
  5332. wc_RemoveErrorNode(-1); \
  5333. }
  5334. #else
  5335. #define CLEAR_ASN_NO_PEM_HEADER_ERROR(err) (void)(err);
  5336. #endif
  5337. /*
  5338. * The SSL object may have its own certificate store. The below macros simplify
  5339. * logic for choosing which WOLFSSL_CERT_MANAGER and WOLFSSL_X509_STORE to use.
  5340. * Always use SSL specific objects when available and revert to CTX otherwise.
  5341. */
  5342. #ifdef WOLFSSL_LOCAL_X509_STORE
  5343. #define SSL_CM(ssl) ((ssl)->x509_store_pt ? (ssl)->x509_store_pt->cm : \
  5344. ((ssl)->ctx->x509_store_pt ? (ssl)->ctx->x509_store_pt->cm : \
  5345. (ssl)->ctx->cm))
  5346. #define SSL_STORE(ssl) ((ssl)->x509_store_pt ? (ssl)->x509_store_pt : \
  5347. ((ssl)->ctx->x509_store_pt ? (ssl)->ctx->x509_store_pt : \
  5348. &(ssl)->ctx->x509_store))
  5349. #define CTX_STORE(ctx) ((ctx)->x509_store_pt ? (ctx)->x509_store_pt : \
  5350. &(ctx)->x509_store)
  5351. #else
  5352. #define SSL_CM(ssl) (ssl)->ctx->cm
  5353. #endif
  5354. /* Issue warning when we are modifying the overall context CM */
  5355. #define SSL_CM_WARNING(ssl) \
  5356. do { \
  5357. if (SSL_CM( (ssl) ) == (ssl)->ctx->cm) { \
  5358. WOLFSSL_MSG("Modifying SSL_CTX CM not SSL specific CM"); \
  5359. } \
  5360. } while (0)
  5361. WOLFSSL_LOCAL int SetSSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup);
  5362. WOLFSSL_LOCAL int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup);
  5363. WOLFSSL_LOCAL int ReinitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup);
  5364. WOLFSSL_LOCAL void FreeSSL(WOLFSSL* ssl, void* heap);
  5365. WOLFSSL_API void SSL_ResourceFree(WOLFSSL* ssl); /* Micrium uses */
  5366. #ifndef NO_CERTS
  5367. WOLFSSL_LOCAL int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff,
  5368. long sz, int format, int type, WOLFSSL* ssl,
  5369. long* used, int userChain, int verify);
  5370. WOLFSSL_LOCAL int ProcessFile(WOLFSSL_CTX* ctx, const char* fname, int format,
  5371. int type, WOLFSSL* ssl, int userChain,
  5372. WOLFSSL_CRL* crl, int verify);
  5373. WOLFSSL_LOCAL int CheckHostName(DecodedCert* dCert, const char *domainName,
  5374. size_t domainNameLen);
  5375. #endif
  5376. #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
  5377. WOLFSSL_LOCAL void InitHandShakeInfo(HandShakeInfo* info, WOLFSSL* ssl);
  5378. WOLFSSL_LOCAL void FinishHandShakeInfo(HandShakeInfo* info);
  5379. WOLFSSL_LOCAL void AddPacketName(WOLFSSL* ssl, const char* name);
  5380. WOLFSSL_LOCAL void InitTimeoutInfo(TimeoutInfo* info);
  5381. WOLFSSL_LOCAL void FreeTimeoutInfo(TimeoutInfo* info, void* heap);
  5382. WOLFSSL_LOCAL int AddPacketInfo(WOLFSSL* ssl, const char* name, int type,
  5383. const byte* data, int sz, int written, int lateRL,
  5384. void* heap);
  5385. WOLFSSL_LOCAL void AddLateName(const char* name, TimeoutInfo* info);
  5386. WOLFSSL_LOCAL void AddLateRecordHeader(const RecordLayerHeader* rl,
  5387. TimeoutInfo* info);
  5388. #endif
  5389. /* Record Layer Header identifier from page 12 */
  5390. enum ContentType {
  5391. no_type = 0,
  5392. change_cipher_spec = 20,
  5393. alert = 21,
  5394. handshake = 22,
  5395. application_data = 23,
  5396. #ifdef WOLFSSL_DTLS13
  5397. ack = 26,
  5398. #endif /* WOLFSSL_DTLS13 */
  5399. };
  5400. /* handshake header, same for each message type, pgs 20/21 */
  5401. typedef struct HandShakeHeader {
  5402. byte type;
  5403. word24 length;
  5404. } HandShakeHeader;
  5405. /* DTLS handshake header, same for each message type */
  5406. typedef struct DtlsHandShakeHeader {
  5407. byte type;
  5408. word24 length;
  5409. byte message_seq[2]; /* start at 0, retransmit gets same # */
  5410. word24 fragment_offset; /* bytes in previous fragments */
  5411. word24 fragment_length; /* length of this fragment */
  5412. } DtlsHandShakeHeader;
  5413. enum HandShakeType {
  5414. hello_request = 0,
  5415. client_hello = 1,
  5416. server_hello = 2,
  5417. hello_verify_request = 3, /* DTLS addition */
  5418. session_ticket = 4,
  5419. end_of_early_data = 5,
  5420. hello_retry_request = 6,
  5421. encrypted_extensions = 8,
  5422. certificate = 11,
  5423. server_key_exchange = 12,
  5424. certificate_request = 13,
  5425. server_hello_done = 14,
  5426. certificate_verify = 15,
  5427. client_key_exchange = 16,
  5428. finished = 20,
  5429. certificate_status = 22,
  5430. key_update = 24,
  5431. change_cipher_hs = 55, /* simulate unique handshake type for sanity
  5432. checks. record layer change_cipher
  5433. conflicts with handshake finished */
  5434. message_hash = 254, /* synthetic message type for TLS v1.3 */
  5435. no_shake = 255 /* used to initialize the DtlsMsg record */
  5436. };
  5437. enum ProvisionSide {
  5438. PROVISION_CLIENT = 1,
  5439. PROVISION_SERVER = 2,
  5440. PROVISION_CLIENT_SERVER = 3
  5441. };
  5442. static const byte kTlsClientStr[SIZEOF_SENDER+1] = { 0x43, 0x4C, 0x4E, 0x54, 0x00 }; /* CLNT */
  5443. static const byte kTlsServerStr[SIZEOF_SENDER+1] = { 0x53, 0x52, 0x56, 0x52, 0x00 }; /* SRVR */
  5444. static const byte kTlsClientFinStr[FINISHED_LABEL_SZ + 1] = "client finished";
  5445. static const byte kTlsServerFinStr[FINISHED_LABEL_SZ + 1] = "server finished";
  5446. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  5447. typedef struct {
  5448. int name_len;
  5449. const char *name;
  5450. int nid;
  5451. } WOLF_EC_NIST_NAME;
  5452. extern const WOLF_EC_NIST_NAME kNistCurves[];
  5453. /* This is the longest and shortest curve name in the kNistCurves list. Note we
  5454. * also have quantum-safe group names as well. */
  5455. #define kNistCurves_MIN_NAME_LEN 5
  5456. #ifdef HAVE_PQC
  5457. #define kNistCurves_MAX_NAME_LEN 32
  5458. #else
  5459. #define kNistCurves_MAX_NAME_LEN 7
  5460. #endif
  5461. #endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
  5462. /* internal functions */
  5463. WOLFSSL_LOCAL int SendChangeCipher(WOLFSSL* ssl);
  5464. WOLFSSL_LOCAL int SendTicket(WOLFSSL* ssl);
  5465. #ifdef HAVE_SESSION_TICKET
  5466. WOLFSSL_LOCAL int DoDecryptTicket(const WOLFSSL* ssl, const byte* input,
  5467. word32 len, InternalTicket **it);
  5468. /* Return 0 when check successful. <0 on failure. */
  5469. WOLFSSL_LOCAL void DoClientTicketFinalize(WOLFSSL* ssl, InternalTicket* it,
  5470. const WOLFSSL_SESSION* sess);
  5471. #ifdef WOLFSSL_TLS13
  5472. WOLFSSL_LOCAL int DoClientTicketCheck(const WOLFSSL* ssl,
  5473. const PreSharedKey* psk, sword64 timeout, const byte* suite);
  5474. WOLFSSL_LOCAL void CleanupClientTickets(PreSharedKey* psk);
  5475. WOLFSSL_LOCAL int DoClientTicket_ex(const WOLFSSL* ssl, PreSharedKey* psk,
  5476. int retainSess);
  5477. #endif
  5478. WOLFSSL_LOCAL int DoClientTicket(WOLFSSL* ssl, const byte* input, word32 len);
  5479. #endif /* HAVE_SESSION_TICKET */
  5480. WOLFSSL_LOCAL int SendData(WOLFSSL* ssl, const void* data, int sz);
  5481. #ifdef WOLFSSL_TLS13
  5482. WOLFSSL_LOCAL int SendTls13ServerHello(WOLFSSL* ssl, byte extMsgType);
  5483. #endif
  5484. WOLFSSL_LOCAL int SendCertificate(WOLFSSL* ssl);
  5485. WOLFSSL_LOCAL int SendCertificateRequest(WOLFSSL* ssl);
  5486. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  5487. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  5488. WOLFSSL_LOCAL int CreateOcspResponse(WOLFSSL* ssl, OcspRequest** ocspRequest,
  5489. buffer* response);
  5490. #endif
  5491. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  5492. !defined(NO_WOLFSSL_SERVER)
  5493. WOLFSSL_LOCAL int SendHelloRequest(WOLFSSL* ssl);
  5494. #endif
  5495. WOLFSSL_LOCAL int SendCertificateStatus(WOLFSSL* ssl);
  5496. WOLFSSL_LOCAL int SendServerKeyExchange(WOLFSSL* ssl);
  5497. WOLFSSL_LOCAL int SendBuffered(WOLFSSL* ssl);
  5498. WOLFSSL_LOCAL int ReceiveData(WOLFSSL* ssl, byte* output, int sz, int peek);
  5499. WOLFSSL_LOCAL int SendFinished(WOLFSSL* ssl);
  5500. WOLFSSL_LOCAL int RetrySendAlert(WOLFSSL* ssl);
  5501. WOLFSSL_LOCAL int SendAlert(WOLFSSL* ssl, int severity, int type);
  5502. WOLFSSL_LOCAL int SendFatalAlertOnly(WOLFSSL *ssl, int error);
  5503. WOLFSSL_LOCAL int ProcessReply(WOLFSSL* ssl);
  5504. WOLFSSL_LOCAL int ProcessReplyEx(WOLFSSL* ssl, int allowSocketErr);
  5505. WOLFSSL_LOCAL const char* AlertTypeToString(int type);
  5506. WOLFSSL_LOCAL int SetCipherSpecs(WOLFSSL* ssl);
  5507. WOLFSSL_LOCAL int GetCipherSpec(word16 side, byte cipherSuite0,
  5508. byte cipherSuite, CipherSpecs* specs, Options* opts);
  5509. WOLFSSL_LOCAL int MakeMasterSecret(WOLFSSL* ssl);
  5510. WOLFSSL_LOCAL int DeriveKeys(WOLFSSL* ssl);
  5511. WOLFSSL_LOCAL int StoreKeys(WOLFSSL* ssl, const byte* keyData, int side);
  5512. WOLFSSL_LOCAL int IsTLS(const WOLFSSL* ssl);
  5513. WOLFSSL_LOCAL int IsAtLeastTLSv1_2(const WOLFSSL* ssl);
  5514. WOLFSSL_LOCAL int IsAtLeastTLSv1_3(ProtocolVersion pv);
  5515. WOLFSSL_LOCAL int IsEncryptionOn(WOLFSSL* ssl, int isSend);
  5516. WOLFSSL_LOCAL int TLSv1_3_Capable(WOLFSSL* ssl);
  5517. WOLFSSL_LOCAL void FreeHandshakeResources(WOLFSSL* ssl);
  5518. WOLFSSL_LOCAL void ShrinkInputBuffer(WOLFSSL* ssl, int forcedFree);
  5519. WOLFSSL_LOCAL void ShrinkOutputBuffer(WOLFSSL* ssl);
  5520. WOLFSSL_LOCAL byte* GetOutputBuffer(WOLFSSL* ssl);
  5521. WOLFSSL_LOCAL int VerifyClientSuite(word16 havePSK, byte cipherSuite0,
  5522. byte cipherSuite);
  5523. WOLFSSL_LOCAL int SetTicket(WOLFSSL* ssl, const byte* ticket, word32 length);
  5524. WOLFSSL_LOCAL int wolfSSL_GetMaxFragSize(WOLFSSL* ssl, int maxFragment);
  5525. #if defined(WOLFSSL_IOTSAFE) && defined(HAVE_PK_CALLBACKS)
  5526. WOLFSSL_LOCAL IOTSAFE *wolfSSL_get_iotsafe_ctx(WOLFSSL *ssl);
  5527. WOLFSSL_LOCAL int wolfSSL_set_iotsafe_ctx(WOLFSSL *ssl, IOTSAFE *iotsafe);
  5528. #endif
  5529. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && defined(HAVE_ECC)
  5530. WOLFSSL_LOCAL int SetECKeyInternal(WOLFSSL_EC_KEY* eckey);
  5531. WOLFSSL_LOCAL int SetECKeyExternal(WOLFSSL_EC_KEY* eckey);
  5532. #endif
  5533. #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
  5534. WOLFSSL_LOCAL int wolfSSL_curve_is_disabled(const WOLFSSL* ssl,
  5535. word16 named_curve);
  5536. #else
  5537. #define wolfSSL_curve_is_disabled(ssl, c) ((void)(ssl), (void)(c), 0)
  5538. #endif
  5539. WOLFSSL_LOCAL WC_RNG* WOLFSSL_RSA_GetRNG(WOLFSSL_RSA *rsa, WC_RNG **tmpRNG,
  5540. int *initTmpRng);
  5541. #ifndef NO_CERTS
  5542. #ifndef NO_RSA
  5543. #ifdef WC_RSA_PSS
  5544. WOLFSSL_LOCAL int CheckRsaPssPadding(const byte* plain, word32 plainSz,
  5545. byte* out, word32 sigSz, enum wc_HashType hashType);
  5546. WOLFSSL_LOCAL int ConvertHashPss(int hashAlgo,
  5547. enum wc_HashType* hashType, int* mgf);
  5548. #endif
  5549. WOLFSSL_LOCAL int VerifyRsaSign(WOLFSSL* ssl, byte* verifySig,
  5550. word32 sigSz, const byte* plain, word32 plainSz, int sigAlgo,
  5551. int hashAlgo, RsaKey* key, DerBuffer* keyBufInfo);
  5552. WOLFSSL_LOCAL int RsaSign(WOLFSSL* ssl, const byte* in, word32 inSz,
  5553. byte* out, word32* outSz, int sigAlgo, int hashAlgo, RsaKey* key,
  5554. DerBuffer* keyBufInfo);
  5555. WOLFSSL_LOCAL int RsaVerify(WOLFSSL* ssl, byte* in, word32 inSz,
  5556. byte** out, int sigAlgo, int hashAlgo, RsaKey* key,
  5557. buffer* keyBufInfo);
  5558. WOLFSSL_LOCAL int RsaDec(WOLFSSL* ssl, byte* in, word32 inSz, byte** out,
  5559. word32* outSz, RsaKey* key, DerBuffer* keyBufInfo);
  5560. WOLFSSL_LOCAL int RsaEnc(WOLFSSL* ssl, const byte* in, word32 inSz, byte* out,
  5561. word32* outSz, RsaKey* key, buffer* keyBufInfo);
  5562. #endif /* !NO_RSA */
  5563. #ifdef HAVE_ECC
  5564. WOLFSSL_LOCAL int EccSign(WOLFSSL* ssl, const byte* in, word32 inSz,
  5565. byte* out, word32* outSz, ecc_key* key, DerBuffer* keyBufInfo);
  5566. WOLFSSL_LOCAL int EccVerify(WOLFSSL* ssl, const byte* in, word32 inSz,
  5567. const byte* out, word32 outSz, ecc_key* key, buffer* keyBufInfo);
  5568. WOLFSSL_LOCAL int EccSharedSecret(WOLFSSL* ssl, ecc_key* priv_key,
  5569. ecc_key* pub_key, byte* pubKeyDer, word32* pubKeySz, byte* out,
  5570. word32* outlen, int side);
  5571. #endif /* HAVE_ECC */
  5572. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)
  5573. WOLFSSL_LOCAL int Sm2wSm3Sign(WOLFSSL* ssl, const byte* id, word32 idSz,
  5574. const byte* in, word32 inSz, byte* out, word32* outSz, ecc_key* key,
  5575. DerBuffer* keyBufInfo);
  5576. WOLFSSL_LOCAL int Sm2wSm3Verify(WOLFSSL* ssl, const byte* id,
  5577. word32 idSz, const byte* in, word32 inSz, const byte* out,
  5578. word32 outSz, ecc_key* key, buffer* keyBufInfo);
  5579. #endif /* WOLFSSL_SM2 && WOLFSSL_SM3 */
  5580. #ifdef HAVE_ED25519
  5581. WOLFSSL_LOCAL int Ed25519CheckPubKey(WOLFSSL* ssl);
  5582. WOLFSSL_LOCAL int Ed25519Sign(WOLFSSL* ssl, const byte* in, word32 inSz,
  5583. byte* out, word32* outSz, ed25519_key* key, DerBuffer* keyBufInfo);
  5584. WOLFSSL_LOCAL int Ed25519Verify(WOLFSSL* ssl, const byte* in,
  5585. word32 inSz, const byte* msg, word32 msgSz, ed25519_key* key,
  5586. buffer* keyBufInfo);
  5587. #endif /* HAVE_ED25519 */
  5588. #ifdef HAVE_ED448
  5589. WOLFSSL_LOCAL int Ed448CheckPubKey(WOLFSSL* ssl);
  5590. WOLFSSL_LOCAL int Ed448Sign(WOLFSSL* ssl, const byte* in, word32 inSz,
  5591. byte* out, word32* outSz, ed448_key* key, DerBuffer* keyBufInfo);
  5592. WOLFSSL_LOCAL int Ed448Verify(WOLFSSL* ssl, const byte* in,
  5593. word32 inSz, const byte* msg, word32 msgSz, ed448_key* key,
  5594. buffer* keyBufInfo);
  5595. #endif /* HAVE_ED448 */
  5596. #ifdef WOLFSSL_TRUST_PEER_CERT
  5597. /* options for searching hash table for a matching trusted peer cert */
  5598. #define WC_MATCH_SKID 0
  5599. #define WC_MATCH_NAME 1
  5600. WOLFSSL_LOCAL TrustedPeerCert* GetTrustedPeer(void* vp, DecodedCert* cert);
  5601. WOLFSSL_LOCAL int MatchTrustedPeer(TrustedPeerCert* tp,
  5602. DecodedCert* cert);
  5603. #endif
  5604. #ifndef GetCA
  5605. WOLFSSL_LOCAL Signer* GetCA(void* vp, byte* hash);
  5606. #endif
  5607. #ifdef WOLFSSL_AKID_NAME
  5608. WOLFSSL_LOCAL Signer* GetCAByAKID(void* vp, const byte* issuer,
  5609. word32 issuerSz, const byte* serial, word32 serialSz);
  5610. #endif
  5611. #if !defined(NO_SKID) && !defined(GetCAByName)
  5612. WOLFSSL_LOCAL Signer* GetCAByName(void* cm, byte* hash);
  5613. #endif
  5614. #endif /* !NO_CERTS */
  5615. WOLFSSL_LOCAL int BuildTlsHandshakeHash(WOLFSSL* ssl, byte* hash,
  5616. word32* hashLen);
  5617. WOLFSSL_LOCAL int BuildTlsFinished(WOLFSSL* ssl, Hashes* hashes,
  5618. const byte* sender);
  5619. WOLFSSL_LOCAL void FreeArrays(WOLFSSL* ssl, int keep);
  5620. WOLFSSL_LOCAL int CheckAvailableSize(WOLFSSL *ssl, int size);
  5621. WOLFSSL_LOCAL int GrowInputBuffer(WOLFSSL* ssl, int size, int usedLength);
  5622. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  5623. WOLFSSL_LOCAL void DoCertFatalAlert(WOLFSSL* ssl, int ret);
  5624. #endif
  5625. #ifndef NO_TLS
  5626. WOLFSSL_LOCAL int MakeTlsMasterSecret(WOLFSSL* ssl);
  5627. #ifndef WOLFSSL_AEAD_ONLY
  5628. WOLFSSL_LOCAL int TLS_hmac(WOLFSSL* ssl, byte* digest, const byte* in,
  5629. word32 sz, int padSz, int content, int verify, int epochOrder);
  5630. #endif
  5631. #endif
  5632. WOLFSSL_LOCAL int cipherExtraData(WOLFSSL* ssl);
  5633. #ifndef NO_WOLFSSL_CLIENT
  5634. WOLFSSL_LOCAL int HaveUniqueSessionObj(WOLFSSL* ssl);
  5635. WOLFSSL_LOCAL int SendClientHello(WOLFSSL* ssl);
  5636. WOLFSSL_LOCAL int DoHelloVerifyRequest(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
  5637. word32 size);
  5638. #ifdef WOLFSSL_TLS13
  5639. WOLFSSL_LOCAL int SendTls13ClientHello(WOLFSSL* ssl);
  5640. #endif
  5641. WOLFSSL_LOCAL int SendClientKeyExchange(WOLFSSL* ssl);
  5642. WOLFSSL_LOCAL int SendCertificateVerify(WOLFSSL* ssl);
  5643. #endif /* NO_WOLFSSL_CLIENT */
  5644. #ifndef NO_WOLFSSL_SERVER
  5645. WOLFSSL_LOCAL int SendServerHello(WOLFSSL* ssl);
  5646. WOLFSSL_LOCAL int SendServerHelloDone(WOLFSSL* ssl);
  5647. #endif /* NO_WOLFSSL_SERVER */
  5648. #ifdef WOLFSSL_DTLS
  5649. WOLFSSL_LOCAL DtlsMsg* DtlsMsgNew(word32 sz, byte tx, void* heap);
  5650. WOLFSSL_LOCAL void DtlsMsgDelete(DtlsMsg* item, void* heap);
  5651. /* Use WOLFSSL_API to enable src/api.c testing */
  5652. WOLFSSL_API void DtlsMsgListDelete(DtlsMsg* head, void* heap);
  5653. WOLFSSL_LOCAL void DtlsTxMsgListClean(WOLFSSL* ssl);
  5654. WOLFSSL_LOCAL int DtlsMsgSet(DtlsMsg* msg, word32 seq, word16 epoch,
  5655. const byte* data, byte type,
  5656. word32 fragOffset, word32 fragSz, void* heap,
  5657. word32 totalLen);
  5658. /* Use WOLFSSL_API to enable src/api.c testing */
  5659. WOLFSSL_API DtlsMsg* DtlsMsgFind(DtlsMsg* head, word16 epoch, word32 seq);
  5660. /* Use WOLFSSL_API to enable src/api.c testing */
  5661. WOLFSSL_API void DtlsMsgStore(WOLFSSL* ssl, word16 epoch, word32 seq,
  5662. const byte* data, word32 dataSz, byte type,
  5663. word32 fragOffset, word32 fragSz,
  5664. void* heap);
  5665. WOLFSSL_LOCAL DtlsMsg* DtlsMsgInsert(DtlsMsg* head, DtlsMsg* item);
  5666. WOLFSSL_LOCAL int DtlsMsgPoolSave(WOLFSSL* ssl, const byte* data,
  5667. word32 dataSz, enum HandShakeType type);
  5668. WOLFSSL_LOCAL int DtlsMsgPoolTimeout(WOLFSSL* ssl);
  5669. WOLFSSL_LOCAL int VerifyForDtlsMsgPoolSend(WOLFSSL* ssl, byte type,
  5670. word32 fragOffset);
  5671. WOLFSSL_LOCAL int VerifyForTxDtlsMsgDelete(WOLFSSL* ssl, DtlsMsg* item);
  5672. WOLFSSL_LOCAL void DtlsMsgPoolReset(WOLFSSL* ssl);
  5673. WOLFSSL_LOCAL int DtlsMsgPoolSend(WOLFSSL* ssl, int sendOnlyFirstPacket);
  5674. WOLFSSL_LOCAL void DtlsMsgDestroyFragBucket(DtlsFragBucket* fragBucket, void* heap);
  5675. WOLFSSL_LOCAL int GetDtlsHandShakeHeader(WOLFSSL *ssl, const byte *input,
  5676. word32 *inOutIdx, byte *type, word32 *size, word32 *fragOffset,
  5677. word32 *fragSz, word32 totalSz);
  5678. WOLFSSL_LOCAL int DtlsMsgDrain(WOLFSSL *ssl);
  5679. WOLFSSL_LOCAL int SendHelloVerifyRequest(WOLFSSL* ssl,
  5680. const byte* cookie, byte cookieSz);
  5681. #if !defined(NO_WOLFSSL_SERVER)
  5682. WOLFSSL_LOCAL int DoClientHelloStateless(WOLFSSL* ssl,
  5683. const byte* input, word32* inOutIdx, word32 helloSz);
  5684. #endif /* !defined(NO_WOLFSSL_SERVER) */
  5685. #endif /* WOLFSSL_DTLS */
  5686. #if defined(HAVE_SECURE_RENEGOTIATION) && defined(WOLFSSL_DTLS)
  5687. WOLFSSL_LOCAL int DtlsSCRKeysSet(WOLFSSL* ssl);
  5688. WOLFSSL_LOCAL int IsDtlsMsgSCRKeys(WOLFSSL* ssl);
  5689. WOLFSSL_LOCAL int DtlsUseSCRKeys(WOLFSSL* ssl);
  5690. WOLFSSL_LOCAL int DtlsCheckOrder(WOLFSSL* ssl, int order);
  5691. #endif
  5692. WOLFSSL_LOCAL int IsSCR(WOLFSSL* ssl);
  5693. WOLFSSL_LOCAL int IsDtlsNotSctpMode(WOLFSSL* ssl);
  5694. WOLFSSL_LOCAL int IsDtlsNotSrtpMode(WOLFSSL* ssl);
  5695. WOLFSSL_LOCAL void WriteSEQ(WOLFSSL* ssl, int verifyOrder, byte* out);
  5696. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  5697. #ifdef WOLFSSL_32BIT_MILLI_TIME
  5698. WOLFSSL_LOCAL word32 TimeNowInMilliseconds(void);
  5699. #else
  5700. WOLFSSL_LOCAL sword64 TimeNowInMilliseconds(void);
  5701. #endif
  5702. #endif
  5703. WOLFSSL_LOCAL word32 LowResTimer(void);
  5704. WOLFSSL_LOCAL int FindSuiteSSL(const WOLFSSL* ssl, byte* suite);
  5705. #ifndef NO_CERTS
  5706. WOLFSSL_LOCAL void InitX509Name(WOLFSSL_X509_NAME* name, int dynamicFlag,
  5707. void* heap);
  5708. WOLFSSL_LOCAL void FreeX509Name(WOLFSSL_X509_NAME* name);
  5709. WOLFSSL_LOCAL void InitX509(WOLFSSL_X509* x509, int dynamicFlag,
  5710. void* heap);
  5711. WOLFSSL_LOCAL void FreeX509(WOLFSSL_X509* x509);
  5712. WOLFSSL_LOCAL int CopyDecodedToX509(WOLFSSL_X509* x509,
  5713. DecodedCert* dCert);
  5714. #endif
  5715. #ifndef MAX_CIPHER_NAME
  5716. #define MAX_CIPHER_NAME 50
  5717. #endif
  5718. #ifdef WOLFSSL_NAMES_STATIC
  5719. typedef char cipher_name[MAX_CIPHER_NAME];
  5720. #else
  5721. typedef const char* cipher_name;
  5722. #endif
  5723. typedef struct CipherSuiteInfo {
  5724. cipher_name name;
  5725. #ifndef NO_ERROR_STRINGS
  5726. cipher_name name_iana;
  5727. #endif
  5728. byte cipherSuite0;
  5729. byte cipherSuite;
  5730. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  5731. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  5732. byte minor;
  5733. byte major;
  5734. #endif
  5735. byte flags;
  5736. } CipherSuiteInfo;
  5737. WOLFSSL_LOCAL const CipherSuiteInfo* GetCipherNames(void);
  5738. WOLFSSL_LOCAL int GetCipherNamesSize(void);
  5739. WOLFSSL_LOCAL const char* GetCipherNameInternal(byte cipherSuite0, byte cipherSuite);
  5740. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  5741. /* used in wolfSSL_sk_CIPHER_description */
  5742. #define MAX_SEGMENTS 5
  5743. #define MAX_SEGMENT_SZ 20
  5744. WOLFSSL_LOCAL int wolfSSL_sk_CIPHER_description(WOLFSSL_CIPHER* cipher);
  5745. WOLFSSL_LOCAL const char* GetCipherSegment(const WOLFSSL_CIPHER* cipher,
  5746. char n[][MAX_SEGMENT_SZ]);
  5747. WOLFSSL_LOCAL const char* GetCipherProtocol(byte minor);
  5748. WOLFSSL_LOCAL const char* GetCipherKeaStr(char n[][MAX_SEGMENT_SZ]);
  5749. WOLFSSL_LOCAL const char* GetCipherAuthStr(char n[][MAX_SEGMENT_SZ]);
  5750. WOLFSSL_LOCAL const char* GetCipherEncStr(char n[][MAX_SEGMENT_SZ]);
  5751. WOLFSSL_LOCAL const char* GetCipherMacStr(char n[][MAX_SEGMENT_SZ]);
  5752. WOLFSSL_LOCAL int SetCipherBits(const char* enc);
  5753. WOLFSSL_LOCAL int IsCipherAEAD(char n[][MAX_SEGMENT_SZ]);
  5754. #endif
  5755. WOLFSSL_LOCAL const char* GetCipherNameIana(byte cipherSuite0, byte cipherSuite);
  5756. WOLFSSL_LOCAL const char* wolfSSL_get_cipher_name_internal(WOLFSSL* ssl);
  5757. WOLFSSL_LOCAL const char* wolfSSL_get_cipher_name_iana(WOLFSSL* ssl);
  5758. WOLFSSL_LOCAL int GetCipherSuiteFromName(const char* name, byte* cipherSuite0,
  5759. byte* cipherSuite, int* flags);
  5760. enum encrypt_side {
  5761. ENCRYPT_SIDE_ONLY = 1,
  5762. DECRYPT_SIDE_ONLY,
  5763. ENCRYPT_AND_DECRYPT_SIDE
  5764. };
  5765. WOLFSSL_LOCAL int SetKeysSide(WOLFSSL* ssl, enum encrypt_side side);
  5766. /* Set*Internal and Set*External functions */
  5767. WOLFSSL_LOCAL int SetDsaInternal(WOLFSSL_DSA* dsa);
  5768. WOLFSSL_LOCAL int SetDsaExternal(WOLFSSL_DSA* dsa);
  5769. #ifndef HAVE_USER_RSA
  5770. WOLFSSL_LOCAL int SetRsaExternal(WOLFSSL_RSA* rsa);
  5771. WOLFSSL_LOCAL int SetRsaInternal(WOLFSSL_RSA* rsa);
  5772. #endif
  5773. typedef enum elem_set {
  5774. ELEMENT_P = 0x01,
  5775. ELEMENT_Q = 0x02,
  5776. ELEMENT_G = 0x04,
  5777. ELEMENT_PUB = 0x08,
  5778. ELEMENT_PRV = 0x10,
  5779. } Element_Set;
  5780. WOLFSSL_LOCAL int SetDhExternal_ex(WOLFSSL_DH *dh, int elm );
  5781. WOLFSSL_LOCAL int SetDhInternal(WOLFSSL_DH* dh);
  5782. WOLFSSL_LOCAL int SetDhExternal(WOLFSSL_DH *dh);
  5783. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  5784. WOLFSSL_LOCAL int DhGenKeyPair(WOLFSSL* ssl, DhKey* dhKey,
  5785. byte* priv, word32* privSz,
  5786. byte* pub, word32* pubSz);
  5787. WOLFSSL_LOCAL int DhAgree(WOLFSSL* ssl, DhKey* dhKey,
  5788. const byte* priv, word32 privSz,
  5789. const byte* otherPub, word32 otherPubSz,
  5790. byte* agree, word32* agreeSz,
  5791. const byte* prime, word32 primeSz);
  5792. #endif /* !NO_DH */
  5793. #ifdef HAVE_ECC
  5794. WOLFSSL_LOCAL int EccMakeKey(WOLFSSL* ssl, ecc_key* key, ecc_key* peer);
  5795. WOLFSSL_LOCAL word16 GetCurveByOID(int oidSum);
  5796. #endif
  5797. WOLFSSL_LOCAL int InitHandshakeHashes(WOLFSSL* ssl);
  5798. WOLFSSL_LOCAL void FreeHandshakeHashes(WOLFSSL* ssl);
  5799. WOLFSSL_LOCAL int InitHandshakeHashesAndCopy(WOLFSSL* ssl, HS_Hashes* source,
  5800. HS_Hashes** destination);
  5801. #ifndef WOLFSSL_NO_TLS12
  5802. WOLFSSL_LOCAL void FreeBuildMsgArgs(WOLFSSL* ssl, BuildMsgArgs* args);
  5803. #endif
  5804. WOLFSSL_LOCAL int BuildMessage(WOLFSSL* ssl, byte* output, int outSz,
  5805. const byte* input, int inSz, int type, int hashOutput,
  5806. int sizeOnly, int asyncOkay, int epochOrder);
  5807. #ifdef WOLFSSL_TLS13
  5808. /* Use WOLFSSL_API to use this function in tests/api.c */
  5809. WOLFSSL_API int BuildTls13Message(WOLFSSL* ssl, byte* output, int outSz, const byte* input,
  5810. int inSz, int type, int hashOutput, int sizeOnly, int asyncOkay);
  5811. WOLFSSL_LOCAL int Tls13UpdateKeys(WOLFSSL* ssl);
  5812. #endif
  5813. WOLFSSL_LOCAL int AllocKey(WOLFSSL* ssl, int type, void** pKey);
  5814. WOLFSSL_LOCAL void FreeKey(WOLFSSL* ssl, int type, void** pKey);
  5815. #ifdef WOLFSSL_ASYNC_CRYPT
  5816. WOLFSSL_LOCAL int wolfSSL_AsyncInit(WOLFSSL* ssl, WC_ASYNC_DEV* asyncDev, word32 flags);
  5817. WOLFSSL_LOCAL int wolfSSL_AsyncPop(WOLFSSL* ssl, byte* state);
  5818. WOLFSSL_LOCAL int wolfSSL_AsyncPush(WOLFSSL* ssl, WC_ASYNC_DEV* asyncDev);
  5819. #endif
  5820. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  5821. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  5822. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  5823. WOLFSSL_LOCAL int LoadCertByIssuer(WOLFSSL_X509_STORE* store,
  5824. X509_NAME* issuer, int Type);
  5825. #endif
  5826. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  5827. WOLFSSL_LOCAL WOLFSSL_BY_DIR_HASH* wolfSSL_BY_DIR_HASH_new(void);
  5828. WOLFSSL_LOCAL void wolfSSL_BY_DIR_HASH_free(WOLFSSL_BY_DIR_HASH* dir_hash);
  5829. WOLFSSL_LOCAL WOLFSSL_STACK* wolfSSL_sk_BY_DIR_HASH_new_null(void);
  5830. WOLFSSL_LOCAL int wolfSSL_sk_BY_DIR_HASH_find(
  5831. WOLF_STACK_OF(WOLFSSL_BY_DIR_HASH)* sk, const WOLFSSL_BY_DIR_HASH* toFind);
  5832. WOLFSSL_LOCAL int wolfSSL_sk_BY_DIR_HASH_num(const WOLF_STACK_OF(WOLFSSL_BY_DIR_HASH) *sk);
  5833. WOLFSSL_LOCAL WOLFSSL_BY_DIR_HASH* wolfSSL_sk_BY_DIR_HASH_value(
  5834. const WOLF_STACK_OF(WOLFSSL_BY_DIR_HASH) *sk, int i);
  5835. WOLFSSL_LOCAL WOLFSSL_BY_DIR_HASH* wolfSSL_sk_BY_DIR_HASH_pop(
  5836. WOLF_STACK_OF(WOLFSSL_BY_DIR_HASH)* sk);
  5837. WOLFSSL_LOCAL void wolfSSL_sk_BY_DIR_HASH_pop_free(WOLF_STACK_OF(WOLFSSL_BY_DIR_HASH)* sk,
  5838. void (*f) (WOLFSSL_BY_DIR_HASH*));
  5839. WOLFSSL_LOCAL void wolfSSL_sk_BY_DIR_HASH_free(WOLF_STACK_OF(WOLFSSL_BY_DIR_HASH) *sk);
  5840. WOLFSSL_LOCAL int wolfSSL_sk_BY_DIR_HASH_push(WOLF_STACK_OF(WOLFSSL_BY_DIR_HASH)* sk,
  5841. WOLFSSL_BY_DIR_HASH* in);
  5842. /* WOLFSSL_BY_DIR_entry stuff */
  5843. WOLFSSL_LOCAL WOLFSSL_BY_DIR_entry* wolfSSL_BY_DIR_entry_new(void);
  5844. WOLFSSL_LOCAL void wolfSSL_BY_DIR_entry_free(WOLFSSL_BY_DIR_entry* entry);
  5845. WOLFSSL_LOCAL WOLFSSL_STACK* wolfSSL_sk_BY_DIR_entry_new_null(void);
  5846. WOLFSSL_LOCAL int wolfSSL_sk_BY_DIR_entry_num(const WOLF_STACK_OF(WOLFSSL_BY_DIR_entry) *sk);
  5847. WOLFSSL_LOCAL WOLFSSL_BY_DIR_entry* wolfSSL_sk_BY_DIR_entry_value(
  5848. const WOLF_STACK_OF(WOLFSSL_BY_DIR_entry) *sk, int i);
  5849. WOLFSSL_LOCAL WOLFSSL_BY_DIR_entry* wolfSSL_sk_BY_DIR_entry_pop(
  5850. WOLF_STACK_OF(WOLFSSL_BY_DIR_entry)* sk);
  5851. WOLFSSL_LOCAL void wolfSSL_sk_BY_DIR_entry_pop_free(WOLF_STACK_OF(wolfSSL_BY_DIR_entry)* sk,
  5852. void (*f) (WOLFSSL_BY_DIR_entry*));
  5853. WOLFSSL_LOCAL void wolfSSL_sk_BY_DIR_entry_free(WOLF_STACK_OF(wolfSSL_BY_DIR_entry) *sk);
  5854. WOLFSSL_LOCAL int wolfSSL_sk_BY_DIR_entry_push(WOLF_STACK_OF(wolfSSL_BY_DIR_entry)* sk,
  5855. WOLFSSL_BY_DIR_entry* in);
  5856. #endif /* OPENSSL_ALL && !NO_FILESYSTEM && !NO_WOLFSSL_DIR */
  5857. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  5858. WOLFSSL_LOCAL int oid2nid(word32 oid, int grp);
  5859. WOLFSSL_LOCAL word32 nid2oid(int nid, int grp);
  5860. #endif
  5861. #ifdef WOLFSSL_DTLS
  5862. WOLFSSL_API int wolfSSL_DtlsUpdateWindow(word16 cur_hi, word32 cur_lo,
  5863. word16* next_hi, word32* next_lo, word32 *window);
  5864. WOLFSSL_LOCAL void DtlsResetState(WOLFSSL *ssl);
  5865. WOLFSSL_LOCAL int DtlsIgnoreError(int err);
  5866. WOLFSSL_LOCAL void DtlsSetSeqNumForReply(WOLFSSL* ssl);
  5867. #endif
  5868. #ifdef WOLFSSL_DTLS13
  5869. /* Use WOLFSSL_API to use this function in tests/api.c */
  5870. WOLFSSL_API struct Dtls13Epoch* Dtls13GetEpoch(WOLFSSL* ssl,
  5871. w64wrapper epochNumber);
  5872. WOLFSSL_LOCAL void Dtls13SetOlderEpochSide(WOLFSSL* ssl, w64wrapper epochNumber,
  5873. int side);
  5874. WOLFSSL_LOCAL int Dtls13NewEpoch(WOLFSSL* ssl, w64wrapper epochNumber,
  5875. int side);
  5876. WOLFSSL_LOCAL int Dtls13SetEpochKeys(WOLFSSL* ssl, w64wrapper epochNumber,
  5877. enum encrypt_side side);
  5878. WOLFSSL_LOCAL int Dtls13GetSeq(WOLFSSL* ssl, int order, word32* seq,
  5879. byte increment);
  5880. WOLFSSL_LOCAL int Dtls13DoScheduledWork(WOLFSSL* ssl);
  5881. WOLFSSL_LOCAL int Dtls13DeriveSnKeys(WOLFSSL* ssl, int provision);
  5882. WOLFSSL_LOCAL int Dtls13SetRecordNumberKeys(WOLFSSL* ssl,
  5883. enum encrypt_side side);
  5884. WOLFSSL_LOCAL int Dtls13AddHeaders(byte* output, word32 length,
  5885. enum HandShakeType hs_type, WOLFSSL* ssl);
  5886. WOLFSSL_LOCAL word16 Dtls13GetHeadersLength(WOLFSSL *ssl,
  5887. enum HandShakeType type);
  5888. WOLFSSL_LOCAL word16 Dtls13GetRlHeaderLength(WOLFSSL *ssl, byte is_encrypted);
  5889. WOLFSSL_LOCAL int Dtls13RlAddCiphertextHeader(WOLFSSL* ssl, byte* out,
  5890. word16 length);
  5891. WOLFSSL_LOCAL int Dtls13RlAddPlaintextHeader(WOLFSSL* ssl, byte* out,
  5892. enum ContentType content_type, word16 length);
  5893. WOLFSSL_LOCAL int Dtls13EncryptRecordNumber(WOLFSSL* ssl, byte* hdr,
  5894. word16 recordLength);
  5895. WOLFSSL_LOCAL int Dtls13IsUnifiedHeader(byte header_flags);
  5896. WOLFSSL_LOCAL int Dtls13GetUnifiedHeaderSize(WOLFSSL* ssl, const byte input,
  5897. word16* size);
  5898. WOLFSSL_LOCAL int Dtls13ParseUnifiedRecordLayer(WOLFSSL* ssl, const byte* input,
  5899. word16 input_size, Dtls13UnifiedHdrInfo* hdrInfo);
  5900. WOLFSSL_LOCAL int Dtls13HandshakeSend(WOLFSSL* ssl, byte* output,
  5901. word16 output_size, word16 length, enum HandShakeType handshake_type,
  5902. int hash_output);
  5903. WOLFSSL_LOCAL int Dtls13RecordRecvd(WOLFSSL* ssl);
  5904. WOLFSSL_LOCAL int Dtls13HandshakeRecv(WOLFSSL* ssl, byte* input,
  5905. word32* inOutIdx, word32 totalSz);
  5906. WOLFSSL_LOCAL int Dtls13HandshakeAddHeader(WOLFSSL* ssl, byte* output,
  5907. enum HandShakeType msg_type, word32 length);
  5908. #define EE_MASK (0x3)
  5909. WOLFSSL_LOCAL int Dtls13FragmentsContinue(WOLFSSL* ssl);
  5910. WOLFSSL_LOCAL int DoDtls13Ack(WOLFSSL* ssl, const byte* input, word32 inputSize,
  5911. word32* processedSize);
  5912. WOLFSSL_LOCAL int Dtls13ReconstructEpochNumber(WOLFSSL* ssl, byte epochBits,
  5913. w64wrapper* epoch);
  5914. WOLFSSL_LOCAL int Dtls13ReconstructSeqNumber(WOLFSSL* ssl,
  5915. Dtls13UnifiedHdrInfo* hdrInfo, w64wrapper* out);
  5916. WOLFSSL_LOCAL int SendDtls13Ack(WOLFSSL* ssl);
  5917. WOLFSSL_LOCAL int Dtls13RtxProcessingCertificate(WOLFSSL* ssl, byte* input,
  5918. word32 inputSize);
  5919. WOLFSSL_LOCAL int Dtls13HashHandshake(WOLFSSL* ssl, const byte* input,
  5920. word16 length);
  5921. WOLFSSL_LOCAL int Dtls13HashClientHello(const WOLFSSL* ssl, byte* hash,
  5922. int* hashSz, const byte* body, word32 length, CipherSpecs* specs);
  5923. WOLFSSL_LOCAL void Dtls13FreeFsmResources(WOLFSSL* ssl);
  5924. WOLFSSL_LOCAL void Dtls13RtxFlushBuffered(WOLFSSL* ssl,
  5925. byte keepNewSessionTicket);
  5926. WOLFSSL_LOCAL int Dtls13RtxTimeout(WOLFSSL* ssl);
  5927. WOLFSSL_LOCAL int Dtls13ProcessBufferedMessages(WOLFSSL* ssl);
  5928. WOLFSSL_LOCAL int Dtls13CheckAEADFailLimit(WOLFSSL* ssl);
  5929. #endif /* WOLFSSL_DTLS13 */
  5930. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5931. WOLFSSL_LOCAL int wolfSSL_StaticEphemeralKeyLoad(WOLFSSL* ssl, int keyAlgo, void* keyPtr);
  5932. #endif
  5933. #ifndef NO_CERTS
  5934. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  5935. defined(OPENSSL_EXTRA_X509_SMALL)
  5936. WOLFSSL_LOCAL int wolfSSL_ASN1_STRING_canon(WOLFSSL_ASN1_STRING* asn_out,
  5937. const WOLFSSL_ASN1_STRING* asn_in);
  5938. #endif
  5939. #ifdef OPENSSL_EXTRA
  5940. WOLFSSL_LOCAL int GetX509Error(int e);
  5941. #endif
  5942. #endif
  5943. #if defined(HAVE_EX_DATA) && \
  5944. (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  5945. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  5946. defined(HAVE_LIGHTY)) || defined(HAVE_EX_DATA) || \
  5947. defined(WOLFSSL_WPAS_SMALL)
  5948. typedef struct CRYPTO_EX_cb_ctx {
  5949. long ctx_l;
  5950. void *ctx_ptr;
  5951. WOLFSSL_CRYPTO_EX_new* new_func;
  5952. WOLFSSL_CRYPTO_EX_free* free_func;
  5953. WOLFSSL_CRYPTO_EX_dup* dup_func;
  5954. struct CRYPTO_EX_cb_ctx* next;
  5955. } CRYPTO_EX_cb_ctx;
  5956. /* use wolfSSL_API visibility to be able to clear in tests/api.c */
  5957. WOLFSSL_API extern CRYPTO_EX_cb_ctx* crypto_ex_cb_ctx_session;
  5958. WOLFSSL_API void crypto_ex_cb_free(CRYPTO_EX_cb_ctx* cb_ctx);
  5959. WOLFSSL_LOCAL void crypto_ex_cb_setup_new_data(void *new_obj,
  5960. CRYPTO_EX_cb_ctx* cb_ctx, WOLFSSL_CRYPTO_EX_DATA* ex_data);
  5961. WOLFSSL_LOCAL void crypto_ex_cb_free_data(void *obj, CRYPTO_EX_cb_ctx* cb_ctx,
  5962. WOLFSSL_CRYPTO_EX_DATA* ex_data);
  5963. WOLFSSL_LOCAL int crypto_ex_cb_dup_data(const WOLFSSL_CRYPTO_EX_DATA *in,
  5964. WOLFSSL_CRYPTO_EX_DATA *out, CRYPTO_EX_cb_ctx* cb_ctx);
  5965. WOLFSSL_LOCAL int wolfssl_get_ex_new_index(int class_index, long ctx_l,
  5966. void* ctx_ptr, WOLFSSL_CRYPTO_EX_new* new_func,
  5967. WOLFSSL_CRYPTO_EX_dup* dup_func, WOLFSSL_CRYPTO_EX_free* free_func);
  5968. #endif
  5969. WOLFSSL_LOCAL WC_RNG* wolfssl_get_global_rng(void);
  5970. WOLFSSL_LOCAL WC_RNG* wolfssl_make_global_rng(void);
  5971. #if !defined(WOLFCRYPT_ONLY) && defined(OPENSSL_EXTRA)
  5972. #if defined(WOLFSSL_KEY_GEN) && defined(WOLFSSL_PEM_TO_DER)
  5973. WOLFSSL_LOCAL int EncryptDerKey(byte *der, int *derSz, const EVP_CIPHER* cipher,
  5974. unsigned char* passwd, int passwdSz, byte **cipherInfo, int maxDerSz);
  5975. #endif
  5976. #endif
  5977. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  5978. WOLFSSL_LOCAL int wolfSSL_RSA_To_Der(WOLFSSL_RSA* rsa, byte** outBuf,
  5979. int publicKey, void* heap);
  5980. #endif
  5981. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) \
  5982. || defined(OPENSSL_EXTRA) || defined(HAVE_LIGHTY) || defined(HAVE_SECRET_CALLBACK)
  5983. WOLFSSL_LOCAL int wolfSSL_SSL_do_handshake_internal(WOLFSSL *s);
  5984. #endif
  5985. #ifdef WOLFSSL_QUIC
  5986. #define WOLFSSL_IS_QUIC(s) (((s) != NULL) && ((s)->quic.method != NULL))
  5987. WOLFSSL_LOCAL int wolfSSL_quic_receive(WOLFSSL* ssl, byte* buf, word32 sz);
  5988. WOLFSSL_LOCAL int wolfSSL_quic_send(WOLFSSL* ssl);
  5989. WOLFSSL_LOCAL void wolfSSL_quic_clear(WOLFSSL* ssl);
  5990. WOLFSSL_LOCAL void wolfSSL_quic_free(WOLFSSL* ssl);
  5991. WOLFSSL_LOCAL int wolfSSL_quic_forward_secrets(WOLFSSL *ssl,
  5992. int ktype, int side);
  5993. WOLFSSL_LOCAL int wolfSSL_quic_keys_active(WOLFSSL* ssl, enum encrypt_side side);
  5994. #else
  5995. #define WOLFSSL_IS_QUIC(s) 0
  5996. #endif /* WOLFSSL_QUIC (else) */
  5997. #if defined(SHOW_SECRETS) && defined(WOLFSSL_SSLKEYLOGFILE)
  5998. WOLFSSL_LOCAL int tls13ShowSecrets(WOLFSSL* ssl, int id, const unsigned char* secret,
  5999. int secretSz, void* ctx);
  6000. #endif
  6001. /* Optional Pre-Master-Secret logging for Wireshark */
  6002. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
  6003. #ifndef WOLFSSL_SSLKEYLOGFILE_OUTPUT
  6004. #define WOLFSSL_SSLKEYLOGFILE_OUTPUT "sslkeylog.log"
  6005. #endif
  6006. #endif
  6007. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK)
  6008. WOLFSSL_LOCAL int FindPskSuite(const WOLFSSL* ssl, PreSharedKey* psk,
  6009. byte* psk_key, word32* psk_keySz, const byte* suite, int* found,
  6010. byte* foundSuite);
  6011. #endif
  6012. WOLFSSL_LOCAL int wolfSSL_GetHmacType_ex(CipherSpecs* specs);
  6013. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  6014. WOLFSSL_LOCAL int CreateCookieExt(const WOLFSSL* ssl, byte* hash,
  6015. word16 hashSz, TLSX** exts,
  6016. byte cipherSuite0, byte cipherSuite);
  6017. #endif
  6018. WOLFSSL_LOCAL int TranslateErrorToAlert(int err);
  6019. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  6020. void* wolfssl_sk_pop_type(WOLFSSL_STACK* sk, WOLF_STACK_TYPE type);
  6021. WOLFSSL_STACK* wolfssl_sk_new_type(WOLF_STACK_TYPE type);
  6022. #endif
  6023. #ifdef __cplusplus
  6024. } /* extern "C" */
  6025. #endif
  6026. #endif /* wolfSSL_INT_H */