2
0

internal.h 183 KB

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