internal.h 252 KB

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