internal.h 207 KB

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