internal.h 227 KB

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