internal.h 140 KB

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