internal.h 73 KB

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