internal.h 245 KB

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