internal.h 172 KB

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