internal.h 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369
  1. /* internal.h
  2. *
  3. * Copyright (C) 2006-2014 wolfSSL Inc.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL 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. * CyaSSL 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-1301, USA
  20. */
  21. #ifndef CYASSL_INT_H
  22. #define CYASSL_INT_H
  23. #include <cyassl/ctaocrypt/types.h>
  24. #include <cyassl/ssl.h>
  25. #include <cyassl/crl.h>
  26. #include <cyassl/ctaocrypt/random.h>
  27. #include <cyassl/ctaocrypt/des3.h>
  28. #include <cyassl/ctaocrypt/hc128.h>
  29. #include <cyassl/ctaocrypt/rabbit.h>
  30. #include <cyassl/ctaocrypt/chacha.h>
  31. #include <cyassl/ctaocrypt/asn.h>
  32. #include <cyassl/ctaocrypt/md5.h>
  33. #include <cyassl/ctaocrypt/sha.h>
  34. #include <cyassl/ctaocrypt/aes.h>
  35. #include <cyassl/ctaocrypt/poly1305.h>
  36. #include <cyassl/ctaocrypt/camellia.h>
  37. #include <cyassl/ctaocrypt/logging.h>
  38. #include <cyassl/ctaocrypt/hmac.h>
  39. #ifndef NO_RC4
  40. #include <wolfssl/wolfcrypt/arc4.h>
  41. #endif
  42. #ifdef HAVE_ECC
  43. #include <cyassl/ctaocrypt/ecc.h>
  44. #endif
  45. #ifndef NO_SHA256
  46. #include <cyassl/ctaocrypt/sha256.h>
  47. #endif
  48. #ifdef HAVE_OCSP
  49. #include <cyassl/ocsp.h>
  50. #endif
  51. #ifdef CYASSL_SHA512
  52. #include <cyassl/ctaocrypt/sha512.h>
  53. #endif
  54. #ifdef HAVE_AESGCM
  55. #include <cyassl/ctaocrypt/sha512.h>
  56. #endif
  57. #ifdef CYASSL_RIPEMD
  58. #include <cyassl/ctaocrypt/ripemd.h>
  59. #endif
  60. #ifdef CYASSL_CALLBACKS
  61. #include <cyassl/callbacks.h>
  62. #include <signal.h>
  63. #endif
  64. #ifdef USE_WINDOWS_API
  65. #ifdef CYASSL_GAME_BUILD
  66. #include "system/xtl.h"
  67. #else
  68. #if defined(_WIN32_WCE) || defined(WIN32_LEAN_AND_MEAN)
  69. /* On WinCE winsock2.h must be included before windows.h */
  70. #include <winsock2.h>
  71. #endif
  72. #include <windows.h>
  73. #endif
  74. #elif defined(THREADX)
  75. #ifndef SINGLE_THREADED
  76. #include "tx_api.h"
  77. #endif
  78. #elif defined(MICRIUM)
  79. /* do nothing, just don't pick Unix */
  80. #elif defined(FREERTOS) || defined(CYASSL_SAFERTOS)
  81. /* do nothing */
  82. #elif defined(EBSNET)
  83. /* do nothing */
  84. #elif defined(FREESCALE_MQX)
  85. /* do nothing */
  86. #elif defined(CYASSL_MDK_ARM)
  87. #if defined(CYASSL_MDK5)
  88. #include "cmsis_os.h"
  89. #else
  90. #include <rtl.h>
  91. #endif
  92. #elif defined(MBED)
  93. #elif defined(CYASSL_TIRTOS)
  94. /* do nothing */
  95. #else
  96. #ifndef SINGLE_THREADED
  97. #define CYASSL_PTHREADS
  98. #include <pthread.h>
  99. #endif
  100. #if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)
  101. #include <unistd.h> /* for close of BIO */
  102. #endif
  103. #endif
  104. #ifdef HAVE_LIBZ
  105. #include "zlib.h"
  106. #endif
  107. #ifdef _MSC_VER
  108. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  109. #pragma warning(disable: 4996)
  110. #endif
  111. #ifdef NO_AES
  112. #if !defined (ALIGN16)
  113. #define ALIGN16
  114. #endif
  115. #endif
  116. #ifdef NO_SHA
  117. #define SHA_DIGEST_SIZE 20
  118. #endif
  119. #ifdef NO_SHA256
  120. #define SHA256_DIGEST_SIZE 32
  121. #endif
  122. #ifdef NO_MD5
  123. #define MD5_DIGEST_SIZE 16
  124. #endif
  125. #ifdef __cplusplus
  126. extern "C" {
  127. #endif
  128. #ifdef USE_WINDOWS_API
  129. typedef unsigned int SOCKET_T;
  130. #else
  131. typedef int SOCKET_T;
  132. #endif
  133. typedef byte word24[3];
  134. /* Define or comment out the cipher suites you'd like to be compiled in
  135. make sure to use at least one BUILD_SSL_xxx or BUILD_TLS_xxx is defined
  136. When adding cipher suites, add name to cipher_names, idx to cipher_name_idx
  137. */
  138. #if !defined(NO_RSA) && !defined(NO_RC4)
  139. #if !defined(NO_SHA)
  140. #define BUILD_SSL_RSA_WITH_RC4_128_SHA
  141. #endif
  142. #if !defined(NO_MD5)
  143. #define BUILD_SSL_RSA_WITH_RC4_128_MD5
  144. #endif
  145. #if !defined(NO_TLS) && defined(HAVE_NTRU) && !defined(NO_SHA)
  146. #define BUILD_TLS_NTRU_RSA_WITH_RC4_128_SHA
  147. #endif
  148. #endif
  149. #if !defined(NO_RSA) && !defined(NO_DES3)
  150. #if !defined(NO_SHA)
  151. #define BUILD_SSL_RSA_WITH_3DES_EDE_CBC_SHA
  152. #if !defined(NO_TLS) && defined(HAVE_NTRU)
  153. #define BUILD_TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA
  154. #endif
  155. #endif
  156. #endif
  157. #if !defined(NO_RSA) && !defined(NO_AES) && !defined(NO_TLS)
  158. #if !defined(NO_SHA)
  159. #define BUILD_TLS_RSA_WITH_AES_128_CBC_SHA
  160. #define BUILD_TLS_RSA_WITH_AES_256_CBC_SHA
  161. #if defined(HAVE_NTRU)
  162. #define BUILD_TLS_NTRU_RSA_WITH_AES_128_CBC_SHA
  163. #define BUILD_TLS_NTRU_RSA_WITH_AES_256_CBC_SHA
  164. #endif
  165. #endif
  166. #if !defined (NO_SHA256)
  167. #define BUILD_TLS_RSA_WITH_AES_128_CBC_SHA256
  168. #define BUILD_TLS_RSA_WITH_AES_256_CBC_SHA256
  169. #endif
  170. #if defined (HAVE_AESGCM)
  171. #define BUILD_TLS_RSA_WITH_AES_128_GCM_SHA256
  172. #if defined (CYASSL_SHA384)
  173. #define BUILD_TLS_RSA_WITH_AES_256_GCM_SHA384
  174. #endif
  175. #endif
  176. #if defined (HAVE_AESCCM)
  177. #define BUILD_TLS_RSA_WITH_AES_128_CCM_8
  178. #define BUILD_TLS_RSA_WITH_AES_256_CCM_8
  179. #endif
  180. #if defined(HAVE_BLAKE2)
  181. #define BUILD_TLS_RSA_WITH_AES_128_CBC_B2B256
  182. #define BUILD_TLS_RSA_WITH_AES_256_CBC_B2B256
  183. #endif
  184. #endif
  185. #if defined(HAVE_CAMELLIA) && !defined(NO_TLS)
  186. #ifndef NO_RSA
  187. #if !defined(NO_SHA)
  188. #define BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA
  189. #define BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA
  190. #endif
  191. #ifndef NO_SHA256
  192. #define BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256
  193. #define BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256
  194. #endif
  195. #if !defined(NO_DH)
  196. #if !defined(NO_SHA)
  197. #define BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA
  198. #define BUILD_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA
  199. #endif
  200. #ifndef NO_SHA256
  201. #define BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256
  202. #define BUILD_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256
  203. #endif
  204. #endif
  205. #endif
  206. #endif
  207. #if !defined(NO_PSK) && !defined(NO_AES) && !defined(NO_TLS)
  208. #if !defined(NO_SHA)
  209. #define BUILD_TLS_PSK_WITH_AES_128_CBC_SHA
  210. #define BUILD_TLS_PSK_WITH_AES_256_CBC_SHA
  211. #endif
  212. #ifndef NO_SHA256
  213. #define BUILD_TLS_PSK_WITH_AES_128_CBC_SHA256
  214. #ifdef HAVE_AESGCM
  215. #define BUILD_TLS_PSK_WITH_AES_128_GCM_SHA256
  216. #endif
  217. #ifdef HAVE_AESCCM
  218. #define BUILD_TLS_PSK_WITH_AES_128_CCM_8
  219. #define BUILD_TLS_PSK_WITH_AES_256_CCM_8
  220. #define BUILD_TLS_PSK_WITH_AES_128_CCM
  221. #define BUILD_TLS_PSK_WITH_AES_256_CCM
  222. #endif
  223. #endif
  224. #ifdef CYASSL_SHA384
  225. #define BUILD_TLS_PSK_WITH_AES_256_CBC_SHA384
  226. #ifdef HAVE_AESGCM
  227. #define BUILD_TLS_PSK_WITH_AES_256_GCM_SHA384
  228. #endif
  229. #endif
  230. #endif
  231. #if !defined(NO_TLS) && defined(HAVE_NULL_CIPHER)
  232. #if !defined(NO_RSA)
  233. #if !defined(NO_SHA)
  234. #define BUILD_TLS_RSA_WITH_NULL_SHA
  235. #endif
  236. #ifndef NO_SHA256
  237. #define BUILD_TLS_RSA_WITH_NULL_SHA256
  238. #endif
  239. #endif
  240. #if !defined(NO_PSK)
  241. #if !defined(NO_SHA)
  242. #define BUILD_TLS_PSK_WITH_NULL_SHA
  243. #endif
  244. #ifndef NO_SHA256
  245. #define BUILD_TLS_PSK_WITH_NULL_SHA256
  246. #endif
  247. #ifdef CYASSL_SHA384
  248. #define BUILD_TLS_PSK_WITH_NULL_SHA384
  249. #endif
  250. #endif
  251. #endif
  252. #if !defined(NO_HC128) && !defined(NO_RSA) && !defined(NO_TLS)
  253. #define BUILD_TLS_RSA_WITH_HC_128_MD5
  254. #if !defined(NO_SHA)
  255. #define BUILD_TLS_RSA_WITH_HC_128_SHA
  256. #endif
  257. #if defined(HAVE_BLAKE2)
  258. #define BUILD_TLS_RSA_WITH_HC_128_B2B256
  259. #endif
  260. #endif
  261. #if !defined(NO_RABBIT) && !defined(NO_TLS) && !defined(NO_RSA)
  262. #if !defined(NO_SHA)
  263. #define BUILD_TLS_RSA_WITH_RABBIT_SHA
  264. #endif
  265. #endif
  266. #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
  267. !defined(NO_RSA)
  268. #if !defined(NO_SHA)
  269. #define BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  270. #define BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA
  271. #endif
  272. #if !defined (NO_SHA256)
  273. #define BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
  274. #define BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
  275. #if defined (HAVE_AESGCM)
  276. #define BUILD_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
  277. #if defined (CYASSL_SHA384)
  278. #define BUILD_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
  279. #endif
  280. #endif
  281. #endif
  282. #endif
  283. #if defined(HAVE_ANON) && !defined(NO_TLS) && !defined(NO_DH) && \
  284. !defined(NO_AES) && !defined(NO_SHA)
  285. #define BUILD_TLS_DH_anon_WITH_AES_128_CBC_SHA
  286. #endif
  287. #if !defined(NO_DH) && !defined(NO_PSK) && !defined(NO_TLS)
  288. #ifndef NO_SHA256
  289. #define BUILD_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256
  290. #ifdef HAVE_NULL_CIPHER
  291. #define BUILD_TLS_DHE_PSK_WITH_NULL_SHA256
  292. #endif
  293. #ifdef HAVE_AESGCM
  294. #define BUILD_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256
  295. #endif
  296. #ifdef HAVE_AESCCM
  297. #define BUILD_TLS_DHE_PSK_WITH_AES_128_CCM
  298. #define BUILD_TLS_DHE_PSK_WITH_AES_256_CCM
  299. #endif
  300. #endif
  301. #ifdef CYASSL_SHA384
  302. #define BUILD_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384
  303. #ifdef HAVE_NULL_CIPHER
  304. #define BUILD_TLS_DHE_PSK_WITH_NULL_SHA384
  305. #endif
  306. #ifdef HAVE_AESGCM
  307. #define BUILD_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384
  308. #endif
  309. #endif
  310. #endif
  311. #if defined(HAVE_ECC) && !defined(NO_TLS)
  312. #if !defined(NO_AES)
  313. #if !defined(NO_SHA)
  314. #if !defined(NO_RSA)
  315. #define BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
  316. #define BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
  317. #define BUILD_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
  318. #define BUILD_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
  319. #endif
  320. #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
  321. #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
  322. #define BUILD_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
  323. #define BUILD_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
  324. #endif /* NO_SHA */
  325. #ifndef NO_SHA256
  326. #if !defined(NO_RSA)
  327. #define BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
  328. #define BUILD_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
  329. #endif
  330. #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
  331. #define BUILD_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
  332. #endif
  333. #ifdef CYASSL_SHA384
  334. #if !defined(NO_RSA)
  335. #define BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
  336. #define BUILD_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
  337. #endif
  338. #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
  339. #define BUILD_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
  340. #endif
  341. #if defined (HAVE_AESGCM)
  342. #if !defined(NO_RSA)
  343. #define BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  344. #define BUILD_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
  345. #if defined(CYASSL_SHA384)
  346. #define BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
  347. #define BUILD_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
  348. #endif
  349. #endif
  350. #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
  351. #define BUILD_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
  352. #if defined(CYASSL_SHA384)
  353. #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
  354. #define BUILD_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
  355. #endif
  356. #endif
  357. #if defined (HAVE_AESCCM)
  358. #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
  359. #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8
  360. #endif
  361. #endif /* NO_AES */
  362. #if !defined(NO_RC4)
  363. #if !defined(NO_SHA)
  364. #if !defined(NO_RSA)
  365. #define BUILD_TLS_ECDHE_RSA_WITH_RC4_128_SHA
  366. #define BUILD_TLS_ECDH_RSA_WITH_RC4_128_SHA
  367. #endif
  368. #define BUILD_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
  369. #define BUILD_TLS_ECDH_ECDSA_WITH_RC4_128_SHA
  370. #endif
  371. #endif
  372. #if !defined(NO_DES3)
  373. #if !defined(NO_RSA)
  374. #define BUILD_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
  375. #define BUILD_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
  376. #endif
  377. #define BUILD_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
  378. #define BUILD_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
  379. #endif
  380. #endif
  381. #if defined(BUILD_SSL_RSA_WITH_RC4_128_SHA) || \
  382. defined(BUILD_SSL_RSA_WITH_RC4_128_MD5)
  383. #define BUILD_ARC4
  384. #endif
  385. #if defined(BUILD_SSL_RSA_WITH_3DES_EDE_CBC_SHA)
  386. #define BUILD_DES3
  387. #endif
  388. #if defined(BUILD_TLS_RSA_WITH_AES_128_CBC_SHA) || \
  389. defined(BUILD_TLS_RSA_WITH_AES_256_CBC_SHA) || \
  390. defined(BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
  391. #undef BUILD_AES
  392. #define BUILD_AES
  393. #endif
  394. #if defined(BUILD_TLS_RSA_WITH_AES_128_GCM_SHA256) || \
  395. defined(BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
  396. #define BUILD_AESGCM
  397. #endif
  398. #if defined(BUILD_TLS_RSA_WITH_HC_128_SHA) || \
  399. defined(BUILD_TLS_RSA_WITH_HC_128_MD5) || \
  400. defined(BUILD_TLS_RSA_WITH_HC_128_B2B256)
  401. #define BUILD_HC128
  402. #endif
  403. #if defined(BUILD_TLS_RSA_WITH_RABBIT_SHA)
  404. #define BUILD_RABBIT
  405. #endif
  406. #ifdef NO_DES3
  407. #define DES_BLOCK_SIZE 8
  408. #else
  409. #undef BUILD_DES3
  410. #define BUILD_DES3
  411. #endif
  412. #ifdef NO_AES
  413. #define AES_BLOCK_SIZE 16
  414. #else
  415. #undef BUILD_AES
  416. #define BUILD_AES
  417. #endif
  418. #ifndef NO_RC4
  419. #undef BUILD_ARC4
  420. #define BUILD_ARC4
  421. #endif
  422. #ifdef HAVE_CHACHA
  423. #define CHACHA20_BLOCK_SIZE 16
  424. /* ChaCha - Poly AEAD suites */
  425. #if defined(HAVE_POLY1305) && !defined(NO_SHA256)
  426. #if defined(HAVE_ECC)
  427. #if !defined(NO_RSA)
  428. #define BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
  429. #endif
  430. #define BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
  431. #endif
  432. #if !defined(NO_DH) && !defined(NO_RSA)
  433. #define BUILD_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256
  434. #endif
  435. #endif /* end of ChaCha - Poly AEAD suites */
  436. #endif
  437. #if defined(BUILD_AESGCM) || defined(HAVE_AESCCM) || defined(HAVE_CHACHA)
  438. #define HAVE_AEAD
  439. #endif
  440. /* actual cipher values, 2nd byte */
  441. enum {
  442. TLS_DHE_RSA_WITH_AES_256_CBC_SHA = 0x39,
  443. TLS_DHE_RSA_WITH_AES_128_CBC_SHA = 0x33,
  444. TLS_DH_anon_WITH_AES_128_CBC_SHA = 0x34,
  445. TLS_RSA_WITH_AES_256_CBC_SHA = 0x35,
  446. TLS_RSA_WITH_AES_128_CBC_SHA = 0x2F,
  447. TLS_RSA_WITH_NULL_SHA = 0x02,
  448. TLS_PSK_WITH_AES_256_CBC_SHA = 0x8d,
  449. TLS_PSK_WITH_AES_128_CBC_SHA256 = 0xae,
  450. TLS_PSK_WITH_AES_256_CBC_SHA384 = 0xaf,
  451. TLS_PSK_WITH_AES_128_CBC_SHA = 0x8c,
  452. TLS_PSK_WITH_NULL_SHA256 = 0xb0,
  453. TLS_PSK_WITH_NULL_SHA384 = 0xb1,
  454. TLS_PSK_WITH_NULL_SHA = 0x2c,
  455. SSL_RSA_WITH_RC4_128_SHA = 0x05,
  456. SSL_RSA_WITH_RC4_128_MD5 = 0x04,
  457. SSL_RSA_WITH_3DES_EDE_CBC_SHA = 0x0A,
  458. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  459. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA = 0x14,
  460. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA = 0x13,
  461. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA = 0x0A,
  462. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA = 0x09,
  463. TLS_ECDHE_RSA_WITH_RC4_128_SHA = 0x11,
  464. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA = 0x07,
  465. TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA = 0x12,
  466. TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA = 0x08,
  467. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 = 0x27,
  468. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 = 0x23,
  469. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 = 0x28,
  470. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 = 0x24,
  471. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  472. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA = 0x0F,
  473. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA = 0x0E,
  474. TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA = 0x05,
  475. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA = 0x04,
  476. TLS_ECDH_RSA_WITH_RC4_128_SHA = 0x0C,
  477. TLS_ECDH_ECDSA_WITH_RC4_128_SHA = 0x02,
  478. TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA = 0x0D,
  479. TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA = 0x03,
  480. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 = 0x29,
  481. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 = 0x25,
  482. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 = 0x2A,
  483. TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 = 0x26,
  484. /* CyaSSL extension - eSTREAM */
  485. TLS_RSA_WITH_HC_128_MD5 = 0xFB,
  486. TLS_RSA_WITH_HC_128_SHA = 0xFC,
  487. TLS_RSA_WITH_RABBIT_SHA = 0xFD,
  488. /* CyaSSL extension - Blake2b 256 */
  489. TLS_RSA_WITH_AES_128_CBC_B2B256 = 0xF8,
  490. TLS_RSA_WITH_AES_256_CBC_B2B256 = 0xF9,
  491. TLS_RSA_WITH_HC_128_B2B256 = 0xFA, /* eSTREAM too */
  492. /* CyaSSL extension - NTRU */
  493. TLS_NTRU_RSA_WITH_RC4_128_SHA = 0xe5,
  494. TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA = 0xe6,
  495. TLS_NTRU_RSA_WITH_AES_128_CBC_SHA = 0xe7, /* clashes w/official SHA-256 */
  496. TLS_NTRU_RSA_WITH_AES_256_CBC_SHA = 0xe8,
  497. /* SHA256 */
  498. TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 = 0x6b,
  499. TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 = 0x67,
  500. TLS_RSA_WITH_AES_256_CBC_SHA256 = 0x3d,
  501. TLS_RSA_WITH_AES_128_CBC_SHA256 = 0x3c,
  502. TLS_RSA_WITH_NULL_SHA256 = 0x3b,
  503. TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 = 0xb2,
  504. TLS_DHE_PSK_WITH_NULL_SHA256 = 0xb4,
  505. /* SHA384 */
  506. TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 = 0xb3,
  507. TLS_DHE_PSK_WITH_NULL_SHA384 = 0xb5,
  508. /* AES-GCM */
  509. TLS_RSA_WITH_AES_128_GCM_SHA256 = 0x9c,
  510. TLS_RSA_WITH_AES_256_GCM_SHA384 = 0x9d,
  511. TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 = 0x9e,
  512. TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 = 0x9f,
  513. TLS_PSK_WITH_AES_128_GCM_SHA256 = 0xa8,
  514. TLS_PSK_WITH_AES_256_GCM_SHA384 = 0xa9,
  515. TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 = 0xaa,
  516. TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 = 0xab,
  517. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  518. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 = 0x2b,
  519. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 = 0x2c,
  520. TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 = 0x2d,
  521. TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 = 0x2e,
  522. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 = 0x2f,
  523. TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 = 0x30,
  524. TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 = 0x31,
  525. TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 = 0x32,
  526. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  527. * also, in some of the other AES-CCM suites
  528. * there will be second byte number conflicts
  529. * with non-ECC AES-GCM */
  530. TLS_RSA_WITH_AES_128_CCM_8 = 0xa0,
  531. TLS_RSA_WITH_AES_256_CCM_8 = 0xa1,
  532. TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 = 0xae,
  533. TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 = 0xaf,
  534. TLS_PSK_WITH_AES_128_CCM = 0xa4,
  535. TLS_PSK_WITH_AES_256_CCM = 0xa5,
  536. TLS_PSK_WITH_AES_128_CCM_8 = 0xa8,
  537. TLS_PSK_WITH_AES_256_CCM_8 = 0xa9,
  538. TLS_DHE_PSK_WITH_AES_128_CCM = 0xa6,
  539. TLS_DHE_PSK_WITH_AES_256_CCM = 0xa7,
  540. /* Camellia */
  541. TLS_RSA_WITH_CAMELLIA_128_CBC_SHA = 0x41,
  542. TLS_RSA_WITH_CAMELLIA_256_CBC_SHA = 0x84,
  543. TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 = 0xba,
  544. TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 = 0xc0,
  545. TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA = 0x45,
  546. TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA = 0x88,
  547. TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 = 0xbe,
  548. TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 = 0xc4,
  549. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 = 0x13,
  550. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 = 0x14,
  551. TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 = 0x15,
  552. /* Renegotiation Indication Extension Special Suite */
  553. TLS_EMPTY_RENEGOTIATION_INFO_SCSV = 0xff
  554. };
  555. #ifndef CYASSL_SESSION_TIMEOUT
  556. #define CYASSL_SESSION_TIMEOUT 500
  557. /* default session resumption cache timeout in seconds */
  558. #endif
  559. enum Misc {
  560. ECC_BYTE = 0xC0, /* ECC first cipher suite byte */
  561. CHACHA_BYTE = 0xCC, /* ChaCha first cipher suite */
  562. SEND_CERT = 1,
  563. SEND_BLANK_CERT = 2,
  564. DTLS_MAJOR = 0xfe, /* DTLS major version number */
  565. DTLS_MINOR = 0xff, /* DTLS minor version number */
  566. DTLSv1_2_MINOR = 0xfd, /* DTLS minor version number */
  567. SSLv3_MAJOR = 3, /* SSLv3 and TLSv1+ major version number */
  568. SSLv3_MINOR = 0, /* TLSv1 minor version number */
  569. TLSv1_MINOR = 1, /* TLSv1 minor version number */
  570. TLSv1_1_MINOR = 2, /* TLSv1_1 minor version number */
  571. TLSv1_2_MINOR = 3, /* TLSv1_2 minor version number */
  572. OLD_HELLO_ID = 0x01, /* SSLv2 Client Hello Indicator */
  573. INVALID_BYTE = 0xff, /* Used to initialize cipher specs values */
  574. NO_COMPRESSION = 0,
  575. ZLIB_COMPRESSION = 221, /* CyaSSL zlib compression */
  576. HELLO_EXT_SIG_ALGO = 13, /* ID for the sig_algo hello extension */
  577. SECRET_LEN = 48, /* pre RSA and all master */
  578. ENCRYPT_LEN = 512, /* allow 4096 bit static buffer */
  579. SIZEOF_SENDER = 4, /* clnt or srvr */
  580. FINISHED_SZ = 36, /* MD5_DIGEST_SIZE + SHA_DIGEST_SIZE */
  581. MAX_RECORD_SIZE = 16384, /* 2^14, max size by standard */
  582. MAX_MSG_EXTRA = 38 + MAX_DIGEST_SIZE,
  583. /* max added to msg, mac + pad from */
  584. /* RECORD_HEADER_SZ + BLOCK_SZ (pad) + Max
  585. digest sz + BLOC_SZ (iv) + pad byte (1) */
  586. MAX_COMP_EXTRA = 1024, /* max compression extra */
  587. MAX_MTU = 1500, /* max expected MTU */
  588. MAX_UDP_SIZE = 8192 - 100, /* was MAX_MTU - 100 */
  589. MAX_DH_SZ = 1036, /* 4096 p, pub, g + 2 byte size for each */
  590. MAX_STR_VERSION = 8, /* string rep of protocol version */
  591. PAD_MD5 = 48, /* pad length for finished */
  592. PAD_SHA = 40, /* pad length for finished */
  593. MAX_PAD_SIZE = 256, /* maximum length of padding */
  594. COMPRESS_DUMMY_SIZE = 64, /* compression dummy round size */
  595. COMPRESS_CONSTANT = 13, /* compression calc constant */
  596. COMPRESS_UPPER = 55, /* compression calc numerator */
  597. COMPRESS_LOWER = 64, /* compression calc denominator */
  598. PEM_LINE_LEN = 80, /* PEM line max + fudge */
  599. LENGTH_SZ = 2, /* length field for HMAC, data only */
  600. VERSION_SZ = 2, /* length of proctocol version */
  601. SEQ_SZ = 8, /* 64 bit sequence number */
  602. BYTE3_LEN = 3, /* up to 24 bit byte lengths */
  603. ALERT_SIZE = 2, /* level + description */
  604. VERIFY_HEADER = 2, /* always use 2 bytes */
  605. EXT_ID_SZ = 2, /* always use 2 bytes */
  606. MAX_DH_SIZE = 513, /* 4096 bit plus possible leading 0 */
  607. MAX_SUITE_SZ = 200, /* 100 suites for now! */
  608. RAN_LEN = 32, /* random length */
  609. SEED_LEN = RAN_LEN * 2, /* tls prf seed length */
  610. ID_LEN = 32, /* session id length */
  611. MAX_COOKIE_LEN = 32, /* max dtls cookie size */
  612. COOKIE_SZ = 20, /* use a 20 byte cookie */
  613. SUITE_LEN = 2, /* cipher suite sz length */
  614. ENUM_LEN = 1, /* always a byte */
  615. OPAQUE8_LEN = 1, /* 1 byte */
  616. OPAQUE16_LEN = 2, /* 2 bytes */
  617. OPAQUE24_LEN = 3, /* 3 bytes */
  618. OPAQUE32_LEN = 4, /* 4 bytes */
  619. COMP_LEN = 1, /* compression length */
  620. CURVE_LEN = 2, /* ecc named curve length */
  621. SERVER_ID_LEN = 20, /* server session id length */
  622. HANDSHAKE_HEADER_SZ = 4, /* type + length(3) */
  623. RECORD_HEADER_SZ = 5, /* type + version + len(2) */
  624. CERT_HEADER_SZ = 3, /* always 3 bytes */
  625. REQ_HEADER_SZ = 2, /* cert request header sz */
  626. HINT_LEN_SZ = 2, /* length of hint size field */
  627. TRUNCATED_HMAC_SZ = 10, /* length of hmac w/ truncated hmac extension */
  628. HELLO_EXT_TYPE_SZ = 2, /* length of a hello extension type */
  629. HELLO_EXT_SZ = 8, /* total length of the lazy hello extensions */
  630. HELLO_EXT_LEN = 6, /* length of the lazy hello extensions */
  631. HELLO_EXT_SIGALGO_SZ = 2, /* length of signature algo extension */
  632. HELLO_EXT_SIGALGO_MAX = 32, /* number of items in the signature algo list */
  633. DTLS_HANDSHAKE_HEADER_SZ = 12, /* normal + seq(2) + offset(3) + length(3) */
  634. DTLS_RECORD_HEADER_SZ = 13, /* normal + epoch(2) + seq_num(6) */
  635. DTLS_HANDSHAKE_EXTRA = 8, /* diff from normal */
  636. DTLS_RECORD_EXTRA = 8, /* diff from normal */
  637. DTLS_HANDSHAKE_SEQ_SZ = 2, /* handshake header sequence number */
  638. DTLS_HANDSHAKE_FRAG_SZ = 3, /* fragment offset and length are 24 bit */
  639. DTLS_POOL_SZ = 5, /* buffers to hold in the retry pool */
  640. FINISHED_LABEL_SZ = 15, /* TLS finished label size */
  641. TLS_FINISHED_SZ = 12, /* TLS has a shorter size */
  642. MASTER_LABEL_SZ = 13, /* TLS master secret label sz */
  643. KEY_LABEL_SZ = 13, /* TLS key block expansion sz */
  644. MAX_PRF_HALF = 256, /* Maximum half secret len */
  645. MAX_PRF_LABSEED = 128, /* Maximum label + seed len */
  646. MAX_PRF_DIG = 224, /* Maximum digest len */
  647. MAX_REQUEST_SZ = 256, /* Maximum cert req len (no auth yet */
  648. SESSION_FLUSH_COUNT = 256, /* Flush session cache unless user turns off */
  649. RC4_KEY_SIZE = 16, /* always 128bit */
  650. DES_KEY_SIZE = 8, /* des */
  651. DES3_KEY_SIZE = 24, /* 3 des ede */
  652. DES_IV_SIZE = DES_BLOCK_SIZE,
  653. AES_256_KEY_SIZE = 32, /* for 256 bit */
  654. AES_192_KEY_SIZE = 24, /* for 192 bit */
  655. AES_IV_SIZE = 16, /* always block size */
  656. AES_128_KEY_SIZE = 16, /* for 128 bit */
  657. AEAD_SEQ_OFFSET = 4, /* Auth Data: Sequence number */
  658. AEAD_TYPE_OFFSET = 8, /* Auth Data: Type */
  659. AEAD_VMAJ_OFFSET = 9, /* Auth Data: Major Version */
  660. AEAD_VMIN_OFFSET = 10, /* Auth Data: Minor Version */
  661. AEAD_LEN_OFFSET = 11, /* Auth Data: Length */
  662. AEAD_AUTH_DATA_SZ = 13, /* Size of the data to authenticate */
  663. AEAD_IMP_IV_SZ = 4, /* Size of the implicit IV */
  664. AEAD_EXP_IV_SZ = 8, /* Size of the explicit IV */
  665. AEAD_NONCE_SZ = AEAD_EXP_IV_SZ + AEAD_IMP_IV_SZ,
  666. AES_GCM_AUTH_SZ = 16, /* AES-GCM Auth Tag length */
  667. AES_CCM_16_AUTH_SZ = 16, /* AES-CCM-16 Auth Tag length */
  668. AES_CCM_8_AUTH_SZ = 8, /* AES-CCM-8 Auth Tag Length */
  669. CAMELLIA_128_KEY_SIZE = 16, /* for 128 bit */
  670. CAMELLIA_192_KEY_SIZE = 24, /* for 192 bit */
  671. CAMELLIA_256_KEY_SIZE = 32, /* for 256 bit */
  672. CAMELLIA_IV_SIZE = 16, /* always block size */
  673. CHACHA20_256_KEY_SIZE = 32, /* for 256 bit */
  674. CHACHA20_128_KEY_SIZE = 16, /* for 128 bit */
  675. CHACHA20_IV_SIZE = 8, /* 64 bits for iv */
  676. POLY1305_AUTH_SZ = 16, /* 128 bits */
  677. HC_128_KEY_SIZE = 16, /* 128 bits */
  678. HC_128_IV_SIZE = 16, /* also 128 bits */
  679. RABBIT_KEY_SIZE = 16, /* 128 bits */
  680. RABBIT_IV_SIZE = 8, /* 64 bits for iv */
  681. EVP_SALT_SIZE = 8, /* evp salt size 64 bits */
  682. ECDHE_SIZE = 32, /* ECHDE server size defaults to 256 bit */
  683. MAX_EXPORT_ECC_SZ = 256, /* Export ANS X9.62 max future size */
  684. MAX_HELLO_SZ = 128, /* max client or server hello */
  685. MAX_CERT_VERIFY_SZ = 1024, /* max */
  686. CLIENT_HELLO_FIRST = 35, /* Protocol + RAN_LEN + sizeof(id_len) */
  687. MAX_SUITE_NAME = 48, /* maximum length of cipher suite string */
  688. DTLS_TIMEOUT_INIT = 1, /* default timeout init for DTLS receive */
  689. DTLS_TIMEOUT_MAX = 64, /* default max timeout for DTLS receive */
  690. DTLS_TIMEOUT_MULTIPLIER = 2, /* default timeout multiplier for DTLS recv */
  691. MAX_PSK_ID_LEN = 128, /* max psk identity/hint supported */
  692. MAX_PSK_KEY_LEN = 64, /* max psk key supported */
  693. MAX_CYASSL_FILE_SIZE = 1024 * 1024 * 4, /* 4 mb file size alloc limit */
  694. #ifdef FORTRESS
  695. MAX_EX_DATA = 3, /* allow for three items of ex_data */
  696. #endif
  697. MAX_X509_SIZE = 2048, /* max static x509 buffer size */
  698. CERT_MIN_SIZE = 256, /* min PEM cert size with header/footer */
  699. MAX_FILENAME_SZ = 256, /* max file name length */
  700. FILE_BUFFER_SIZE = 1024, /* default static file buffer size for input,
  701. will use dynamic buffer if not big enough */
  702. MAX_NTRU_PUB_KEY_SZ = 1027, /* NTRU max for now */
  703. MAX_NTRU_ENCRYPT_SZ = 1027, /* NTRU max for now */
  704. MAX_NTRU_BITS = 256, /* max symmetric bit strength */
  705. NO_SNIFF = 0, /* not sniffing */
  706. SNIFF = 1, /* currently sniffing */
  707. HASH_SIG_SIZE = 2, /* default SHA1 RSA */
  708. NO_CAVIUM_DEVICE = -2, /* invalid cavium device id */
  709. NO_COPY = 0, /* should we copy static buffer for write */
  710. COPY = 1 /* should we copy static buffer for write */
  711. };
  712. #ifdef SESSION_INDEX
  713. /* Shift values for making a session index */
  714. #define SESSIDX_ROW_SHIFT 4
  715. #define SESSIDX_IDX_MASK 0x0F
  716. #endif
  717. /* max cert chain peer depth */
  718. #ifndef MAX_CHAIN_DEPTH
  719. #define MAX_CHAIN_DEPTH 9
  720. #endif
  721. #ifndef SESSION_TICKET_LEN
  722. #define SESSION_TICKET_LEN 256
  723. #endif
  724. /* don't use extra 3/4k stack space unless need to */
  725. #ifdef HAVE_NTRU
  726. #define MAX_ENCRYPT_SZ MAX_NTRU_ENCRYPT_SZ
  727. #else
  728. #define MAX_ENCRYPT_SZ ENCRYPT_LEN
  729. #endif
  730. /* states */
  731. enum states {
  732. NULL_STATE = 0,
  733. SERVER_HELLOVERIFYREQUEST_COMPLETE,
  734. SERVER_HELLO_COMPLETE,
  735. SERVER_CERT_COMPLETE,
  736. SERVER_KEYEXCHANGE_COMPLETE,
  737. SERVER_HELLODONE_COMPLETE,
  738. SERVER_FINISHED_COMPLETE,
  739. CLIENT_HELLO_COMPLETE,
  740. CLIENT_KEYEXCHANGE_COMPLETE,
  741. CLIENT_FINISHED_COMPLETE,
  742. HANDSHAKE_DONE
  743. };
  744. #if defined(__GNUC__)
  745. #define CYASSL_PACK __attribute__ ((packed))
  746. #else
  747. #define CYASSL_PACK
  748. #endif
  749. /* SSL Version */
  750. typedef struct ProtocolVersion {
  751. byte major;
  752. byte minor;
  753. } CYASSL_PACK ProtocolVersion;
  754. CYASSL_LOCAL ProtocolVersion MakeSSLv3(void);
  755. CYASSL_LOCAL ProtocolVersion MakeTLSv1(void);
  756. CYASSL_LOCAL ProtocolVersion MakeTLSv1_1(void);
  757. CYASSL_LOCAL ProtocolVersion MakeTLSv1_2(void);
  758. #ifdef CYASSL_DTLS
  759. CYASSL_LOCAL ProtocolVersion MakeDTLSv1(void);
  760. CYASSL_LOCAL ProtocolVersion MakeDTLSv1_2(void);
  761. #endif
  762. enum BIO_TYPE {
  763. BIO_BUFFER = 1,
  764. BIO_SOCKET = 2,
  765. BIO_SSL = 3,
  766. BIO_MEMORY = 4
  767. };
  768. /* CyaSSL BIO_METHOD type */
  769. struct CYASSL_BIO_METHOD {
  770. byte type; /* method type */
  771. };
  772. /* CyaSSL BIO type */
  773. struct CYASSL_BIO {
  774. byte type; /* method type */
  775. byte close; /* close flag */
  776. byte eof; /* eof flag */
  777. CYASSL* ssl; /* possible associated ssl */
  778. byte* mem; /* memory buffer */
  779. int memLen; /* memory buffer length */
  780. int fd; /* possible file descriptor */
  781. CYASSL_BIO* prev; /* previous in chain */
  782. CYASSL_BIO* next; /* next in chain */
  783. };
  784. /* CyaSSL method type */
  785. struct CYASSL_METHOD {
  786. ProtocolVersion version;
  787. byte side; /* connection side, server or client */
  788. byte downgrade; /* whether to downgrade version, default no */
  789. };
  790. /* defautls to client */
  791. CYASSL_LOCAL void InitSSL_Method(CYASSL_METHOD*, ProtocolVersion);
  792. /* for sniffer */
  793. CYASSL_LOCAL int DoFinished(CYASSL* ssl, const byte* input, word32* inOutIdx,
  794. word32 size, word32 totalSz, int sniff);
  795. CYASSL_LOCAL int DoApplicationData(CYASSL* ssl, byte* input, word32* inOutIdx);
  796. /* CyaSSL buffer type */
  797. typedef struct buffer {
  798. word32 length;
  799. byte* buffer;
  800. } buffer;
  801. enum {
  802. FORCED_FREE = 1,
  803. NO_FORCED_FREE = 0
  804. };
  805. /* only use compression extra if using compression */
  806. #ifdef HAVE_LIBZ
  807. #define COMP_EXTRA MAX_COMP_EXTRA
  808. #else
  809. #define COMP_EXTRA 0
  810. #endif
  811. /* only the sniffer needs space in the buffer for extra MTU record(s) */
  812. #ifdef CYASSL_SNIFFER
  813. #define MTU_EXTRA MAX_MTU * 3
  814. #else
  815. #define MTU_EXTRA 0
  816. #endif
  817. /* embedded callbacks require large static buffers, make sure on */
  818. #ifdef CYASSL_CALLBACKS
  819. #undef LARGE_STATIC_BUFFERS
  820. #define LARGE_STATIC_BUFFERS
  821. #endif
  822. /* give user option to use 16K static buffers */
  823. #if defined(LARGE_STATIC_BUFFERS)
  824. #define RECORD_SIZE MAX_RECORD_SIZE
  825. #else
  826. #ifdef CYASSL_DTLS
  827. #define RECORD_SIZE MAX_MTU
  828. #else
  829. #define RECORD_SIZE 128
  830. #endif
  831. #endif
  832. /* user option to turn off 16K output option */
  833. /* if using small static buffers (default) and SSL_write tries to write data
  834. larger than the record we have, dynamically get it, unless user says only
  835. write in static buffer chuncks */
  836. #ifndef STATIC_CHUNKS_ONLY
  837. #define OUTPUT_RECORD_SIZE MAX_RECORD_SIZE
  838. #else
  839. #define OUTPUT_RECORD_SIZE RECORD_SIZE
  840. #endif
  841. /* CyaSSL input buffer
  842. RFC 2246:
  843. length
  844. The length (in bytes) of the following TLSPlaintext.fragment.
  845. The length should not exceed 2^14.
  846. */
  847. #if defined(LARGE_STATIC_BUFFERS)
  848. #define STATIC_BUFFER_LEN RECORD_HEADER_SZ + RECORD_SIZE + COMP_EXTRA + \
  849. MTU_EXTRA + MAX_MSG_EXTRA
  850. #else
  851. /* don't fragment memory from the record header */
  852. #define STATIC_BUFFER_LEN RECORD_HEADER_SZ
  853. #endif
  854. typedef struct {
  855. word32 length; /* total buffer length used */
  856. word32 idx; /* idx to part of length already consumed */
  857. byte* buffer; /* place holder for static or dynamic buffer */
  858. word32 bufferSize; /* current buffer size */
  859. ALIGN16 byte staticBuffer[STATIC_BUFFER_LEN];
  860. byte dynamicFlag; /* dynamic memory currently in use */
  861. byte offset; /* alignment offset attempt */
  862. } bufferStatic;
  863. /* Cipher Suites holder */
  864. typedef struct Suites {
  865. int setSuites; /* user set suites from default */
  866. byte suites[MAX_SUITE_SZ];
  867. word16 suiteSz; /* suite length in bytes */
  868. byte hashSigAlgo[HELLO_EXT_SIGALGO_MAX]; /* sig/algo to offer */
  869. word16 hashSigAlgoSz; /* SigAlgo extension length in bytes */
  870. byte hashAlgo; /* selected hash algorithm */
  871. byte sigAlgo; /* selected sig algorithm */
  872. } Suites;
  873. CYASSL_LOCAL
  874. void InitSuites(Suites*, ProtocolVersion,
  875. byte, byte, byte, byte, byte, byte, int);
  876. CYASSL_LOCAL
  877. int SetCipherList(Suites*, const char* list);
  878. #ifndef PSK_TYPES_DEFINED
  879. typedef unsigned int (*psk_client_callback)(CYASSL*, const char*, char*,
  880. unsigned int, unsigned char*, unsigned int);
  881. typedef unsigned int (*psk_server_callback)(CYASSL*, const char*,
  882. unsigned char*, unsigned int);
  883. #endif /* PSK_TYPES_DEFINED */
  884. #ifdef HAVE_NETX
  885. CYASSL_LOCAL int NetX_Receive(CYASSL *ssl, char *buf, int sz, void *ctx);
  886. CYASSL_LOCAL int NetX_Send(CYASSL *ssl, char *buf, int sz, void *ctx);
  887. #endif /* HAVE_NETX */
  888. /* CyaSSL Cipher type just points back to SSL */
  889. struct CYASSL_CIPHER {
  890. CYASSL* ssl;
  891. };
  892. typedef struct OCSP_Entry OCSP_Entry;
  893. #ifdef SHA_DIGEST_SIZE
  894. #define OCSP_DIGEST_SIZE SHA_DIGEST_SIZE
  895. #else
  896. #define OCSP_DIGEST_SIZE 160
  897. #endif
  898. #ifdef NO_ASN
  899. /* no_asn won't have */
  900. typedef struct CertStatus CertStatus;
  901. #endif
  902. struct OCSP_Entry {
  903. OCSP_Entry* next; /* next entry */
  904. byte issuerHash[OCSP_DIGEST_SIZE]; /* issuer hash */
  905. byte issuerKeyHash[OCSP_DIGEST_SIZE]; /* issuer public key hash */
  906. CertStatus* status; /* OCSP response list */
  907. int totalStatus; /* number on list */
  908. };
  909. #ifndef HAVE_OCSP
  910. typedef struct CYASSL_OCSP CYASSL_OCSP;
  911. #endif
  912. /* CyaSSL OCSP controller */
  913. struct CYASSL_OCSP {
  914. CYASSL_CERT_MANAGER* cm; /* pointer back to cert manager */
  915. OCSP_Entry* ocspList; /* OCSP response list */
  916. CyaSSL_Mutex ocspLock; /* OCSP list lock */
  917. };
  918. #ifndef MAX_DATE_SIZE
  919. #define MAX_DATE_SIZE 32
  920. #endif
  921. typedef struct CRL_Entry CRL_Entry;
  922. #ifdef SHA_DIGEST_SIZE
  923. #define CRL_DIGEST_SIZE SHA_DIGEST_SIZE
  924. #else
  925. #define CRL_DIGEST_SIZE 160
  926. #endif
  927. #ifdef NO_ASN
  928. typedef struct RevokedCert RevokedCert;
  929. #endif
  930. /* Complete CRL */
  931. struct CRL_Entry {
  932. CRL_Entry* next; /* next entry */
  933. byte issuerHash[CRL_DIGEST_SIZE]; /* issuer hash */
  934. /* byte crlHash[CRL_DIGEST_SIZE]; raw crl data hash */
  935. /* restore the hash here if needed for optimized comparisons */
  936. byte lastDate[MAX_DATE_SIZE]; /* last date updated */
  937. byte nextDate[MAX_DATE_SIZE]; /* next update date */
  938. byte lastDateFormat; /* last date format */
  939. byte nextDateFormat; /* next date format */
  940. RevokedCert* certs; /* revoked cert list */
  941. int totalCerts; /* number on list */
  942. };
  943. typedef struct CRL_Monitor CRL_Monitor;
  944. /* CRL directory monitor */
  945. struct CRL_Monitor {
  946. char* path; /* full dir path, if valid pointer we're using */
  947. int type; /* PEM or ASN1 type */
  948. };
  949. #ifndef HAVE_CRL
  950. typedef struct CYASSL_CRL CYASSL_CRL;
  951. #endif
  952. /* CyaSSL CRL controller */
  953. struct CYASSL_CRL {
  954. CYASSL_CERT_MANAGER* cm; /* pointer back to cert manager */
  955. CRL_Entry* crlList; /* our CRL list */
  956. CyaSSL_Mutex crlLock; /* CRL list lock */
  957. CRL_Monitor monitors[2]; /* PEM and DER possible */
  958. #ifdef HAVE_CRL_MONITOR
  959. pthread_t tid; /* monitoring thread */
  960. int mfd; /* monitor fd, -1 if no init yet */
  961. #endif
  962. };
  963. #ifdef NO_ASN
  964. typedef struct Signer Signer;
  965. #endif
  966. #ifndef CA_TABLE_SIZE
  967. #define CA_TABLE_SIZE 11
  968. #endif
  969. /* CyaSSL Certificate Manager */
  970. struct CYASSL_CERT_MANAGER {
  971. Signer* caTable[CA_TABLE_SIZE]; /* the CA signer table */
  972. CyaSSL_Mutex caLock; /* CA list lock */
  973. CallbackCACache caCacheCallback; /* CA cache addition callback */
  974. void* heap; /* heap helper */
  975. CYASSL_CRL* crl; /* CRL checker */
  976. byte crlEnabled; /* is CRL on ? */
  977. byte crlCheckAll; /* always leaf, but all ? */
  978. CbMissingCRL cbMissingCRL; /* notify through cb of missing crl */
  979. CYASSL_OCSP* ocsp; /* OCSP checker */
  980. byte ocspEnabled; /* is OCSP on ? */
  981. byte ocspSendNonce; /* send the OCSP nonce ? */
  982. byte ocspUseOverrideURL; /* ignore cert's responder, override */
  983. char* ocspOverrideURL; /* use this responder */
  984. void* ocspIOCtx; /* I/O callback CTX */
  985. CbOCSPIO ocspIOCb; /* I/O callback for OCSP lookup */
  986. CbOCSPRespFree ocspRespFreeCb; /* Frees OCSP Response from IO Cb */
  987. };
  988. CYASSL_LOCAL int CM_SaveCertCache(CYASSL_CERT_MANAGER*, const char*);
  989. CYASSL_LOCAL int CM_RestoreCertCache(CYASSL_CERT_MANAGER*, const char*);
  990. CYASSL_LOCAL int CM_MemSaveCertCache(CYASSL_CERT_MANAGER*, void*, int, int*);
  991. CYASSL_LOCAL int CM_MemRestoreCertCache(CYASSL_CERT_MANAGER*, const void*, int);
  992. CYASSL_LOCAL int CM_GetCertCacheMemSize(CYASSL_CERT_MANAGER*);
  993. /* CyaSSL Sock Addr */
  994. struct CYASSL_SOCKADDR {
  995. unsigned int sz; /* sockaddr size */
  996. void* sa; /* pointer to the sockaddr_in or sockaddr_in6 */
  997. };
  998. typedef struct CYASSL_DTLS_CTX {
  999. CYASSL_SOCKADDR peer;
  1000. int fd;
  1001. } CYASSL_DTLS_CTX;
  1002. #ifdef CYASSL_DTLS
  1003. #ifdef WORD64_AVAILABLE
  1004. typedef word64 DtlsSeq;
  1005. #else
  1006. typedef word32 DtlsSeq;
  1007. #endif
  1008. #define DTLS_SEQ_BITS (sizeof(DtlsSeq) * CHAR_BIT)
  1009. typedef struct DtlsState {
  1010. DtlsSeq window; /* Sliding window for current epoch */
  1011. word16 nextEpoch; /* Expected epoch in next record */
  1012. word32 nextSeq; /* Expected sequence in next record */
  1013. word16 curEpoch; /* Received epoch in current record */
  1014. word32 curSeq; /* Received sequence in current record */
  1015. DtlsSeq prevWindow; /* Sliding window for old epoch */
  1016. word32 prevSeq; /* Next sequence in allowed old epoch */
  1017. } DtlsState;
  1018. #endif /* CYASSL_DTLS */
  1019. /* keys and secrets */
  1020. typedef struct Keys {
  1021. byte client_write_MAC_secret[MAX_DIGEST_SIZE]; /* max sizes */
  1022. byte server_write_MAC_secret[MAX_DIGEST_SIZE];
  1023. byte client_write_key[AES_256_KEY_SIZE]; /* max sizes */
  1024. byte server_write_key[AES_256_KEY_SIZE];
  1025. byte client_write_IV[AES_IV_SIZE]; /* max sizes */
  1026. byte server_write_IV[AES_IV_SIZE];
  1027. #ifdef HAVE_AEAD
  1028. byte aead_exp_IV[AEAD_EXP_IV_SZ];
  1029. byte aead_enc_imp_IV[AEAD_IMP_IV_SZ];
  1030. byte aead_dec_imp_IV[AEAD_IMP_IV_SZ];
  1031. #endif
  1032. word32 peer_sequence_number;
  1033. word32 sequence_number;
  1034. #ifdef CYASSL_DTLS
  1035. DtlsState dtls_state; /* Peer's state */
  1036. word16 dtls_peer_handshake_number;
  1037. word16 dtls_expected_peer_handshake_number;
  1038. word16 dtls_epoch; /* Current tx epoch */
  1039. word32 dtls_sequence_number; /* Current tx sequence */
  1040. word16 dtls_handshake_number; /* Current tx handshake seq */
  1041. #endif
  1042. word32 encryptSz; /* last size of encrypted data */
  1043. word32 padSz; /* how much to advance after decrypt part */
  1044. byte encryptionOn; /* true after change cipher spec */
  1045. byte decryptedCur; /* only decrypt current record once */
  1046. } Keys;
  1047. /* RFC 6066 TLS Extensions */
  1048. #ifdef HAVE_TLS_EXTENSIONS
  1049. typedef enum {
  1050. SERVER_NAME_INDICATION = 0x0000,
  1051. MAX_FRAGMENT_LENGTH = 0x0001,
  1052. TRUNCATED_HMAC = 0x0004,
  1053. ELLIPTIC_CURVES = 0x000a,
  1054. SESSION_TICKET = 0x0023,
  1055. SECURE_RENEGOTIATION = 0xff01
  1056. } TLSX_Type;
  1057. typedef struct TLSX {
  1058. TLSX_Type type; /* Extension Type */
  1059. void* data; /* Extension Data */
  1060. byte resp; /* IsResponse Flag */
  1061. struct TLSX* next; /* List Behavior */
  1062. } TLSX;
  1063. CYASSL_LOCAL TLSX* TLSX_Find(TLSX* list, TLSX_Type type);
  1064. CYASSL_LOCAL void TLSX_FreeAll(TLSX* list);
  1065. CYASSL_LOCAL int TLSX_SupportExtensions(CYASSL* ssl);
  1066. #ifndef NO_CYASSL_CLIENT
  1067. CYASSL_LOCAL word16 TLSX_GetRequestSize(CYASSL* ssl);
  1068. CYASSL_LOCAL word16 TLSX_WriteRequest(CYASSL* ssl, byte* output);
  1069. #endif
  1070. #ifndef NO_CYASSL_SERVER
  1071. CYASSL_LOCAL word16 TLSX_GetResponseSize(CYASSL* ssl);
  1072. CYASSL_LOCAL word16 TLSX_WriteResponse(CYASSL* ssl, byte* output);
  1073. #endif
  1074. CYASSL_LOCAL int TLSX_Parse(CYASSL* ssl, byte* input, word16 length,
  1075. byte isRequest, Suites *suites);
  1076. #elif defined(HAVE_SNI) \
  1077. || defined(HAVE_MAX_FRAGMENT) \
  1078. || defined(HAVE_TRUNCATED_HMAC) \
  1079. || defined(HAVE_SUPPORTED_CURVES) \
  1080. || defined(HAVE_SECURE_RENEGOTIATION) \
  1081. || defined(HAVE_SESSION_TICKET)
  1082. #error Using TLS extensions requires HAVE_TLS_EXTENSIONS to be defined.
  1083. #endif /* HAVE_TLS_EXTENSIONS */
  1084. /* Server Name Indication */
  1085. #ifdef HAVE_SNI
  1086. typedef struct SNI {
  1087. byte type; /* SNI Type */
  1088. union { char* host_name; } data; /* SNI Data */
  1089. struct SNI* next; /* List Behavior */
  1090. #ifndef NO_CYASSL_SERVER
  1091. byte options; /* Behaviour options */
  1092. byte status; /* Matching result */
  1093. #endif
  1094. } SNI;
  1095. CYASSL_LOCAL int TLSX_UseSNI(TLSX** extensions, byte type, const void* data,
  1096. word16 size);
  1097. #ifndef NO_CYASSL_SERVER
  1098. CYASSL_LOCAL void TLSX_SNI_SetOptions(TLSX* extensions, byte type,
  1099. byte options);
  1100. CYASSL_LOCAL byte TLSX_SNI_Status(TLSX* extensions, byte type);
  1101. CYASSL_LOCAL word16 TLSX_SNI_GetRequest(TLSX* extensions, byte type,
  1102. void** data);
  1103. CYASSL_LOCAL int TLSX_SNI_GetFromBuffer(const byte* buffer, word32 bufferSz,
  1104. byte type, byte* sni, word32* inOutSz);
  1105. #endif
  1106. #endif /* HAVE_SNI */
  1107. /* Maximum Fragment Length */
  1108. #ifdef HAVE_MAX_FRAGMENT
  1109. CYASSL_LOCAL int TLSX_UseMaxFragment(TLSX** extensions, byte mfl);
  1110. #endif /* HAVE_MAX_FRAGMENT */
  1111. #ifdef HAVE_TRUNCATED_HMAC
  1112. CYASSL_LOCAL int TLSX_UseTruncatedHMAC(TLSX** extensions);
  1113. #endif /* HAVE_TRUNCATED_HMAC */
  1114. #ifdef HAVE_SUPPORTED_CURVES
  1115. typedef struct EllipticCurve {
  1116. word16 name; /* CurveNames */
  1117. struct EllipticCurve* next; /* List Behavior */
  1118. } EllipticCurve;
  1119. CYASSL_LOCAL int TLSX_UseSupportedCurve(TLSX** extensions, word16 name);
  1120. #ifndef NO_CYASSL_SERVER
  1121. CYASSL_LOCAL int TLSX_ValidateEllipticCurves(CYASSL* ssl, byte first,
  1122. byte second);
  1123. #endif
  1124. #endif /* HAVE_SUPPORTED_CURVES */
  1125. #ifdef HAVE_SECURE_RENEGOTIATION
  1126. enum key_cache_state {
  1127. SCR_CACHE_NULL = 0, /* empty / begin state */
  1128. SCR_CACHE_NEEDED, /* need to cache keys */
  1129. SCR_CACHE_COPY, /* we have a cached copy */
  1130. SCR_CACHE_PARTIAL, /* partial restore to real keys */
  1131. SCR_CACHE_COMPLETE /* complete restore to real keys */
  1132. };
  1133. /* Additional Conection State according to rfc5746 section 3.1 */
  1134. typedef struct SecureRenegotiation {
  1135. byte enabled; /* secure_renegotiation flag in rfc */
  1136. byte startScr; /* server requested client to start scr */
  1137. enum key_cache_state cache_status; /* track key cache state */
  1138. byte client_verify_data[TLS_FINISHED_SZ]; /* cached */
  1139. byte server_verify_data[TLS_FINISHED_SZ]; /* cached */
  1140. byte subject_hash[SHA_DIGEST_SIZE]; /* peer cert hash */
  1141. Keys tmp_keys; /* can't overwrite real keys yet */
  1142. } SecureRenegotiation;
  1143. CYASSL_LOCAL int TLSX_UseSecureRenegotiation(TLSX** extensions);
  1144. #endif /* HAVE_SECURE_RENEGOTIATION */
  1145. #ifdef HAVE_SESSION_TICKET
  1146. typedef struct SessionTicket {
  1147. word32 lifetime;
  1148. byte* data;
  1149. word16 size;
  1150. } SessionTicket;
  1151. CYASSL_LOCAL int TLSX_UseSessionTicket(TLSX** extensions,
  1152. SessionTicket* ticket);
  1153. CYASSL_LOCAL SessionTicket* TLSX_SessionTicket_Create(word32 lifetime,
  1154. byte* data, word16 size);
  1155. CYASSL_LOCAL void TLSX_SessionTicket_Free(SessionTicket* ticket);
  1156. #endif /* HAVE_SESSION_TICKET */
  1157. /* CyaSSL context type */
  1158. struct CYASSL_CTX {
  1159. CYASSL_METHOD* method;
  1160. CyaSSL_Mutex countMutex; /* reference count mutex */
  1161. int refCount; /* reference count */
  1162. #ifndef NO_CERTS
  1163. buffer certificate;
  1164. buffer certChain;
  1165. /* chain after self, in DER, with leading size for each cert */
  1166. buffer privateKey;
  1167. buffer serverDH_P;
  1168. buffer serverDH_G;
  1169. CYASSL_CERT_MANAGER* cm; /* our cert manager, ctx owns SSL will use */
  1170. #endif
  1171. Suites suites;
  1172. void* heap; /* for user memory overrides */
  1173. byte verifyPeer;
  1174. byte verifyNone;
  1175. byte failNoCert;
  1176. byte sessionCacheOff;
  1177. byte sessionCacheFlushOff;
  1178. byte sendVerify; /* for client side */
  1179. byte haveRSA; /* RSA available */
  1180. byte haveDH; /* server DH parms set by user */
  1181. byte haveNTRU; /* server private NTRU key loaded */
  1182. byte haveECDSAsig; /* server cert signed w/ ECDSA */
  1183. byte haveStaticECC; /* static server ECC private key */
  1184. byte partialWrite; /* only one msg per write call */
  1185. byte quietShutdown; /* don't send close notify */
  1186. byte groupMessages; /* group handshake messages before sending */
  1187. CallbackIORecv CBIORecv;
  1188. CallbackIOSend CBIOSend;
  1189. #ifdef CYASSL_DTLS
  1190. CallbackGenCookie CBIOCookie; /* gen cookie callback */
  1191. #endif
  1192. VerifyCallback verifyCallback; /* cert verification callback */
  1193. word32 timeout; /* session timeout */
  1194. #ifdef HAVE_ECC
  1195. word16 eccTempKeySz; /* in octets 20 - 66 */
  1196. word32 pkCurveOID; /* curve Ecc_Sum */
  1197. #endif
  1198. #ifndef NO_PSK
  1199. byte havePSK; /* psk key set by user */
  1200. psk_client_callback client_psk_cb; /* client callback */
  1201. psk_server_callback server_psk_cb; /* server callback */
  1202. char server_hint[MAX_PSK_ID_LEN];
  1203. #endif /* NO_PSK */
  1204. #ifdef HAVE_ANON
  1205. byte haveAnon; /* User wants to allow Anon suites */
  1206. #endif /* HAVE_ANON */
  1207. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  1208. pem_password_cb passwd_cb;
  1209. void* userdata;
  1210. #endif /* OPENSSL_EXTRA */
  1211. #ifdef HAVE_OCSP
  1212. CYASSL_OCSP ocsp;
  1213. #endif
  1214. #ifdef HAVE_CAVIUM
  1215. int devId; /* cavium device id to use */
  1216. #endif
  1217. #ifdef HAVE_TLS_EXTENSIONS
  1218. TLSX* extensions; /* RFC 6066 TLS Extensions data */
  1219. #endif
  1220. #ifdef ATOMIC_USER
  1221. CallbackMacEncrypt MacEncryptCb; /* Atomic User Mac/Encrypt Cb */
  1222. CallbackDecryptVerify DecryptVerifyCb; /* Atomic User Decrypt/Verify Cb */
  1223. #endif
  1224. #ifdef HAVE_PK_CALLBACKS
  1225. #ifdef HAVE_ECC
  1226. CallbackEccSign EccSignCb; /* User EccSign Callback handler */
  1227. CallbackEccVerify EccVerifyCb; /* User EccVerify Callback handler */
  1228. #endif /* HAVE_ECC */
  1229. #ifndef NO_RSA
  1230. CallbackRsaSign RsaSignCb; /* User RsaSign Callback handler */
  1231. CallbackRsaVerify RsaVerifyCb; /* User RsaVerify Callback handler */
  1232. CallbackRsaEnc RsaEncCb; /* User Rsa Public Encrypt handler */
  1233. CallbackRsaDec RsaDecCb; /* User Rsa Private Decrypt handler */
  1234. #endif /* NO_RSA */
  1235. #endif /* HAVE_PK_CALLBACKS */
  1236. };
  1237. CYASSL_LOCAL
  1238. int InitSSL_Ctx(CYASSL_CTX*, CYASSL_METHOD*);
  1239. CYASSL_LOCAL
  1240. void FreeSSL_Ctx(CYASSL_CTX*);
  1241. CYASSL_LOCAL
  1242. void SSL_CtxResourceFree(CYASSL_CTX*);
  1243. CYASSL_LOCAL
  1244. int DeriveTlsKeys(CYASSL* ssl);
  1245. CYASSL_LOCAL
  1246. int ProcessOldClientHello(CYASSL* ssl, const byte* input, word32* inOutIdx,
  1247. word32 inSz, word16 sz);
  1248. #ifndef NO_CERTS
  1249. CYASSL_LOCAL
  1250. int AddCA(CYASSL_CERT_MANAGER* ctx, buffer der, int type, int verify);
  1251. CYASSL_LOCAL
  1252. int AlreadySigner(CYASSL_CERT_MANAGER* cm, byte* hash);
  1253. #endif
  1254. /* All cipher suite related info */
  1255. typedef struct CipherSpecs {
  1256. byte bulk_cipher_algorithm;
  1257. byte cipher_type; /* block, stream, or aead */
  1258. byte mac_algorithm;
  1259. byte kea; /* key exchange algo */
  1260. byte sig_algo;
  1261. byte hash_size;
  1262. byte pad_size;
  1263. byte static_ecdh;
  1264. word16 key_size;
  1265. word16 iv_size;
  1266. word16 block_size;
  1267. word16 aead_mac_size;
  1268. } CipherSpecs;
  1269. void InitCipherSpecs(CipherSpecs* cs);
  1270. /* Supported Message Authentication Codes from page 43 */
  1271. enum MACAlgorithm {
  1272. no_mac,
  1273. md5_mac,
  1274. sha_mac,
  1275. sha224_mac,
  1276. sha256_mac, /* needs to match external KDF_MacAlgorithm */
  1277. sha384_mac,
  1278. sha512_mac,
  1279. rmd_mac,
  1280. blake2b_mac
  1281. };
  1282. /* Supported Key Exchange Protocols */
  1283. enum KeyExchangeAlgorithm {
  1284. no_kea,
  1285. rsa_kea,
  1286. diffie_hellman_kea,
  1287. fortezza_kea,
  1288. psk_kea,
  1289. dhe_psk_kea,
  1290. ntru_kea,
  1291. ecc_diffie_hellman_kea,
  1292. ecc_static_diffie_hellman_kea /* for verify suite only */
  1293. };
  1294. /* Supported Authentication Schemes */
  1295. enum SignatureAlgorithm {
  1296. anonymous_sa_algo,
  1297. rsa_sa_algo,
  1298. dsa_sa_algo,
  1299. ecc_dsa_sa_algo
  1300. };
  1301. /* Supprted ECC Curve Types */
  1302. enum EccCurves {
  1303. named_curve = 3
  1304. };
  1305. /* Supprted ECC Named Curves */
  1306. enum EccNamedCurves {
  1307. secp256r1 = 0x17, /* default, OpenSSL also calls it prime256v1 */
  1308. secp384r1 = 0x18,
  1309. secp521r1 = 0x19,
  1310. secp160r1 = 0x10,
  1311. secp192r1 = 0x13, /* Openssl also call it prime192v1 */
  1312. secp224r1 = 0x15
  1313. };
  1314. /* Valid client certificate request types from page 27 */
  1315. enum ClientCertificateType {
  1316. rsa_sign = 1,
  1317. dss_sign = 2,
  1318. rsa_fixed_dh = 3,
  1319. dss_fixed_dh = 4,
  1320. rsa_ephemeral_dh = 5,
  1321. dss_ephemeral_dh = 6,
  1322. fortezza_kea_cert = 20,
  1323. ecdsa_sign = 64,
  1324. rsa_fixed_ecdh = 65,
  1325. ecdsa_fixed_ecdh = 66
  1326. };
  1327. enum CipherType { stream, block, aead };
  1328. /* cipher for now */
  1329. typedef struct Ciphers {
  1330. #ifdef BUILD_ARC4
  1331. Arc4* arc4;
  1332. #endif
  1333. #ifdef BUILD_DES3
  1334. Des3* des3;
  1335. #endif
  1336. #if defined(BUILD_AES) || defined(BUILD_AESGCM)
  1337. Aes* aes;
  1338. #endif
  1339. #ifdef HAVE_CAMELLIA
  1340. Camellia* cam;
  1341. #endif
  1342. #ifdef HAVE_CHACHA
  1343. ChaCha* chacha;
  1344. #endif
  1345. #ifdef HAVE_HC128
  1346. HC128* hc128;
  1347. #endif
  1348. #ifdef BUILD_RABBIT
  1349. Rabbit* rabbit;
  1350. #endif
  1351. byte setup; /* have we set it up flag for detection */
  1352. } Ciphers;
  1353. #ifdef HAVE_ONE_TIME_AUTH
  1354. /* Ciphers for one time authentication such as poly1305 */
  1355. typedef struct OneTimeAuth {
  1356. #ifdef HAVE_POLY1305
  1357. Poly1305* poly1305;
  1358. #endif
  1359. byte setup; /* flag for if a cipher has been set */
  1360. } OneTimeAuth;
  1361. #endif
  1362. CYASSL_LOCAL void InitCiphers(CYASSL* ssl);
  1363. CYASSL_LOCAL void FreeCiphers(CYASSL* ssl);
  1364. /* hashes type */
  1365. typedef struct Hashes {
  1366. #ifndef NO_OLD_TLS
  1367. byte md5[MD5_DIGEST_SIZE];
  1368. #endif
  1369. byte sha[SHA_DIGEST_SIZE];
  1370. #ifndef NO_SHA256
  1371. byte sha256[SHA256_DIGEST_SIZE];
  1372. #endif
  1373. #ifdef CYASSL_SHA384
  1374. byte sha384[SHA384_DIGEST_SIZE];
  1375. #endif
  1376. } Hashes;
  1377. /* Static x509 buffer */
  1378. typedef struct x509_buffer {
  1379. int length; /* actual size */
  1380. byte buffer[MAX_X509_SIZE]; /* max static cert size */
  1381. } x509_buffer;
  1382. /* CyaSSL X509_CHAIN, for no dynamic memory SESSION_CACHE */
  1383. struct CYASSL_X509_CHAIN {
  1384. int count; /* total number in chain */
  1385. x509_buffer certs[MAX_CHAIN_DEPTH]; /* only allow max depth 4 for now */
  1386. };
  1387. /* CyaSSL session type */
  1388. struct CYASSL_SESSION {
  1389. byte sessionID[ID_LEN]; /* id for protocol */
  1390. byte sessionIDSz;
  1391. byte masterSecret[SECRET_LEN]; /* stored secret */
  1392. word32 bornOn; /* create time in seconds */
  1393. word32 timeout; /* timeout in seconds */
  1394. #ifdef SESSION_CERTS
  1395. CYASSL_X509_CHAIN chain; /* peer cert chain, static */
  1396. ProtocolVersion version; /* which version was used */
  1397. byte cipherSuite0; /* first byte, normally 0 */
  1398. byte cipherSuite; /* 2nd byte, actual suite */
  1399. #endif
  1400. #ifndef NO_CLIENT_CACHE
  1401. byte serverID[SERVER_ID_LEN]; /* for easier client lookup */
  1402. word16 idLen; /* serverID length */
  1403. #endif
  1404. #ifdef HAVE_SESSION_TICKET
  1405. byte ticket[SESSION_TICKET_LEN];
  1406. word16 ticketLen;
  1407. #endif
  1408. };
  1409. CYASSL_LOCAL
  1410. CYASSL_SESSION* GetSession(CYASSL*, byte*);
  1411. CYASSL_LOCAL
  1412. int SetSession(CYASSL*, CYASSL_SESSION*);
  1413. typedef int (*hmacfp) (CYASSL*, byte*, const byte*, word32, int, int);
  1414. #ifndef NO_CLIENT_CACHE
  1415. CYASSL_SESSION* GetSessionClient(CYASSL*, const byte*, int);
  1416. #endif
  1417. /* client connect state for nonblocking restart */
  1418. enum ConnectState {
  1419. CONNECT_BEGIN = 0,
  1420. CLIENT_HELLO_SENT,
  1421. HELLO_AGAIN, /* HELLO_AGAIN s for DTLS case */
  1422. HELLO_AGAIN_REPLY,
  1423. FIRST_REPLY_DONE,
  1424. FIRST_REPLY_FIRST,
  1425. FIRST_REPLY_SECOND,
  1426. FIRST_REPLY_THIRD,
  1427. FIRST_REPLY_FOURTH,
  1428. FINISHED_DONE,
  1429. SECOND_REPLY_DONE
  1430. };
  1431. /* server accept state for nonblocking restart */
  1432. enum AcceptState {
  1433. ACCEPT_BEGIN = 0,
  1434. ACCEPT_CLIENT_HELLO_DONE,
  1435. HELLO_VERIFY_SENT,
  1436. ACCEPT_FIRST_REPLY_DONE,
  1437. SERVER_HELLO_SENT,
  1438. CERT_SENT,
  1439. KEY_EXCHANGE_SENT,
  1440. CERT_REQ_SENT,
  1441. SERVER_HELLO_DONE,
  1442. ACCEPT_SECOND_REPLY_DONE,
  1443. CHANGE_CIPHER_SENT,
  1444. ACCEPT_FINISHED_DONE,
  1445. ACCEPT_THIRD_REPLY_DONE
  1446. };
  1447. typedef struct Buffers {
  1448. #ifndef NO_CERTS
  1449. buffer certificate; /* CYASSL_CTX owns, unless we own */
  1450. buffer key; /* CYASSL_CTX owns, unless we own */
  1451. buffer certChain; /* CYASSL_CTX owns, unless we own */
  1452. /* chain after self, in DER, with leading size for each cert */
  1453. buffer serverDH_P; /* CYASSL_CTX owns, unless we own */
  1454. buffer serverDH_G; /* CYASSL_CTX owns, unless we own */
  1455. buffer serverDH_Pub;
  1456. buffer serverDH_Priv;
  1457. #endif
  1458. buffer domainName; /* for client check */
  1459. bufferStatic inputBuffer;
  1460. bufferStatic outputBuffer;
  1461. buffer clearOutputBuffer;
  1462. int prevSent; /* previous plain text bytes sent
  1463. when got WANT_WRITE */
  1464. int plainSz; /* plain text bytes in buffer to send
  1465. when got WANT_WRITE */
  1466. byte weOwnCert; /* SSL own cert flag */
  1467. byte weOwnCertChain; /* SSL own cert chain flag */
  1468. byte weOwnKey; /* SSL own key flag */
  1469. byte weOwnDH; /* SSL own dh (p,g) flag */
  1470. #ifdef CYASSL_DTLS
  1471. CYASSL_DTLS_CTX dtlsCtx; /* DTLS connection context */
  1472. #endif
  1473. #ifdef HAVE_PK_CALLBACKS
  1474. #ifdef HAVE_ECC
  1475. buffer peerEccDsaKey; /* we own for Ecc Verify Callbacks */
  1476. #endif /* HAVE_ECC */
  1477. #ifndef NO_RSA
  1478. buffer peerRsaKey; /* we own for Rsa Verify Callbacks */
  1479. #endif /* NO_RSA */
  1480. #endif /* HAVE_PK_CALLBACKS */
  1481. } Buffers;
  1482. typedef struct Options {
  1483. byte sessionCacheOff;
  1484. byte sessionCacheFlushOff;
  1485. byte cipherSuite0; /* first byte, normally 0 */
  1486. byte cipherSuite; /* second byte, actual suite */
  1487. byte serverState;
  1488. byte clientState;
  1489. byte handShakeState;
  1490. byte handShakeDone; /* at least one handshake complete */
  1491. byte side; /* client or server end */
  1492. byte verifyPeer;
  1493. byte verifyNone;
  1494. byte failNoCert;
  1495. byte downgrade; /* allow downgrade of versions */
  1496. byte minDowngrade; /* minimum downgrade version */
  1497. byte sendVerify; /* false = 0, true = 1, sendBlank = 2 */
  1498. byte resuming;
  1499. byte haveSessionId; /* server may not send */
  1500. byte tls; /* using TLS ? */
  1501. byte tls1_1; /* using TLSv1.1+ ? */
  1502. byte dtls; /* using datagrams ? */
  1503. byte connReset; /* has the peer reset */
  1504. byte isClosed; /* if we consider conn closed */
  1505. byte closeNotify; /* we've recieved a close notify */
  1506. byte sentNotify; /* we've sent a close notify */
  1507. byte connectState; /* nonblocking resume */
  1508. byte acceptState; /* nonblocking resume */
  1509. byte usingCompression; /* are we using compression */
  1510. byte haveRSA; /* RSA available */
  1511. byte haveDH; /* server DH parms set by user */
  1512. byte haveNTRU; /* server NTRU private key loaded */
  1513. byte haveECDSAsig; /* server ECDSA signed cert */
  1514. byte haveStaticECC; /* static server ECC private key */
  1515. byte havePeerCert; /* do we have peer's cert */
  1516. byte havePeerVerify; /* and peer's cert verify */
  1517. byte usingPSK_cipher; /* whether we're using psk as cipher */
  1518. byte usingAnon_cipher; /* whether we're using an anon cipher */
  1519. byte sendAlertState; /* nonblocking resume */
  1520. byte processReply; /* nonblocking resume */
  1521. byte partialWrite; /* only one msg per write call */
  1522. byte quietShutdown; /* don't send close notify */
  1523. byte certOnly; /* stop once we get cert */
  1524. byte groupMessages; /* group handshake messages */
  1525. byte usingNonblock; /* set when using nonblocking socket */
  1526. byte saveArrays; /* save array Memory for user get keys
  1527. or psk */
  1528. #ifdef HAVE_POLY1305
  1529. byte oldPoly; /* set when to use old rfc way of poly*/
  1530. #endif
  1531. #ifndef NO_PSK
  1532. byte havePSK; /* psk key set by user */
  1533. psk_client_callback client_psk_cb;
  1534. psk_server_callback server_psk_cb;
  1535. #endif /* NO_PSK */
  1536. #ifdef HAVE_ANON
  1537. byte haveAnon; /* User wants to allow Anon suites */
  1538. #endif /* HAVE_ANON */
  1539. } Options;
  1540. typedef struct Arrays {
  1541. byte clientRandom[RAN_LEN];
  1542. byte serverRandom[RAN_LEN];
  1543. byte sessionID[ID_LEN];
  1544. byte sessionIDSz;
  1545. byte preMasterSecret[ENCRYPT_LEN];
  1546. byte masterSecret[SECRET_LEN];
  1547. #ifdef CYASSL_DTLS
  1548. byte cookie[MAX_COOKIE_LEN];
  1549. byte cookieSz;
  1550. #endif
  1551. #ifndef NO_PSK
  1552. char client_identity[MAX_PSK_ID_LEN];
  1553. char server_hint[MAX_PSK_ID_LEN];
  1554. byte psk_key[MAX_PSK_KEY_LEN];
  1555. word32 psk_keySz; /* acutal size */
  1556. #endif
  1557. word32 preMasterSz; /* differs for DH, actual size */
  1558. } Arrays;
  1559. #ifndef ASN_NAME_MAX
  1560. #define ASN_NAME_MAX 256
  1561. #endif
  1562. #ifndef MAX_DATE_SZ
  1563. #define MAX_DATE_SZ 32
  1564. #endif
  1565. struct CYASSL_X509_NAME {
  1566. char *name;
  1567. char staticName[ASN_NAME_MAX];
  1568. int dynamicName;
  1569. int sz;
  1570. #ifdef OPENSSL_EXTRA
  1571. DecodedName fullName;
  1572. #endif /* OPENSSL_EXTRA */
  1573. };
  1574. #ifndef EXTERNAL_SERIAL_SIZE
  1575. #define EXTERNAL_SERIAL_SIZE 32
  1576. #endif
  1577. #ifdef NO_ASN
  1578. typedef struct DNS_entry DNS_entry;
  1579. #endif
  1580. struct CYASSL_X509 {
  1581. int version;
  1582. CYASSL_X509_NAME issuer;
  1583. CYASSL_X509_NAME subject;
  1584. int serialSz;
  1585. byte serial[EXTERNAL_SERIAL_SIZE];
  1586. char subjectCN[ASN_NAME_MAX]; /* common name short cut */
  1587. #ifdef CYASSL_SEP
  1588. int deviceTypeSz;
  1589. byte deviceType[EXTERNAL_SERIAL_SIZE];
  1590. int hwTypeSz;
  1591. byte hwType[EXTERNAL_SERIAL_SIZE];
  1592. int hwSerialNumSz;
  1593. byte hwSerialNum[EXTERNAL_SERIAL_SIZE];
  1594. #ifdef OPENSSL_EXTRA
  1595. byte certPolicySet;
  1596. byte certPolicyCrit;
  1597. #endif /* OPENSSL_EXTRA */
  1598. #endif
  1599. int notBeforeSz;
  1600. byte notBefore[MAX_DATE_SZ];
  1601. int notAfterSz;
  1602. byte notAfter[MAX_DATE_SZ];
  1603. int sigOID;
  1604. buffer sig;
  1605. int pubKeyOID;
  1606. buffer pubKey;
  1607. #ifdef HAVE_ECC
  1608. word32 pkCurveOID;
  1609. #endif /* HAVE_ECC */
  1610. buffer derCert; /* may need */
  1611. DNS_entry* altNames; /* alt names list */
  1612. DNS_entry* altNamesNext; /* hint for retrieval */
  1613. byte dynamicMemory; /* dynamic memory flag */
  1614. byte isCa;
  1615. #ifdef OPENSSL_EXTRA
  1616. word32 pathLength;
  1617. word16 keyUsage;
  1618. byte basicConstSet;
  1619. byte basicConstCrit;
  1620. byte basicConstPlSet;
  1621. byte subjAltNameSet;
  1622. byte subjAltNameCrit;
  1623. byte authKeyIdSet;
  1624. byte authKeyIdCrit;
  1625. byte* authKeyId;
  1626. word32 authKeyIdSz;
  1627. byte subjKeyIdSet;
  1628. byte subjKeyIdCrit;
  1629. byte* subjKeyId;
  1630. word32 subjKeyIdSz;
  1631. byte keyUsageSet;
  1632. byte keyUsageCrit;
  1633. #endif /* OPENSSL_EXTRA */
  1634. };
  1635. /* record layer header for PlainText, Compressed, and CipherText */
  1636. typedef struct RecordLayerHeader {
  1637. byte type;
  1638. byte pvMajor;
  1639. byte pvMinor;
  1640. byte length[2];
  1641. } RecordLayerHeader;
  1642. /* record layer header for DTLS PlainText, Compressed, and CipherText */
  1643. typedef struct DtlsRecordLayerHeader {
  1644. byte type;
  1645. byte pvMajor;
  1646. byte pvMinor;
  1647. byte epoch[2]; /* increment on cipher state change */
  1648. byte sequence_number[6]; /* per record */
  1649. byte length[2];
  1650. } DtlsRecordLayerHeader;
  1651. typedef struct DtlsPool {
  1652. buffer buf[DTLS_POOL_SZ];
  1653. int used;
  1654. } DtlsPool;
  1655. typedef struct DtlsMsg {
  1656. struct DtlsMsg* next;
  1657. word32 seq; /* Handshake sequence number */
  1658. word32 sz; /* Length of whole mesage */
  1659. word32 fragSz; /* Length of fragments received */
  1660. byte type;
  1661. byte* buf;
  1662. byte* msg;
  1663. } DtlsMsg;
  1664. #ifdef HAVE_NETX
  1665. /* NETX I/O Callback default */
  1666. typedef struct NetX_Ctx {
  1667. NX_TCP_SOCKET* nxSocket; /* send/recv socket handle */
  1668. NX_PACKET* nxPacket; /* incoming packet handle for short reads */
  1669. ULONG nxOffset; /* offset already read from nxPacket */
  1670. ULONG nxWait; /* wait option flag */
  1671. } NetX_Ctx;
  1672. #endif
  1673. /* Handshake messages recevied from peer (plus change cipher */
  1674. typedef struct MsgsReceived {
  1675. word16 got_hello_request:1;
  1676. word16 got_client_hello:1;
  1677. word16 got_server_hello:1;
  1678. word16 got_hello_verify_request:1;
  1679. word16 got_session_ticket:1;
  1680. word16 got_certificate:1;
  1681. word16 got_server_key_exchange:1;
  1682. word16 got_certificate_request:1;
  1683. word16 got_server_hello_done:1;
  1684. word16 got_certificate_verify:1;
  1685. word16 got_client_key_exchange:1;
  1686. word16 got_finished:1;
  1687. word16 got_change_cipher:1;
  1688. } MsgsReceived;
  1689. /* CyaSSL ssl type */
  1690. struct CYASSL {
  1691. CYASSL_CTX* ctx;
  1692. int error;
  1693. ProtocolVersion version; /* negotiated version */
  1694. ProtocolVersion chVersion; /* client hello version */
  1695. Suites* suites; /* only need during handshake */
  1696. Ciphers encrypt;
  1697. Ciphers decrypt;
  1698. #ifdef HAVE_ONE_TIME_AUTH
  1699. OneTimeAuth auth;
  1700. #endif
  1701. CipherSpecs specs;
  1702. Keys keys;
  1703. MsgsReceived msgsReceived; /* peer messages received */
  1704. int rfd; /* read file descriptor */
  1705. int wfd; /* write file descriptor */
  1706. int rflags; /* user read flags */
  1707. int wflags; /* user write flags */
  1708. CYASSL_BIO* biord; /* socket bio read to free/close */
  1709. CYASSL_BIO* biowr; /* socket bio write to free/close */
  1710. void* IOCB_ReadCtx;
  1711. void* IOCB_WriteCtx;
  1712. RNG* rng;
  1713. #ifndef NO_OLD_TLS
  1714. #ifndef NO_SHA
  1715. Sha hashSha; /* sha hash of handshake msgs */
  1716. #endif
  1717. #ifndef NO_MD5
  1718. Md5 hashMd5; /* md5 hash of handshake msgs */
  1719. #endif
  1720. #endif
  1721. #ifndef NO_SHA256
  1722. Sha256 hashSha256; /* sha256 hash of handshake msgs */
  1723. #endif
  1724. #ifdef CYASSL_SHA384
  1725. Sha384 hashSha384; /* sha384 hash of handshake msgs */
  1726. #endif
  1727. Hashes verifyHashes;
  1728. Hashes certHashes; /* for cert verify */
  1729. Buffers buffers;
  1730. Options options;
  1731. Arrays* arrays;
  1732. CYASSL_SESSION session;
  1733. VerifyCallback verifyCallback; /* cert verification callback */
  1734. void* verifyCbCtx; /* cert verify callback user ctx*/
  1735. #ifndef NO_RSA
  1736. RsaKey* peerRsaKey;
  1737. byte peerRsaKeyPresent;
  1738. #endif
  1739. #ifdef HAVE_NTRU
  1740. word16 peerNtruKeyLen;
  1741. byte peerNtruKey[MAX_NTRU_PUB_KEY_SZ];
  1742. byte peerNtruKeyPresent;
  1743. #endif
  1744. #ifdef HAVE_ECC
  1745. ecc_key* peerEccKey; /* peer's ECDHE key */
  1746. ecc_key* peerEccDsaKey; /* peer's ECDSA key */
  1747. ecc_key* eccTempKey; /* private ECDHE key */
  1748. ecc_key* eccDsaKey; /* private ECDSA key */
  1749. word16 eccTempKeySz; /* in octets 20 - 66 */
  1750. word32 pkCurveOID; /* curve Ecc_Sum */
  1751. byte peerEccKeyPresent;
  1752. byte peerEccDsaKeyPresent;
  1753. byte eccTempKeyPresent;
  1754. byte eccDsaKeyPresent;
  1755. #endif
  1756. hmacfp hmac;
  1757. void* heap; /* for user overrides */
  1758. RecordLayerHeader curRL;
  1759. word16 curSize;
  1760. word32 timeout; /* session timeout */
  1761. CYASSL_CIPHER cipher;
  1762. #ifdef HAVE_LIBZ
  1763. z_stream c_stream; /* compression stream */
  1764. z_stream d_stream; /* decompression stream */
  1765. byte didStreamInit; /* for stream init and end */
  1766. #endif
  1767. #ifdef CYASSL_DTLS
  1768. int dtls_timeout_init; /* starting timeout vaule */
  1769. int dtls_timeout_max; /* maximum timeout value */
  1770. int dtls_timeout; /* current timeout value, changes */
  1771. DtlsPool* dtls_pool;
  1772. DtlsMsg* dtls_msg_list;
  1773. void* IOCB_CookieCtx; /* gen cookie ctx */
  1774. word32 dtls_expected_rx;
  1775. #endif
  1776. #ifdef CYASSL_CALLBACKS
  1777. HandShakeInfo handShakeInfo; /* info saved during handshake */
  1778. TimeoutInfo timeoutInfo; /* info saved during handshake */
  1779. byte hsInfoOn; /* track handshake info */
  1780. byte toInfoOn; /* track timeout info */
  1781. #endif
  1782. #ifdef HAVE_FUZZER
  1783. CallbackFuzzer fuzzerCb; /* for testing with using fuzzer */
  1784. void* fuzzerCtx; /* user defined pointer */
  1785. #endif
  1786. #ifdef KEEP_PEER_CERT
  1787. CYASSL_X509 peerCert; /* X509 peer cert */
  1788. #endif
  1789. #ifdef FORTRESS
  1790. void* ex_data[MAX_EX_DATA]; /* external data, for Fortress */
  1791. #endif
  1792. #ifdef HAVE_CAVIUM
  1793. int devId; /* cavium device id to use */
  1794. #endif
  1795. #ifdef HAVE_TLS_EXTENSIONS
  1796. TLSX* extensions; /* RFC 6066 TLS Extensions data */
  1797. #ifdef HAVE_MAX_FRAGMENT
  1798. word16 max_fragment;
  1799. #endif
  1800. #ifdef HAVE_TRUNCATED_HMAC
  1801. byte truncated_hmac;
  1802. #endif
  1803. #ifdef HAVE_SECURE_RENEGOTIATION
  1804. SecureRenegotiation* secure_renegotiation; /* valid pointer indicates */
  1805. #endif /* user turned on */
  1806. #if !defined(NO_CYASSL_CLIENT) && defined(HAVE_SESSION_TICKET)
  1807. CallbackSessionTicket session_ticket_cb;
  1808. void* session_ticket_ctx;
  1809. byte expect_session_ticket;
  1810. #endif
  1811. #endif /* HAVE_TLS_EXTENSIONS */
  1812. #ifdef HAVE_NETX
  1813. NetX_Ctx nxCtx; /* NetX IO Context */
  1814. #endif
  1815. #ifdef SESSION_INDEX
  1816. int sessionIndex; /* Session's location in the cache. */
  1817. #endif
  1818. CYASSL_ALERT_HISTORY alert_history;
  1819. #ifdef ATOMIC_USER
  1820. void* MacEncryptCtx; /* Atomic User Mac/Encrypt Callback Context */
  1821. void* DecryptVerifyCtx; /* Atomic User Decrypt/Verify Callback Context */
  1822. #endif
  1823. #ifdef HAVE_PK_CALLBACKS
  1824. #ifdef HAVE_ECC
  1825. void* EccSignCtx; /* Ecc Sign Callback Context */
  1826. void* EccVerifyCtx; /* Ecc Verify Callback Context */
  1827. #endif /* HAVE_ECC */
  1828. #ifndef NO_RSA
  1829. void* RsaSignCtx; /* Rsa Sign Callback Context */
  1830. void* RsaVerifyCtx; /* Rsa Verify Callback Context */
  1831. void* RsaEncCtx; /* Rsa Public Encrypt Callback Context */
  1832. void* RsaDecCtx; /* Rsa Private Decrypt Callback Context */
  1833. #endif /* NO_RSA */
  1834. #endif /* HAVE_PK_CALLBACKS */
  1835. #ifdef HAVE_SECRET_CALLBACK
  1836. SessionSecretCb sessionSecretCb;
  1837. void* sessionSecretCtx;
  1838. #endif /* HAVE_SECRET_CALLBACK */
  1839. };
  1840. CYASSL_LOCAL
  1841. int InitSSL(CYASSL*, CYASSL_CTX*);
  1842. CYASSL_LOCAL
  1843. void FreeSSL(CYASSL*);
  1844. CYASSL_API void SSL_ResourceFree(CYASSL*); /* Micrium uses */
  1845. enum {
  1846. IV_SZ = 32, /* max iv sz */
  1847. NAME_SZ = 80 /* max one line */
  1848. };
  1849. typedef struct EncryptedInfo {
  1850. char name[NAME_SZ]; /* encryption name */
  1851. byte iv[IV_SZ]; /* encrypted IV */
  1852. word32 ivSz; /* encrypted IV size */
  1853. long consumed; /* tracks PEM bytes consumed */
  1854. byte set; /* if encryption set */
  1855. CYASSL_CTX* ctx; /* CTX owner */
  1856. } EncryptedInfo;
  1857. #ifndef NO_CERTS
  1858. CYASSL_LOCAL int PemToDer(const unsigned char* buff, long sz, int type,
  1859. buffer* der, void* heap, EncryptedInfo* info,
  1860. int* eccKey);
  1861. CYASSL_LOCAL int ProcessFile(CYASSL_CTX* ctx, const char* fname, int format,
  1862. int type, CYASSL* ssl, int userChain,
  1863. CYASSL_CRL* crl);
  1864. #endif
  1865. #ifdef CYASSL_CALLBACKS
  1866. CYASSL_LOCAL
  1867. void InitHandShakeInfo(HandShakeInfo*);
  1868. CYASSL_LOCAL
  1869. void FinishHandShakeInfo(HandShakeInfo*, const CYASSL*);
  1870. CYASSL_LOCAL
  1871. void AddPacketName(const char*, HandShakeInfo*);
  1872. CYASSL_LOCAL
  1873. void InitTimeoutInfo(TimeoutInfo*);
  1874. CYASSL_LOCAL
  1875. void FreeTimeoutInfo(TimeoutInfo*, void*);
  1876. CYASSL_LOCAL
  1877. void AddPacketInfo(const char*, TimeoutInfo*, const byte*, int, void*);
  1878. CYASSL_LOCAL
  1879. void AddLateName(const char*, TimeoutInfo*);
  1880. CYASSL_LOCAL
  1881. void AddLateRecordHeader(const RecordLayerHeader* rl, TimeoutInfo* info);
  1882. #endif
  1883. /* Record Layer Header identifier from page 12 */
  1884. enum ContentType {
  1885. no_type = 0,
  1886. change_cipher_spec = 20,
  1887. alert = 21,
  1888. handshake = 22,
  1889. application_data = 23
  1890. };
  1891. /* handshake header, same for each message type, pgs 20/21 */
  1892. typedef struct HandShakeHeader {
  1893. byte type;
  1894. word24 length;
  1895. } HandShakeHeader;
  1896. /* DTLS handshake header, same for each message type */
  1897. typedef struct DtlsHandShakeHeader {
  1898. byte type;
  1899. word24 length;
  1900. byte message_seq[2]; /* start at 0, restransmit gets same # */
  1901. word24 fragment_offset; /* bytes in previous fragments */
  1902. word24 fragment_length; /* length of this fragment */
  1903. } DtlsHandShakeHeader;
  1904. enum HandShakeType {
  1905. no_shake = -1,
  1906. hello_request = 0,
  1907. client_hello = 1,
  1908. server_hello = 2,
  1909. hello_verify_request = 3, /* DTLS addition */
  1910. session_ticket = 4,
  1911. certificate = 11,
  1912. server_key_exchange = 12,
  1913. certificate_request = 13,
  1914. server_hello_done = 14,
  1915. certificate_verify = 15,
  1916. client_key_exchange = 16,
  1917. finished = 20,
  1918. change_cipher_hs = 55 /* simulate unique handshake type for sanity
  1919. checks. record layer change_cipher
  1920. conflicts with handshake finished */
  1921. };
  1922. static const byte client[SIZEOF_SENDER] = { 0x43, 0x4C, 0x4E, 0x54 };
  1923. static const byte server[SIZEOF_SENDER] = { 0x53, 0x52, 0x56, 0x52 };
  1924. static const byte tls_client[FINISHED_LABEL_SZ + 1] = "client finished";
  1925. static const byte tls_server[FINISHED_LABEL_SZ + 1] = "server finished";
  1926. /* internal functions */
  1927. CYASSL_LOCAL int SendChangeCipher(CYASSL*);
  1928. CYASSL_LOCAL int SendData(CYASSL*, const void*, int);
  1929. CYASSL_LOCAL int SendCertificate(CYASSL*);
  1930. CYASSL_LOCAL int SendCertificateRequest(CYASSL*);
  1931. CYASSL_LOCAL int SendServerKeyExchange(CYASSL*);
  1932. CYASSL_LOCAL int SendBuffered(CYASSL*);
  1933. CYASSL_LOCAL int ReceiveData(CYASSL*, byte*, int, int);
  1934. CYASSL_LOCAL int SendFinished(CYASSL*);
  1935. CYASSL_LOCAL int SendAlert(CYASSL*, int, int);
  1936. CYASSL_LOCAL int ProcessReply(CYASSL*);
  1937. CYASSL_LOCAL int SetCipherSpecs(CYASSL*);
  1938. CYASSL_LOCAL int MakeMasterSecret(CYASSL*);
  1939. CYASSL_LOCAL int AddSession(CYASSL*);
  1940. CYASSL_LOCAL int DeriveKeys(CYASSL* ssl);
  1941. CYASSL_LOCAL int StoreKeys(CYASSL* ssl, const byte* keyData);
  1942. CYASSL_LOCAL int IsTLS(const CYASSL* ssl);
  1943. CYASSL_LOCAL int IsAtLeastTLSv1_2(const CYASSL* ssl);
  1944. CYASSL_LOCAL void FreeHandshakeResources(CYASSL* ssl);
  1945. CYASSL_LOCAL void ShrinkInputBuffer(CYASSL* ssl, int forcedFree);
  1946. CYASSL_LOCAL void ShrinkOutputBuffer(CYASSL* ssl);
  1947. CYASSL_LOCAL int VerifyClientSuite(CYASSL* ssl);
  1948. #ifndef NO_CERTS
  1949. CYASSL_LOCAL Signer* GetCA(void* cm, byte* hash);
  1950. #ifndef NO_SKID
  1951. CYASSL_LOCAL Signer* GetCAByName(void* cm, byte* hash);
  1952. #endif
  1953. #endif
  1954. CYASSL_LOCAL int BuildTlsFinished(CYASSL* ssl, Hashes* hashes,
  1955. const byte* sender);
  1956. CYASSL_LOCAL void FreeArrays(CYASSL* ssl, int keep);
  1957. CYASSL_LOCAL int CheckAvailableSize(CYASSL *ssl, int size);
  1958. CYASSL_LOCAL int GrowInputBuffer(CYASSL* ssl, int size, int usedLength);
  1959. #ifndef NO_TLS
  1960. CYASSL_LOCAL int MakeTlsMasterSecret(CYASSL*);
  1961. CYASSL_LOCAL int TLS_hmac(CYASSL* ssl, byte* digest, const byte* in,
  1962. word32 sz, int content, int verify);
  1963. #endif
  1964. #ifndef NO_CYASSL_CLIENT
  1965. CYASSL_LOCAL int SendClientHello(CYASSL*);
  1966. CYASSL_LOCAL int SendClientKeyExchange(CYASSL*);
  1967. CYASSL_LOCAL int SendCertificateVerify(CYASSL*);
  1968. #endif /* NO_CYASSL_CLIENT */
  1969. #ifndef NO_CYASSL_SERVER
  1970. CYASSL_LOCAL int SendServerHello(CYASSL*);
  1971. CYASSL_LOCAL int SendServerHelloDone(CYASSL*);
  1972. #ifdef CYASSL_DTLS
  1973. CYASSL_LOCAL int SendHelloVerifyRequest(CYASSL*);
  1974. #endif
  1975. #endif /* NO_CYASSL_SERVER */
  1976. #ifdef CYASSL_DTLS
  1977. CYASSL_LOCAL int DtlsPoolInit(CYASSL*);
  1978. CYASSL_LOCAL int DtlsPoolSave(CYASSL*, const byte*, int);
  1979. CYASSL_LOCAL int DtlsPoolTimeout(CYASSL*);
  1980. CYASSL_LOCAL int DtlsPoolSend(CYASSL*);
  1981. CYASSL_LOCAL void DtlsPoolReset(CYASSL*);
  1982. CYASSL_LOCAL DtlsMsg* DtlsMsgNew(word32, void*);
  1983. CYASSL_LOCAL void DtlsMsgDelete(DtlsMsg*, void*);
  1984. CYASSL_LOCAL void DtlsMsgListDelete(DtlsMsg*, void*);
  1985. CYASSL_LOCAL void DtlsMsgSet(DtlsMsg*, word32, const byte*, byte,
  1986. word32, word32);
  1987. CYASSL_LOCAL DtlsMsg* DtlsMsgFind(DtlsMsg*, word32);
  1988. CYASSL_LOCAL DtlsMsg* DtlsMsgStore(DtlsMsg*, word32, const byte*, word32,
  1989. byte, word32, word32, void*);
  1990. CYASSL_LOCAL DtlsMsg* DtlsMsgInsert(DtlsMsg*, DtlsMsg*);
  1991. #endif /* CYASSL_DTLS */
  1992. #ifndef NO_TLS
  1993. #endif /* NO_TLS */
  1994. CYASSL_LOCAL word32 LowResTimer(void);
  1995. CYASSL_LOCAL void InitX509Name(CYASSL_X509_NAME*, int);
  1996. CYASSL_LOCAL void FreeX509Name(CYASSL_X509_NAME* name);
  1997. CYASSL_LOCAL void InitX509(CYASSL_X509*, int);
  1998. CYASSL_LOCAL void FreeX509(CYASSL_X509*);
  1999. #ifndef NO_CERTS
  2000. CYASSL_LOCAL int CopyDecodedToX509(CYASSL_X509*, DecodedCert*);
  2001. #endif
  2002. /* used by ssl.c and cyassl_int.c */
  2003. CYASSL_LOCAL void c32to24(word32 in, word24 out);
  2004. CYASSL_LOCAL const char* const* GetCipherNames(void);
  2005. CYASSL_LOCAL int GetCipherNamesSize(void);
  2006. enum encrypt_side {
  2007. ENCRYPT_SIDE_ONLY = 1,
  2008. DECRYPT_SIDE_ONLY,
  2009. ENCRYPT_AND_DECRYPT_SIDE
  2010. };
  2011. CYASSL_LOCAL int SetKeysSide(CYASSL*, enum encrypt_side);
  2012. #ifdef __cplusplus
  2013. } /* extern "C" */
  2014. #endif
  2015. #endif /* CyaSSL_INT_H */