internal.h 216 KB

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