PageRenderTime 64ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 2ms

/src/internal.c

https://github.com/andersmalm/cyassl
C | 8996 lines | 7080 code | 1550 blank | 366 comment | 1611 complexity | 6cce09424e74b757c538993f414fadba MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /* internal.c
  2. *
  3. * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <cyassl/internal.h>
  25. #include <cyassl/error.h>
  26. #include <cyassl/ctaocrypt/asn.h>
  27. #ifdef HAVE_LIBZ
  28. #include "zlib.h"
  29. #endif
  30. #ifdef HAVE_NTRU
  31. #include "crypto_ntru.h"
  32. #endif
  33. #if defined(DEBUG_CYASSL) || defined(SHOW_SECRETS)
  34. #ifdef FREESCALE_MQX
  35. #include <fio.h>
  36. #else
  37. #include <stdio.h>
  38. #endif
  39. #endif
  40. #ifdef __sun
  41. #include <sys/filio.h>
  42. #endif
  43. #ifndef TRUE
  44. #define TRUE 1
  45. #endif
  46. #ifndef FALSE
  47. #define FALSE 0
  48. #endif
  49. #if defined(OPENSSL_EXTRA) && defined(NO_DH)
  50. #error OPENSSL_EXTRA needs DH, please remove NO_DH
  51. #endif
  52. #ifndef NO_CYASSL_CLIENT
  53. static int DoHelloVerifyRequest(CYASSL* ssl, const byte* input, word32*);
  54. static int DoServerHello(CYASSL* ssl, const byte* input, word32*, word32);
  55. static int DoServerKeyExchange(CYASSL* ssl, const byte* input, word32*);
  56. #ifndef NO_CERTS
  57. static int DoCertificateRequest(CYASSL* ssl, const byte* input,word32*);
  58. #endif
  59. #endif
  60. #ifndef NO_CYASSL_SERVER
  61. static int DoClientHello(CYASSL* ssl, const byte* input, word32*, word32,
  62. word32);
  63. static int DoClientKeyExchange(CYASSL* ssl, byte* input, word32*, word32);
  64. #if !defined(NO_RSA) || defined(HAVE_ECC)
  65. static int DoCertificateVerify(CYASSL* ssl, byte*, word32*, word32);
  66. #endif
  67. #endif
  68. typedef enum {
  69. doProcessInit = 0,
  70. #ifndef NO_CYASSL_SERVER
  71. runProcessOldClientHello,
  72. #endif
  73. getRecordLayerHeader,
  74. getData,
  75. runProcessingOneMessage
  76. } processReply;
  77. #ifndef NO_MD5
  78. static void Hmac(CYASSL* ssl, byte* digest, const byte* buffer, word32 sz,
  79. int content, int verify);
  80. static void BuildCertHashes(CYASSL* ssl, Hashes* hashes);
  81. #endif
  82. #ifndef min
  83. static INLINE word32 min(word32 a, word32 b)
  84. {
  85. return a > b ? b : a;
  86. }
  87. #endif /* min */
  88. int IsTLS(const CYASSL* ssl)
  89. {
  90. if (ssl->version.major == SSLv3_MAJOR && ssl->version.minor >=TLSv1_MINOR)
  91. return 1;
  92. return 0;
  93. }
  94. int IsAtLeastTLSv1_2(const CYASSL* ssl)
  95. {
  96. if (ssl->version.major == SSLv3_MAJOR && ssl->version.minor >=TLSv1_2_MINOR)
  97. return 1;
  98. return 0;
  99. }
  100. #ifdef HAVE_NTRU
  101. static byte GetEntropy(ENTROPY_CMD cmd, byte* out)
  102. {
  103. /* TODO: add locking? */
  104. static RNG rng;
  105. if (cmd == INIT) {
  106. int ret = InitRng(&rng);
  107. if (ret == 0)
  108. return 1;
  109. else
  110. return 0;
  111. }
  112. if (out == NULL)
  113. return 0;
  114. if (cmd == GET_BYTE_OF_ENTROPY) {
  115. RNG_GenerateBlock(&rng, out, 1);
  116. return 1;
  117. }
  118. if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
  119. *out = 1;
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. #endif /* HAVE_NTRU */
  125. /* used by ssl.c too */
  126. void c32to24(word32 in, word24 out)
  127. {
  128. out[0] = (in >> 16) & 0xff;
  129. out[1] = (in >> 8) & 0xff;
  130. out[2] = in & 0xff;
  131. }
  132. #ifdef CYASSL_DTLS
  133. static INLINE void c32to48(word32 in, byte out[6])
  134. {
  135. out[0] = 0;
  136. out[1] = 0;
  137. out[2] = (in >> 24) & 0xff;
  138. out[3] = (in >> 16) & 0xff;
  139. out[4] = (in >> 8) & 0xff;
  140. out[5] = in & 0xff;
  141. }
  142. #endif /* CYASSL_DTLS */
  143. /* convert 16 bit integer to opaque */
  144. static INLINE void c16toa(word16 u16, byte* c)
  145. {
  146. c[0] = (u16 >> 8) & 0xff;
  147. c[1] = u16 & 0xff;
  148. }
  149. /* convert 32 bit integer to opaque */
  150. static INLINE void c32toa(word32 u32, byte* c)
  151. {
  152. c[0] = (u32 >> 24) & 0xff;
  153. c[1] = (u32 >> 16) & 0xff;
  154. c[2] = (u32 >> 8) & 0xff;
  155. c[3] = u32 & 0xff;
  156. }
  157. /* convert a 24 bit integer into a 32 bit one */
  158. static INLINE void c24to32(const word24 u24, word32* u32)
  159. {
  160. *u32 = (u24[0] << 16) | (u24[1] << 8) | u24[2];
  161. }
  162. /* convert opaque to 16 bit integer */
  163. static INLINE void ato16(const byte* c, word16* u16)
  164. {
  165. *u16 = (c[0] << 8) | (c[1]);
  166. }
  167. /* convert opaque to 32 bit integer */
  168. static INLINE void ato32(const byte* c, word32* u32)
  169. {
  170. *u32 = (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3];
  171. }
  172. #ifdef HAVE_LIBZ
  173. /* alloc user allocs to work with zlib */
  174. static void* myAlloc(void* opaque, unsigned int item, unsigned int size)
  175. {
  176. (void)opaque;
  177. return XMALLOC(item * size, opaque, DYNAMIC_TYPE_LIBZ);
  178. }
  179. static void myFree(void* opaque, void* memory)
  180. {
  181. (void)opaque;
  182. XFREE(memory, opaque, DYNAMIC_TYPE_LIBZ);
  183. }
  184. /* init zlib comp/decomp streams, 0 on success */
  185. static int InitStreams(CYASSL* ssl)
  186. {
  187. ssl->c_stream.zalloc = (alloc_func)myAlloc;
  188. ssl->c_stream.zfree = (free_func)myFree;
  189. ssl->c_stream.opaque = (voidpf)ssl->heap;
  190. if (deflateInit(&ssl->c_stream, Z_DEFAULT_COMPRESSION) != Z_OK)
  191. return ZLIB_INIT_ERROR;
  192. ssl->didStreamInit = 1;
  193. ssl->d_stream.zalloc = (alloc_func)myAlloc;
  194. ssl->d_stream.zfree = (free_func)myFree;
  195. ssl->d_stream.opaque = (voidpf)ssl->heap;
  196. if (inflateInit(&ssl->d_stream) != Z_OK) return ZLIB_INIT_ERROR;
  197. return 0;
  198. }
  199. static void FreeStreams(CYASSL* ssl)
  200. {
  201. if (ssl->didStreamInit) {
  202. deflateEnd(&ssl->c_stream);
  203. inflateEnd(&ssl->d_stream);
  204. }
  205. }
  206. /* compress in to out, return out size or error */
  207. static int Compress(CYASSL* ssl, byte* in, int inSz, byte* out, int outSz)
  208. {
  209. int err;
  210. int currTotal = (int)ssl->c_stream.total_out;
  211. ssl->c_stream.next_in = in;
  212. ssl->c_stream.avail_in = inSz;
  213. ssl->c_stream.next_out = out;
  214. ssl->c_stream.avail_out = outSz;
  215. err = deflate(&ssl->c_stream, Z_SYNC_FLUSH);
  216. if (err != Z_OK && err != Z_STREAM_END) return ZLIB_COMPRESS_ERROR;
  217. return (int)ssl->c_stream.total_out - currTotal;
  218. }
  219. /* decompress in to out, returnn out size or error */
  220. static int DeCompress(CYASSL* ssl, byte* in, int inSz, byte* out, int outSz)
  221. {
  222. int err;
  223. int currTotal = (int)ssl->d_stream.total_out;
  224. ssl->d_stream.next_in = in;
  225. ssl->d_stream.avail_in = inSz;
  226. ssl->d_stream.next_out = out;
  227. ssl->d_stream.avail_out = outSz;
  228. err = inflate(&ssl->d_stream, Z_SYNC_FLUSH);
  229. if (err != Z_OK && err != Z_STREAM_END) return ZLIB_DECOMPRESS_ERROR;
  230. return (int)ssl->d_stream.total_out - currTotal;
  231. }
  232. #endif /* HAVE_LIBZ */
  233. void InitSSL_Method(CYASSL_METHOD* method, ProtocolVersion pv)
  234. {
  235. method->version = pv;
  236. method->side = CLIENT_END;
  237. method->downgrade = 0;
  238. }
  239. /* Initialze SSL context, return 0 on success */
  240. int InitSSL_Ctx(CYASSL_CTX* ctx, CYASSL_METHOD* method)
  241. {
  242. ctx->method = method;
  243. ctx->refCount = 1; /* so either CTX_free or SSL_free can release */
  244. #ifndef NO_CERTS
  245. ctx->certificate.buffer = 0;
  246. ctx->certChain.buffer = 0;
  247. ctx->privateKey.buffer = 0;
  248. ctx->serverDH_P.buffer = 0;
  249. ctx->serverDH_G.buffer = 0;
  250. #endif
  251. ctx->haveDH = 0;
  252. ctx->haveNTRU = 0; /* start off */
  253. ctx->haveECDSAsig = 0; /* start off */
  254. ctx->haveStaticECC = 0; /* start off */
  255. ctx->heap = ctx; /* defaults to self */
  256. #ifndef NO_PSK
  257. ctx->havePSK = 0;
  258. ctx->server_hint[0] = 0;
  259. ctx->client_psk_cb = 0;
  260. ctx->server_psk_cb = 0;
  261. #endif /* NO_PSK */
  262. #ifdef HAVE_ECC
  263. ctx->eccTempKeySz = ECDHE_SIZE;
  264. #endif
  265. #ifdef OPENSSL_EXTRA
  266. ctx->passwd_cb = 0;
  267. ctx->userdata = 0;
  268. #endif /* OPENSSL_EXTRA */
  269. ctx->timeout = DEFAULT_TIMEOUT;
  270. #ifndef CYASSL_USER_IO
  271. ctx->CBIORecv = EmbedReceive;
  272. ctx->CBIOSend = EmbedSend;
  273. #ifdef CYASSL_DTLS
  274. if (method->version.major == DTLS_MAJOR
  275. && method->version.minor == DTLS_MINOR) {
  276. ctx->CBIORecv = EmbedReceiveFrom;
  277. ctx->CBIOSend = EmbedSendTo;
  278. }
  279. #endif
  280. #else
  281. /* user will set */
  282. ctx->CBIORecv = NULL;
  283. ctx->CBIOSend = NULL;
  284. #endif
  285. ctx->partialWrite = 0;
  286. ctx->verifyCallback = 0;
  287. #ifndef NO_CERTS
  288. ctx->cm = CyaSSL_CertManagerNew();
  289. #endif
  290. #ifdef HAVE_NTRU
  291. if (method->side == CLIENT_END)
  292. ctx->haveNTRU = 1; /* always on cliet side */
  293. /* server can turn on by loading key */
  294. #endif
  295. #ifdef HAVE_ECC
  296. if (method->side == CLIENT_END) {
  297. ctx->haveECDSAsig = 1; /* always on cliet side */
  298. ctx->haveStaticECC = 1; /* server can turn on by loading key */
  299. }
  300. #endif
  301. ctx->suites.setSuites = 0; /* user hasn't set yet */
  302. /* remove DH later if server didn't set, add psk later */
  303. InitSuites(&ctx->suites, method->version, TRUE, FALSE, TRUE, ctx->haveNTRU,
  304. ctx->haveECDSAsig, ctx->haveStaticECC, method->side);
  305. ctx->verifyPeer = 0;
  306. ctx->verifyNone = 0;
  307. ctx->failNoCert = 0;
  308. ctx->sessionCacheOff = 0; /* initially on */
  309. ctx->sessionCacheFlushOff = 0; /* initially on */
  310. ctx->sendVerify = 0;
  311. ctx->quietShutdown = 0;
  312. ctx->groupMessages = 0;
  313. #ifdef HAVE_OCSP
  314. CyaSSL_OCSP_Init(&ctx->ocsp);
  315. #endif
  316. #ifdef HAVE_CAVIUM
  317. ctx->devId = NO_CAVIUM_DEVICE;
  318. #endif
  319. if (InitMutex(&ctx->countMutex) < 0) {
  320. CYASSL_MSG("Mutex error on CTX init");
  321. return BAD_MUTEX_ERROR;
  322. }
  323. #ifndef NO_CERTS
  324. if (ctx->cm == NULL) {
  325. CYASSL_MSG("Bad Cert Manager New");
  326. return BAD_CERT_MANAGER_ERROR;
  327. }
  328. #endif
  329. return 0;
  330. }
  331. /* In case contexts are held in array and don't want to free actual ctx */
  332. void SSL_CtxResourceFree(CYASSL_CTX* ctx)
  333. {
  334. XFREE(ctx->method, ctx->heap, DYNAMIC_TYPE_METHOD);
  335. #ifndef NO_CERTS
  336. XFREE(ctx->serverDH_G.buffer, ctx->heap, DYNAMIC_TYPE_DH);
  337. XFREE(ctx->serverDH_P.buffer, ctx->heap, DYNAMIC_TYPE_DH);
  338. XFREE(ctx->privateKey.buffer, ctx->heap, DYNAMIC_TYPE_KEY);
  339. XFREE(ctx->certificate.buffer, ctx->heap, DYNAMIC_TYPE_CERT);
  340. XFREE(ctx->certChain.buffer, ctx->heap, DYNAMIC_TYPE_CERT);
  341. CyaSSL_CertManagerFree(ctx->cm);
  342. #endif
  343. #ifdef HAVE_OCSP
  344. CyaSSL_OCSP_Cleanup(&ctx->ocsp);
  345. #endif
  346. }
  347. void FreeSSL_Ctx(CYASSL_CTX* ctx)
  348. {
  349. int doFree = 0;
  350. if (LockMutex(&ctx->countMutex) != 0) {
  351. CYASSL_MSG("Couldn't lock count mutex");
  352. return;
  353. }
  354. ctx->refCount--;
  355. if (ctx->refCount == 0)
  356. doFree = 1;
  357. UnLockMutex(&ctx->countMutex);
  358. if (doFree) {
  359. CYASSL_MSG("CTX ref count down to 0, doing full free");
  360. SSL_CtxResourceFree(ctx);
  361. XFREE(ctx, ctx->heap, DYNAMIC_TYPE_CTX);
  362. }
  363. else {
  364. (void)ctx;
  365. CYASSL_MSG("CTX ref count not 0 yet, no free");
  366. }
  367. }
  368. /* Set cipher pointers to null */
  369. void InitCiphers(CYASSL* ssl)
  370. {
  371. #ifdef BUILD_ARC4
  372. ssl->encrypt.arc4 = NULL;
  373. ssl->decrypt.arc4 = NULL;
  374. #endif
  375. #ifdef BUILD_DES3
  376. ssl->encrypt.des3 = NULL;
  377. ssl->decrypt.des3 = NULL;
  378. #endif
  379. #ifdef BUILD_AES
  380. ssl->encrypt.aes = NULL;
  381. ssl->decrypt.aes = NULL;
  382. #endif
  383. #ifdef HAVE_CAMELLIA
  384. ssl->encrypt.cam = NULL;
  385. ssl->decrypt.cam = NULL;
  386. #endif
  387. #ifdef HAVE_HC128
  388. ssl->encrypt.hc128 = NULL;
  389. ssl->decrypt.hc128 = NULL;
  390. #endif
  391. #ifdef BUILD_RABBIT
  392. ssl->encrypt.rabbit = NULL;
  393. ssl->decrypt.rabbit = NULL;
  394. #endif
  395. ssl->encrypt.setup = 0;
  396. ssl->decrypt.setup = 0;
  397. }
  398. /* Free ciphers */
  399. void FreeCiphers(CYASSL* ssl)
  400. {
  401. (void)ssl;
  402. #ifdef BUILD_ARC4
  403. #ifdef HAVE_CAVIUM
  404. if (ssl->devId != NO_CAVIUM_DEVICE) {
  405. Arc4FreeCavium(ssl->encrypt.arc4);
  406. Arc4FreeCavium(ssl->decrypt.arc4);
  407. }
  408. #endif
  409. XFREE(ssl->encrypt.arc4, ssl->heap, DYNAMIC_TYPE_CIPHER);
  410. XFREE(ssl->decrypt.arc4, ssl->heap, DYNAMIC_TYPE_CIPHER);
  411. #endif
  412. #ifdef BUILD_DES3
  413. #ifdef HAVE_CAVIUM
  414. if (ssl->devId != NO_CAVIUM_DEVICE) {
  415. Des3_FreeCavium(ssl->encrypt.des3);
  416. Des3_FreeCavium(ssl->decrypt.des3);
  417. }
  418. #endif
  419. XFREE(ssl->encrypt.des3, ssl->heap, DYNAMIC_TYPE_CIPHER);
  420. XFREE(ssl->decrypt.des3, ssl->heap, DYNAMIC_TYPE_CIPHER);
  421. #endif
  422. #ifdef BUILD_AES
  423. #ifdef HAVE_CAVIUM
  424. if (ssl->devId != NO_CAVIUM_DEVICE) {
  425. AesFreeCavium(ssl->encrypt.aes);
  426. AesFreeCavium(ssl->decrypt.aes);
  427. }
  428. #endif
  429. XFREE(ssl->encrypt.aes, ssl->heap, DYNAMIC_TYPE_CIPHER);
  430. XFREE(ssl->decrypt.aes, ssl->heap, DYNAMIC_TYPE_CIPHER);
  431. #endif
  432. #ifdef BUILD_CAMELLIA
  433. XFREE(ssl->encrypt.cam, ssl->heap, DYNAMIC_TYPE_CIPHER);
  434. XFREE(ssl->decrypt.cam, ssl->heap, DYNAMIC_TYPE_CIPHER);
  435. #endif
  436. #ifdef HAVE_HC128
  437. XFREE(ssl->encrypt.hc128, ssl->heap, DYNAMIC_TYPE_CIPHER);
  438. XFREE(ssl->decrypt.hc128, ssl->heap, DYNAMIC_TYPE_CIPHER);
  439. #endif
  440. #ifdef BUILD_RABBIT
  441. XFREE(ssl->encrypt.rabbit, ssl->heap, DYNAMIC_TYPE_CIPHER);
  442. XFREE(ssl->decrypt.rabbit, ssl->heap, DYNAMIC_TYPE_CIPHER);
  443. #endif
  444. }
  445. void InitCipherSpecs(CipherSpecs* cs)
  446. {
  447. cs->bulk_cipher_algorithm = INVALID_BYTE;
  448. cs->cipher_type = INVALID_BYTE;
  449. cs->mac_algorithm = INVALID_BYTE;
  450. cs->kea = INVALID_BYTE;
  451. cs->sig_algo = INVALID_BYTE;
  452. cs->hash_size = 0;
  453. cs->static_ecdh = 0;
  454. cs->key_size = 0;
  455. cs->iv_size = 0;
  456. cs->block_size = 0;
  457. }
  458. void InitSuites(Suites* suites, ProtocolVersion pv, byte haveRSA, byte havePSK,
  459. byte haveDH, byte haveNTRU, byte haveECDSAsig,
  460. byte haveStaticECC, int side)
  461. {
  462. word16 idx = 0;
  463. int tls = pv.major == SSLv3_MAJOR && pv.minor >= TLSv1_MINOR;
  464. int tls1_2 = pv.major == SSLv3_MAJOR && pv.minor >= TLSv1_2_MINOR;
  465. int haveRSAsig = 1;
  466. (void)tls; /* shut up compiler */
  467. (void)tls1_2;
  468. (void)haveDH;
  469. (void)havePSK;
  470. (void)haveNTRU;
  471. (void)haveStaticECC;
  472. if (suites == NULL) {
  473. CYASSL_MSG("InitSuites pointer error");
  474. return;
  475. }
  476. if (suites->setSuites)
  477. return; /* trust user settings, don't override */
  478. if (side == SERVER_END && haveStaticECC)
  479. haveRSA = 0; /* can't do RSA with ECDSA key */
  480. if (side == SERVER_END && haveECDSAsig) {
  481. haveRSAsig = 0; /* can't have RSA sig if signed by ECDSA */
  482. (void)haveRSAsig; /* non ecc builds won't read */
  483. }
  484. #ifdef CYASSL_DTLS
  485. if (pv.major == DTLS_MAJOR && pv.minor == DTLS_MINOR)
  486. tls = 1;
  487. #endif
  488. #ifdef BUILD_TLS_NTRU_RSA_WITH_AES_256_CBC_SHA
  489. if (tls && haveNTRU && haveRSA) {
  490. suites->suites[idx++] = 0;
  491. suites->suites[idx++] = TLS_NTRU_RSA_WITH_AES_256_CBC_SHA;
  492. }
  493. #endif
  494. #ifdef BUILD_TLS_NTRU_RSA_WITH_AES_128_CBC_SHA
  495. if (tls && haveNTRU && haveRSA) {
  496. suites->suites[idx++] = 0;
  497. suites->suites[idx++] = TLS_NTRU_RSA_WITH_AES_128_CBC_SHA;
  498. }
  499. #endif
  500. #ifdef BUILD_TLS_NTRU_RSA_WITH_RC4_128_SHA
  501. if (tls && haveNTRU && haveRSA) {
  502. suites->suites[idx++] = 0;
  503. suites->suites[idx++] = TLS_NTRU_RSA_WITH_RC4_128_SHA;
  504. }
  505. #endif
  506. #ifdef BUILD_TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA
  507. if (tls && haveNTRU && haveRSA) {
  508. suites->suites[idx++] = 0;
  509. suites->suites[idx++] = TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA;
  510. }
  511. #endif
  512. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
  513. if (tls1_2 && haveStaticECC) {
  514. suites->suites[idx++] = ECC_BYTE;
  515. suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384;
  516. }
  517. #endif
  518. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
  519. if (tls && haveStaticECC) {
  520. suites->suites[idx++] = ECC_BYTE;
  521. suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA;
  522. }
  523. #endif
  524. #ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
  525. if (tls1_2 && haveECDSAsig && haveStaticECC) {
  526. suites->suites[idx++] = ECC_BYTE;
  527. suites->suites[idx++] = TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384;
  528. }
  529. #endif
  530. #ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
  531. if (tls && haveECDSAsig && haveStaticECC) {
  532. suites->suites[idx++] = ECC_BYTE;
  533. suites->suites[idx++] = TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA;
  534. }
  535. #endif
  536. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
  537. if (tls1_2 && haveStaticECC) {
  538. suites->suites[idx++] = ECC_BYTE;
  539. suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256;
  540. }
  541. #endif
  542. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
  543. if (tls && haveStaticECC) {
  544. suites->suites[idx++] = ECC_BYTE;
  545. suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA;
  546. }
  547. #endif
  548. #ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
  549. if (tls1_2 && haveECDSAsig && haveStaticECC) {
  550. suites->suites[idx++] = ECC_BYTE;
  551. suites->suites[idx++] = TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256;
  552. }
  553. #endif
  554. #ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
  555. if (tls && haveECDSAsig && haveStaticECC) {
  556. suites->suites[idx++] = ECC_BYTE;
  557. suites->suites[idx++] = TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA;
  558. }
  559. #endif
  560. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
  561. if (tls && haveStaticECC) {
  562. suites->suites[idx++] = ECC_BYTE;
  563. suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_RC4_128_SHA;
  564. }
  565. #endif
  566. #ifdef BUILD_TLS_ECDH_ECDSA_WITH_RC4_128_SHA
  567. if (tls && haveECDSAsig && haveStaticECC) {
  568. suites->suites[idx++] = ECC_BYTE;
  569. suites->suites[idx++] = TLS_ECDH_ECDSA_WITH_RC4_128_SHA;
  570. }
  571. #endif
  572. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
  573. if (tls && haveStaticECC) {
  574. suites->suites[idx++] = ECC_BYTE;
  575. suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA;
  576. }
  577. #endif
  578. #ifdef BUILD_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
  579. if (tls && haveECDSAsig && haveStaticECC) {
  580. suites->suites[idx++] = ECC_BYTE;
  581. suites->suites[idx++] = TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA;
  582. }
  583. #endif
  584. #ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
  585. if (tls1_2 && haveRSA) {
  586. suites->suites[idx++] = ECC_BYTE;
  587. suites->suites[idx++] = TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384;
  588. }
  589. #endif
  590. #ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
  591. if (tls && haveRSA) {
  592. suites->suites[idx++] = ECC_BYTE;
  593. suites->suites[idx++] = TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA;
  594. }
  595. #endif
  596. #ifdef BUILD_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
  597. if (tls1_2 && haveRSAsig && haveStaticECC) {
  598. suites->suites[idx++] = ECC_BYTE;
  599. suites->suites[idx++] = TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384;
  600. }
  601. #endif
  602. #ifdef BUILD_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
  603. if (tls && haveRSAsig && haveStaticECC) {
  604. suites->suites[idx++] = ECC_BYTE;
  605. suites->suites[idx++] = TLS_ECDH_RSA_WITH_AES_256_CBC_SHA;
  606. }
  607. #endif
  608. #ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  609. if (tls1_2 && haveRSA) {
  610. suites->suites[idx++] = ECC_BYTE;
  611. suites->suites[idx++] = TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256;
  612. }
  613. #endif
  614. #ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
  615. if (tls && haveRSA) {
  616. suites->suites[idx++] = ECC_BYTE;
  617. suites->suites[idx++] = TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA;
  618. }
  619. #endif
  620. #ifdef BUILD_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
  621. if (tls1_2 && haveRSAsig && haveStaticECC) {
  622. suites->suites[idx++] = ECC_BYTE;
  623. suites->suites[idx++] = TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256;
  624. }
  625. #endif
  626. #ifdef BUILD_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
  627. if (tls && haveRSAsig && haveStaticECC) {
  628. suites->suites[idx++] = ECC_BYTE;
  629. suites->suites[idx++] = TLS_ECDH_RSA_WITH_AES_128_CBC_SHA;
  630. }
  631. #endif
  632. #ifdef BUILD_TLS_ECDHE_RSA_WITH_RC4_128_SHA
  633. if (tls && haveRSA) {
  634. suites->suites[idx++] = ECC_BYTE;
  635. suites->suites[idx++] = TLS_ECDHE_RSA_WITH_RC4_128_SHA;
  636. }
  637. #endif
  638. #ifdef BUILD_TLS_ECDH_RSA_WITH_RC4_128_SHA
  639. if (tls && haveRSAsig && haveStaticECC) {
  640. suites->suites[idx++] = ECC_BYTE;
  641. suites->suites[idx++] = TLS_ECDH_RSA_WITH_RC4_128_SHA;
  642. }
  643. #endif
  644. #ifdef BUILD_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
  645. if (tls && haveRSA) {
  646. suites->suites[idx++] = ECC_BYTE;
  647. suites->suites[idx++] = TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA;
  648. }
  649. #endif
  650. #ifdef BUILD_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
  651. if (tls && haveRSAsig && haveStaticECC) {
  652. suites->suites[idx++] = ECC_BYTE;
  653. suites->suites[idx++] = TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA;
  654. }
  655. #endif
  656. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
  657. if (tls1_2 && haveDH && haveRSA) {
  658. suites->suites[idx++] = 0;
  659. suites->suites[idx++] = TLS_DHE_RSA_WITH_AES_256_GCM_SHA384;
  660. }
  661. #endif
  662. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_SHA256
  663. if (tls1_2 && haveECDSAsig && haveDH) {
  664. suites->suites[idx++] = ECC_BYTE;
  665. suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_SHA256;
  666. }
  667. #endif
  668. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8_SHA384
  669. if (tls1_2 && haveECDSAsig && haveDH) {
  670. suites->suites[idx++] = ECC_BYTE;
  671. suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8_SHA384;
  672. }
  673. #endif
  674. #ifdef BUILD_TLS_RSA_WITH_AES_128_CCM_8_SHA256
  675. if (tls1_2 && haveRSA) {
  676. suites->suites[idx++] = ECC_BYTE;
  677. suites->suites[idx++] = TLS_RSA_WITH_AES_128_CCM_8_SHA256;
  678. }
  679. #endif
  680. #ifdef BUILD_TLS_RSA_WITH_AES_256_CCM_8_SHA384
  681. if (tls1_2 && haveRSA) {
  682. suites->suites[idx++] = ECC_BYTE;
  683. suites->suites[idx++] = TLS_RSA_WITH_AES_256_CCM_8_SHA384;
  684. }
  685. #endif
  686. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
  687. if (tls1_2 && haveDH && haveRSA) {
  688. suites->suites[idx++] = 0;
  689. suites->suites[idx++] = TLS_DHE_RSA_WITH_AES_256_CBC_SHA256;
  690. }
  691. #endif
  692. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
  693. if (tls1_2 && haveDH && haveRSA) {
  694. suites->suites[idx++] = 0;
  695. suites->suites[idx++] = TLS_DHE_RSA_WITH_AES_128_GCM_SHA256;
  696. }
  697. #endif
  698. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
  699. if (tls1_2 && haveDH && haveRSA) {
  700. suites->suites[idx++] = 0;
  701. suites->suites[idx++] = TLS_DHE_RSA_WITH_AES_128_CBC_SHA256;
  702. }
  703. #endif
  704. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA
  705. if (tls && haveDH && haveRSA) {
  706. suites->suites[idx++] = 0;
  707. suites->suites[idx++] = TLS_DHE_RSA_WITH_AES_256_CBC_SHA;
  708. }
  709. #endif
  710. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  711. if (tls && haveDH && haveRSA) {
  712. suites->suites[idx++] = 0;
  713. suites->suites[idx++] = TLS_DHE_RSA_WITH_AES_128_CBC_SHA;
  714. }
  715. #endif
  716. #ifdef BUILD_TLS_RSA_WITH_AES_256_GCM_SHA384
  717. if (tls1_2 && haveRSA) {
  718. suites->suites[idx++] = 0;
  719. suites->suites[idx++] = TLS_RSA_WITH_AES_256_GCM_SHA384;
  720. }
  721. #endif
  722. #ifdef BUILD_TLS_RSA_WITH_AES_256_CBC_SHA256
  723. if (tls1_2 && haveRSA) {
  724. suites->suites[idx++] = 0;
  725. suites->suites[idx++] = TLS_RSA_WITH_AES_256_CBC_SHA256;
  726. }
  727. #endif
  728. #ifdef BUILD_TLS_RSA_WITH_AES_128_GCM_SHA256
  729. if (tls1_2 && haveRSA) {
  730. suites->suites[idx++] = 0;
  731. suites->suites[idx++] = TLS_RSA_WITH_AES_128_GCM_SHA256;
  732. }
  733. #endif
  734. #ifdef BUILD_TLS_RSA_WITH_AES_128_CBC_SHA256
  735. if (tls1_2 && haveRSA) {
  736. suites->suites[idx++] = 0;
  737. suites->suites[idx++] = TLS_RSA_WITH_AES_128_CBC_SHA256;
  738. }
  739. #endif
  740. #ifdef BUILD_TLS_RSA_WITH_AES_256_CBC_SHA
  741. if (tls && haveRSA) {
  742. suites->suites[idx++] = 0;
  743. suites->suites[idx++] = TLS_RSA_WITH_AES_256_CBC_SHA;
  744. }
  745. #endif
  746. #ifdef BUILD_TLS_RSA_WITH_AES_128_CBC_SHA
  747. if (tls && haveRSA) {
  748. suites->suites[idx++] = 0;
  749. suites->suites[idx++] = TLS_RSA_WITH_AES_128_CBC_SHA;
  750. }
  751. #endif
  752. #ifdef BUILD_TLS_RSA_WITH_NULL_SHA
  753. if (tls && haveRSA) {
  754. suites->suites[idx++] = 0;
  755. suites->suites[idx++] = TLS_RSA_WITH_NULL_SHA;
  756. }
  757. #endif
  758. #ifdef BUILD_TLS_RSA_WITH_NULL_SHA256
  759. if (tls && haveRSA) {
  760. suites->suites[idx++] = 0;
  761. suites->suites[idx++] = TLS_RSA_WITH_NULL_SHA256;
  762. }
  763. #endif
  764. #ifdef BUILD_TLS_PSK_WITH_AES_256_CBC_SHA
  765. if (tls && havePSK) {
  766. suites->suites[idx++] = 0;
  767. suites->suites[idx++] = TLS_PSK_WITH_AES_256_CBC_SHA;
  768. }
  769. #endif
  770. #ifdef BUILD_TLS_PSK_WITH_AES_128_CBC_SHA256
  771. if (tls && havePSK) {
  772. suites->suites[idx++] = 0;
  773. suites->suites[idx++] = TLS_PSK_WITH_AES_128_CBC_SHA256;
  774. }
  775. #endif
  776. #ifdef BUILD_TLS_PSK_WITH_AES_128_CBC_SHA
  777. if (tls && havePSK) {
  778. suites->suites[idx++] = 0;
  779. suites->suites[idx++] = TLS_PSK_WITH_AES_128_CBC_SHA;
  780. }
  781. #endif
  782. #ifdef BUILD_TLS_PSK_WITH_NULL_SHA256
  783. if (tls & havePSK) {
  784. suites->suites[idx++] = 0;
  785. suites->suites[idx++] = TLS_PSK_WITH_NULL_SHA256;
  786. }
  787. #endif
  788. #ifdef BUILD_TLS_PSK_WITH_NULL_SHA
  789. if (tls & havePSK) {
  790. suites->suites[idx++] = 0;
  791. suites->suites[idx++] = TLS_PSK_WITH_NULL_SHA;
  792. }
  793. #endif
  794. #ifdef BUILD_SSL_RSA_WITH_RC4_128_SHA
  795. if (haveRSA ) {
  796. suites->suites[idx++] = 0;
  797. suites->suites[idx++] = SSL_RSA_WITH_RC4_128_SHA;
  798. }
  799. #endif
  800. #ifdef BUILD_SSL_RSA_WITH_RC4_128_MD5
  801. if (haveRSA ) {
  802. suites->suites[idx++] = 0;
  803. suites->suites[idx++] = SSL_RSA_WITH_RC4_128_MD5;
  804. }
  805. #endif
  806. #ifdef BUILD_SSL_RSA_WITH_3DES_EDE_CBC_SHA
  807. if (haveRSA ) {
  808. suites->suites[idx++] = 0;
  809. suites->suites[idx++] = SSL_RSA_WITH_3DES_EDE_CBC_SHA;
  810. }
  811. #endif
  812. #ifdef BUILD_TLS_RSA_WITH_HC_128_CBC_MD5
  813. if (tls && haveRSA) {
  814. suites->suites[idx++] = 0;
  815. suites->suites[idx++] = TLS_RSA_WITH_HC_128_CBC_MD5;
  816. }
  817. #endif
  818. #ifdef BUILD_TLS_RSA_WITH_HC_128_CBC_SHA
  819. if (tls && haveRSA) {
  820. suites->suites[idx++] = 0;
  821. suites->suites[idx++] = TLS_RSA_WITH_HC_128_CBC_SHA;
  822. }
  823. #endif
  824. #ifdef BUILD_TLS_RSA_WITH_RABBIT_CBC_SHA
  825. if (tls && haveRSA) {
  826. suites->suites[idx++] = 0;
  827. suites->suites[idx++] = TLS_RSA_WITH_RABBIT_CBC_SHA;
  828. }
  829. #endif
  830. #ifdef BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA
  831. if (tls && haveRSA) {
  832. suites->suites[idx++] = 0;
  833. suites->suites[idx++] = TLS_RSA_WITH_CAMELLIA_128_CBC_SHA;
  834. }
  835. #endif
  836. #ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA
  837. if (tls && haveDH && haveRSA) {
  838. suites->suites[idx++] = 0;
  839. suites->suites[idx++] = TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA;
  840. }
  841. #endif
  842. #ifdef BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA
  843. if (tls && haveRSA) {
  844. suites->suites[idx++] = 0;
  845. suites->suites[idx++] = TLS_RSA_WITH_CAMELLIA_256_CBC_SHA;
  846. }
  847. #endif
  848. #ifdef BUILD_TLS_DHE_WITH_RSA_CAMELLIA_256_CBC_SHA
  849. if (tls && haveDH && haveRSA) {
  850. suites->suites[idx++] = 0;
  851. suites->suites[idx++] = TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA;
  852. }
  853. #endif
  854. #ifdef BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256
  855. if (tls && haveRSA) {
  856. suites->suites[idx++] = 0;
  857. suites->suites[idx++] = TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256;
  858. }
  859. #endif
  860. #ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256
  861. if (tls && haveDH && haveRSA) {
  862. suites->suites[idx++] = 0;
  863. suites->suites[idx++] = TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256;
  864. }
  865. #endif
  866. #ifdef BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256
  867. if (tls && haveRSA) {
  868. suites->suites[idx++] = 0;
  869. suites->suites[idx++] = TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256;
  870. }
  871. #endif
  872. #ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256
  873. if (tls && haveDH && haveRSA) {
  874. suites->suites[idx++] = 0;
  875. suites->suites[idx++] = TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256;
  876. }
  877. #endif
  878. suites->suiteSz = idx;
  879. }
  880. /* init everything to 0, NULL, default values before calling anything that may
  881. fail so that desctructor has a "good" state to cleanup */
  882. int InitSSL(CYASSL* ssl, CYASSL_CTX* ctx)
  883. {
  884. int ret;
  885. byte haveRSA = 0;
  886. byte havePSK = 0;
  887. ssl->ctx = ctx; /* only for passing to calls, options could change */
  888. ssl->version = ctx->method->version;
  889. ssl->suites = NULL;
  890. #ifdef HAVE_LIBZ
  891. ssl->didStreamInit = 0;
  892. #endif
  893. #ifndef NO_RSA
  894. haveRSA = 1;
  895. #endif
  896. #ifndef NO_CERTS
  897. ssl->buffers.certificate.buffer = 0;
  898. ssl->buffers.key.buffer = 0;
  899. ssl->buffers.certChain.buffer = 0;
  900. #endif
  901. ssl->buffers.inputBuffer.length = 0;
  902. ssl->buffers.inputBuffer.idx = 0;
  903. ssl->buffers.inputBuffer.buffer = ssl->buffers.inputBuffer.staticBuffer;
  904. ssl->buffers.inputBuffer.bufferSize = STATIC_BUFFER_LEN;
  905. ssl->buffers.inputBuffer.dynamicFlag = 0;
  906. ssl->buffers.outputBuffer.length = 0;
  907. ssl->buffers.outputBuffer.idx = 0;
  908. ssl->buffers.outputBuffer.buffer = ssl->buffers.outputBuffer.staticBuffer;
  909. ssl->buffers.outputBuffer.bufferSize = STATIC_BUFFER_LEN;
  910. ssl->buffers.outputBuffer.dynamicFlag = 0;
  911. ssl->buffers.domainName.buffer = 0;
  912. #ifndef NO_CERTS
  913. ssl->buffers.serverDH_P.buffer = 0;
  914. ssl->buffers.serverDH_G.buffer = 0;
  915. ssl->buffers.serverDH_Pub.buffer = 0;
  916. ssl->buffers.serverDH_Priv.buffer = 0;
  917. #endif
  918. ssl->buffers.clearOutputBuffer.buffer = 0;
  919. ssl->buffers.clearOutputBuffer.length = 0;
  920. ssl->buffers.prevSent = 0;
  921. ssl->buffers.plainSz = 0;
  922. #ifdef OPENSSL_EXTRA
  923. ssl->peerCert.derCert.buffer = NULL;
  924. ssl->peerCert.altNames = NULL;
  925. ssl->peerCert.altNamesNext = NULL;
  926. #endif
  927. #ifdef HAVE_ECC
  928. ssl->eccTempKeySz = ctx->eccTempKeySz;
  929. ssl->peerEccKeyPresent = 0;
  930. ssl->peerEccDsaKeyPresent = 0;
  931. ssl->eccDsaKeyPresent = 0;
  932. ssl->eccTempKeyPresent = 0;
  933. ssl->peerEccKey = NULL;
  934. ssl->peerEccDsaKey = NULL;
  935. ssl->eccDsaKey = NULL;
  936. ssl->eccTempKey = NULL;
  937. #endif
  938. ssl->timeout = ctx->timeout;
  939. ssl->rfd = -1; /* set to invalid descriptor */
  940. ssl->wfd = -1;
  941. ssl->rflags = 0; /* no user flags yet */
  942. ssl->wflags = 0; /* no user flags yet */
  943. ssl->biord = 0;
  944. ssl->biowr = 0;
  945. ssl->IOCB_ReadCtx = &ssl->rfd; /* prevent invalid pointer access if not */
  946. ssl->IOCB_WriteCtx = &ssl->wfd; /* correctly set */
  947. #ifndef NO_MD5
  948. InitMd5(&ssl->hashMd5);
  949. #endif
  950. InitSha(&ssl->hashSha);
  951. #ifndef NO_SHA256
  952. InitSha256(&ssl->hashSha256);
  953. #endif
  954. #ifdef CYASSL_SHA384
  955. InitSha384(&ssl->hashSha384);
  956. #endif
  957. #ifndef NO_RSA
  958. ssl->peerRsaKey = NULL;
  959. ssl->peerRsaKeyPresent = 0;
  960. #endif
  961. ssl->verifyCallback = ctx->verifyCallback;
  962. ssl->options.side = ctx->method->side;
  963. ssl->options.downgrade = ctx->method->downgrade;
  964. ssl->error = 0;
  965. ssl->options.connReset = 0;
  966. ssl->options.isClosed = 0;
  967. ssl->options.closeNotify = 0;
  968. ssl->options.sentNotify = 0;
  969. ssl->options.usingCompression = 0;
  970. if (ssl->options.side == SERVER_END)
  971. ssl->options.haveDH = ctx->haveDH;
  972. else
  973. ssl->options.haveDH = 0;
  974. ssl->options.haveNTRU = ctx->haveNTRU;
  975. ssl->options.haveECDSAsig = ctx->haveECDSAsig;
  976. ssl->options.haveStaticECC = ctx->haveStaticECC;
  977. ssl->options.havePeerCert = 0;
  978. ssl->options.usingPSK_cipher = 0;
  979. ssl->options.sendAlertState = 0;
  980. #ifndef NO_PSK
  981. havePSK = ctx->havePSK;
  982. ssl->options.havePSK = ctx->havePSK;
  983. ssl->options.client_psk_cb = ctx->client_psk_cb;
  984. ssl->options.server_psk_cb = ctx->server_psk_cb;
  985. #endif /* NO_PSK */
  986. ssl->options.serverState = NULL_STATE;
  987. ssl->options.clientState = NULL_STATE;
  988. ssl->options.connectState = CONNECT_BEGIN;
  989. ssl->options.acceptState = ACCEPT_BEGIN;
  990. ssl->options.handShakeState = NULL_STATE;
  991. ssl->options.processReply = doProcessInit;
  992. #ifdef CYASSL_DTLS
  993. ssl->keys.dtls_sequence_number = 0;
  994. ssl->keys.dtls_peer_sequence_number = 0;
  995. ssl->keys.dtls_expected_peer_sequence_number = 0;
  996. ssl->keys.dtls_handshake_number = 0;
  997. ssl->keys.dtls_expected_peer_handshake_number = 0;
  998. ssl->keys.dtls_epoch = 0;
  999. ssl->keys.dtls_peer_epoch = 0;
  1000. ssl->keys.dtls_expected_peer_epoch = 0;
  1001. ssl->dtls_timeout = DTLS_DEFAULT_TIMEOUT;
  1002. ssl->dtls_pool = NULL;
  1003. #endif
  1004. ssl->keys.encryptionOn = 0; /* initially off */
  1005. ssl->keys.decryptedCur = 0; /* initially off */
  1006. ssl->options.sessionCacheOff = ctx->sessionCacheOff;
  1007. ssl->options.sessionCacheFlushOff = ctx->sessionCacheFlushOff;
  1008. ssl->options.verifyPeer = ctx->verifyPeer;
  1009. ssl->options.verifyNone = ctx->verifyNone;
  1010. ssl->options.failNoCert = ctx->failNoCert;
  1011. ssl->options.sendVerify = ctx->sendVerify;
  1012. ssl->options.resuming = 0;
  1013. ssl->options.haveSessionId = 0;
  1014. #ifndef NO_OLD_TLS
  1015. ssl->hmac = Hmac; /* default to SSLv3 */
  1016. #else
  1017. ssl->hmac = TLS_hmac;
  1018. #endif
  1019. ssl->heap = ctx->heap; /* defaults to self */
  1020. ssl->options.tls = 0;
  1021. ssl->options.tls1_1 = 0;
  1022. if (ssl->version.major == DTLS_MAJOR && ssl->version.minor == DTLS_MINOR)
  1023. ssl->options.dtls = 1;
  1024. else
  1025. ssl->options.dtls = 0;
  1026. ssl->options.partialWrite = ctx->partialWrite;
  1027. ssl->options.quietShutdown = ctx->quietShutdown;
  1028. ssl->options.certOnly = 0;
  1029. ssl->options.groupMessages = ctx->groupMessages;
  1030. ssl->options.usingNonblock = 0;
  1031. ssl->options.saveArrays = 0;
  1032. #ifndef NO_CERTS
  1033. /* ctx still owns certificate, certChain, key, dh, and cm */
  1034. ssl->buffers.certificate = ctx->certificate;
  1035. ssl->buffers.certChain = ctx->certChain;
  1036. ssl->buffers.key = ctx->privateKey;
  1037. if (ssl->options.side == SERVER_END) {
  1038. ssl->buffers.serverDH_P = ctx->serverDH_P;
  1039. ssl->buffers.serverDH_G = ctx->serverDH_G;
  1040. }
  1041. #endif
  1042. ssl->buffers.weOwnCert = 0;
  1043. ssl->buffers.weOwnKey = 0;
  1044. ssl->buffers.weOwnDH = 0;
  1045. #ifdef CYASSL_DTLS
  1046. ssl->buffers.dtlsHandshake.length = 0;
  1047. ssl->buffers.dtlsHandshake.buffer = NULL;
  1048. ssl->buffers.dtlsType = 0;
  1049. ssl->buffers.dtlsCtx.fd = -1;
  1050. ssl->buffers.dtlsCtx.peer.sa = NULL;
  1051. ssl->buffers.dtlsCtx.peer.sz = 0;
  1052. #endif
  1053. #ifdef OPENSSL_EXTRA
  1054. ssl->peerCert.issuer.sz = 0;
  1055. ssl->peerCert.subject.sz = 0;
  1056. #endif
  1057. #ifdef SESSION_CERTS
  1058. ssl->session.chain.count = 0;
  1059. #endif
  1060. ssl->cipher.ssl = ssl;
  1061. #ifdef FORTRESS
  1062. ssl->ex_data[0] = 0;
  1063. ssl->ex_data[1] = 0;
  1064. ssl->ex_data[2] = 0;
  1065. #endif
  1066. #ifdef CYASSL_CALLBACKS
  1067. ssl->hsInfoOn = 0;
  1068. ssl->toInfoOn = 0;
  1069. #endif
  1070. #ifdef HAVE_CAVIUM
  1071. ssl->devId = ctx->devId;
  1072. #endif
  1073. ssl->rng = NULL;
  1074. ssl->arrays = NULL;
  1075. InitCiphers(ssl);
  1076. /* all done with init, now can return errors, call other stuff */
  1077. /* increment CTX reference count */
  1078. if (LockMutex(&ctx->countMutex) != 0) {
  1079. CYASSL_MSG("Couldn't lock CTX count mutex");
  1080. return BAD_MUTEX_ERROR;
  1081. }
  1082. ctx->refCount++;
  1083. UnLockMutex(&ctx->countMutex);
  1084. /* arrays */
  1085. ssl->arrays = (Arrays*)XMALLOC(sizeof(Arrays), ssl->heap,
  1086. DYNAMIC_TYPE_ARRAYS);
  1087. if (ssl->arrays == NULL) {
  1088. CYASSL_MSG("Arrays Memory error");
  1089. return MEMORY_E;
  1090. }
  1091. #ifndef NO_PSK
  1092. ssl->arrays->client_identity[0] = 0;
  1093. if (ctx->server_hint[0]) { /* set in CTX */
  1094. XSTRNCPY(ssl->arrays->server_hint, ctx->server_hint, MAX_PSK_ID_LEN);
  1095. ssl->arrays->server_hint[MAX_PSK_ID_LEN - 1] = '\0';
  1096. }
  1097. else
  1098. ssl->arrays->server_hint[0] = 0;
  1099. #endif /* NO_PSK */
  1100. #ifdef CYASSL_DTLS
  1101. ssl->arrays->cookieSz = 0;
  1102. #endif
  1103. /* RNG */
  1104. ssl->rng = (RNG*)XMALLOC(sizeof(RNG), ssl->heap, DYNAMIC_TYPE_RNG);
  1105. if (ssl->rng == NULL) {
  1106. CYASSL_MSG("RNG Memory error");
  1107. return MEMORY_E;
  1108. }
  1109. if ( (ret = InitRng(ssl->rng)) != 0)
  1110. return ret;
  1111. /* suites */
  1112. ssl->suites = (Suites*)XMALLOC(sizeof(Suites), ssl->heap,
  1113. DYNAMIC_TYPE_SUITES);
  1114. if (ssl->suites == NULL) {
  1115. CYASSL_MSG("Suites Memory error");
  1116. return MEMORY_E;
  1117. }
  1118. *ssl->suites = ctx->suites;
  1119. /* peer key */
  1120. #ifndef NO_RSA
  1121. ssl->peerRsaKey = (RsaKey*)XMALLOC(sizeof(RsaKey), ssl->heap,
  1122. DYNAMIC_TYPE_RSA);
  1123. if (ssl->peerRsaKey == NULL) {
  1124. CYASSL_MSG("PeerRsaKey Memory error");
  1125. return MEMORY_E;
  1126. }
  1127. InitRsaKey(ssl->peerRsaKey, ctx->heap);
  1128. #endif
  1129. #ifndef NO_CERTS
  1130. /* make sure server has cert and key unless using PSK */
  1131. if (ssl->options.side == SERVER_END && !havePSK)
  1132. if (!ssl->buffers.certificate.buffer || !ssl->buffers.key.buffer) {
  1133. CYASSL_MSG("Server missing certificate and/or private key");
  1134. return NO_PRIVATE_KEY;
  1135. }
  1136. #endif
  1137. #ifdef HAVE_ECC
  1138. ssl->peerEccKey = (ecc_key*)XMALLOC(sizeof(ecc_key),
  1139. ctx->heap, DYNAMIC_TYPE_ECC);
  1140. if (ssl->peerEccKey == NULL) {
  1141. CYASSL_MSG("PeerEccKey Memory error");
  1142. return MEMORY_E;
  1143. }
  1144. ssl->peerEccDsaKey = (ecc_key*)XMALLOC(sizeof(ecc_key),
  1145. ctx->heap, DYNAMIC_TYPE_ECC);
  1146. if (ssl->peerEccDsaKey == NULL) {
  1147. CYASSL_MSG("PeerEccDsaKey Memory error");
  1148. return MEMORY_E;
  1149. }
  1150. ssl->eccDsaKey = (ecc_key*)XMALLOC(sizeof(ecc_key),
  1151. ctx->heap, DYNAMIC_TYPE_ECC);
  1152. if (ssl->eccDsaKey == NULL) {
  1153. CYASSL_MSG("EccDsaKey Memory error");
  1154. return MEMORY_E;
  1155. }
  1156. ssl->eccTempKey = (ecc_key*)XMALLOC(sizeof(ecc_key),
  1157. ctx->heap, DYNAMIC_TYPE_ECC);
  1158. if (ssl->eccTempKey == NULL) {
  1159. CYASSL_MSG("EccTempKey Memory error");
  1160. return MEMORY_E;
  1161. }
  1162. ecc_init(ssl->peerEccKey);
  1163. ecc_init(ssl->peerEccDsaKey);
  1164. ecc_init(ssl->eccDsaKey);
  1165. ecc_init(ssl->eccTempKey);
  1166. #endif
  1167. /* make sure server has DH parms, and add PSK if there, add NTRU too */
  1168. if (ssl->options.side == SERVER_END)
  1169. InitSuites(ssl->suites, ssl->version, haveRSA, havePSK,
  1170. ssl->options.haveDH, ssl->options.haveNTRU,
  1171. ssl->options.haveECDSAsig, ssl->options.haveStaticECC,
  1172. ssl->options.side);
  1173. else
  1174. InitSuites(ssl->suites, ssl->version, haveRSA, havePSK, TRUE,
  1175. ssl->options.haveNTRU, ssl->options.haveECDSAsig,
  1176. ssl->options.haveStaticECC, ssl->options.side);
  1177. return 0;
  1178. }
  1179. /* free use of temporary arrays */
  1180. void FreeArrays(CYASSL* ssl, int keep)
  1181. {
  1182. if (ssl->arrays && keep) {
  1183. /* keeps session id for user retrieval */
  1184. XMEMCPY(ssl->session.sessionID, ssl->arrays->sessionID, ID_LEN);
  1185. }
  1186. XFREE(ssl->arrays, ssl->heap, DYNAMIC_TYPE_ARRAYS);
  1187. ssl->arrays = NULL;
  1188. }
  1189. /* In case holding SSL object in array and don't want to free actual ssl */
  1190. void SSL_ResourceFree(CYASSL* ssl)
  1191. {
  1192. FreeCiphers(ssl);
  1193. FreeArrays(ssl, 0);
  1194. XFREE(ssl->rng, ssl->heap, DYNAMIC_TYPE_RNG);
  1195. XFREE(ssl->suites, ssl->heap, DYNAMIC_TYPE_SUITES);
  1196. XFREE(ssl->buffers.domainName.buffer, ssl->heap, DYNAMIC_TYPE_DOMAIN);
  1197. #ifndef NO_CERTS
  1198. XFREE(ssl->buffers.serverDH_Priv.buffer, ssl->heap, DYNAMIC_TYPE_DH);
  1199. XFREE(ssl->buffers.serverDH_Pub.buffer, ssl->heap, DYNAMIC_TYPE_DH);
  1200. /* parameters (p,g) may be owned by ctx */
  1201. if (ssl->buffers.weOwnDH || ssl->options.side == CLIENT_END) {
  1202. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap, DYNAMIC_TYPE_DH);
  1203. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap, DYNAMIC_TYPE_DH);
  1204. }
  1205. /* CYASSL_CTX always owns certChain */
  1206. if (ssl->buffers.weOwnCert)
  1207. XFREE(ssl->buffers.certificate.buffer, ssl->heap, DYNAMIC_TYPE_CERT);
  1208. if (ssl->buffers.weOwnKey)
  1209. XFREE(ssl->buffers.key.buffer, ssl->heap, DYNAMIC_TYPE_KEY);
  1210. #endif
  1211. #ifndef NO_RSA
  1212. if (ssl->peerRsaKey) {
  1213. FreeRsaKey(ssl->peerRsaKey);
  1214. XFREE(ssl->peerRsaKey, ssl->heap, DYNAMIC_TYPE_RSA);
  1215. }
  1216. #endif
  1217. if (ssl->buffers.inputBuffer.dynamicFlag)
  1218. ShrinkInputBuffer(ssl, FORCED_FREE);
  1219. if (ssl->buffers.outputBuffer.dynamicFlag)
  1220. ShrinkOutputBuffer(ssl);
  1221. #ifdef CYASSL_DTLS
  1222. if (ssl->buffers.dtlsHandshake.buffer != NULL)
  1223. XFREE(ssl->buffers.dtlsHandshake.buffer, ssl->heap, DYNAMIC_TYPE_NONE);
  1224. if (ssl->dtls_pool != NULL) {
  1225. DtlsPoolReset(ssl);
  1226. XFREE(ssl->dtls_pool, ssl->heap, DYNAMIC_TYPE_NONE);
  1227. }
  1228. XFREE(ssl->buffers.dtlsCtx.peer.sa, ssl->heap, DYNAMIC_TYPE_SOCKADDR);
  1229. ssl->buffers.dtlsCtx.peer.sa = NULL;
  1230. #endif
  1231. #if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)
  1232. XFREE(ssl->peerCert.derCert.buffer, ssl->heap, DYNAMIC_TYPE_CERT);
  1233. if (ssl->peerCert.altNames)
  1234. FreeAltNames(ssl->peerCert.altNames, ssl->heap);
  1235. CyaSSL_BIO_free(ssl->biord);
  1236. if (ssl->biord != ssl->biowr) /* in case same as write */
  1237. CyaSSL_BIO_free(ssl->biowr);
  1238. #endif
  1239. #ifdef HAVE_LIBZ
  1240. FreeStreams(ssl);
  1241. #endif
  1242. #ifdef HAVE_ECC
  1243. if (ssl->peerEccKey) {
  1244. if (ssl->peerEccKeyPresent)
  1245. ecc_free(ssl->peerEccKey);
  1246. XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
  1247. }
  1248. if (ssl->peerEccDsaKey) {
  1249. if (ssl->peerEccDsaKeyPresent)
  1250. ecc_free(ssl->peerEccDsaKey);
  1251. XFREE(ssl->peerEccDsaKey, ssl->heap, DYNAMIC_TYPE_ECC);
  1252. }
  1253. if (ssl->eccTempKey) {
  1254. if (ssl->eccTempKeyPresent)
  1255. ecc_free(ssl->eccTempKey);
  1256. XFREE(ssl->eccTempKey, ssl->heap, DYNAMIC_TYPE_ECC);
  1257. }
  1258. if (ssl->eccDsaKey) {
  1259. if (ssl->eccDsaKeyPresent)
  1260. ecc_free(ssl->eccDsaKey);
  1261. XFREE(ssl->eccDsaKey, ssl->heap, DYNAMIC_TYPE_ECC);
  1262. }
  1263. #endif
  1264. }
  1265. /* Free any handshake resources no longer needed */
  1266. void FreeHandshakeResources(CYASSL* ssl)
  1267. {
  1268. /* input buffer */
  1269. if (ssl->buffers.inputBuffer.dynamicFlag)
  1270. ShrinkInputBuffer(ssl, NO_FORCED_FREE);
  1271. /* suites */
  1272. XFREE(ssl->suites, ssl->heap, DYNAMIC_TYPE_SUITES);
  1273. ssl->suites = NULL;
  1274. /* RNG */
  1275. if (ssl->specs.cipher_type == stream || ssl->options.tls1_1 == 0) {
  1276. XFREE(ssl->rng, ssl->heap, DYNAMIC_TYPE_RNG);
  1277. ssl->rng = NULL;
  1278. }
  1279. #ifdef CYASSL_DTLS
  1280. /* DTLS_POOL */
  1281. if (ssl->options.dtls && ssl->dtls_pool != NULL) {
  1282. DtlsPoolReset(ssl);
  1283. XFREE(ssl->dtls_pool, ssl->heap, DYNAMIC_TYPE_DTLS_POOL);
  1284. ssl->dtls_pool = NULL;
  1285. }
  1286. #endif
  1287. /* arrays */
  1288. if (ssl->options.saveArrays)
  1289. FreeArrays(ssl, 1);
  1290. #ifndef NO_RSA
  1291. /* peerRsaKey */
  1292. if (ssl->peerRsaKey) {
  1293. FreeRsaKey(ssl->peerRsaKey);
  1294. XFREE(ssl->peerRsaKey, ssl->heap, DYNAMIC_TYPE_RSA);
  1295. ssl->peerRsaKey = NULL;
  1296. }
  1297. #endif
  1298. #ifdef HAVE_ECC
  1299. if (ssl->peerEccKey)
  1300. {
  1301. if (ssl->peerEccKeyPresent) {
  1302. ecc_free(ssl->peerEccKey);
  1303. ssl->peerEccKeyPresent = 0;
  1304. }
  1305. XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
  1306. ssl->peerEccKey = NULL;
  1307. }
  1308. if (ssl->peerEccDsaKey)
  1309. {
  1310. if (ssl->peerEccDsaKeyPresent) {
  1311. ecc_free(ssl->peerEccDsaKey);
  1312. ssl->peerEccDsaKeyPresent = 0;
  1313. }
  1314. XFREE(ssl->peerEccDsaKey, ssl->heap, DYNAMIC_TYPE_ECC);
  1315. ssl->peerEccDsaKey = NULL;
  1316. }
  1317. if (ssl->eccTempKey)
  1318. {
  1319. if (ssl->eccTempKeyPresent) {
  1320. ecc_free(ssl->eccTempKey);
  1321. ssl->eccTempKeyPresent = 0;
  1322. }
  1323. XFREE(ssl->eccTempKey, ssl->heap, DYNAMIC_TYPE_ECC);
  1324. ssl->eccTempKey = NULL;
  1325. }
  1326. if (ssl->eccDsaKey)
  1327. {
  1328. if (ssl->eccDsaKeyPresent) {
  1329. ecc_free(ssl->eccDsaKey);
  1330. ssl->eccDsaKeyPresent = 0;
  1331. }
  1332. XFREE(ssl->eccDsaKey, ssl->heap, DYNAMIC_TYPE_ECC);
  1333. ssl->eccDsaKey = NULL;
  1334. }
  1335. #endif
  1336. }
  1337. void FreeSSL(CYASSL* ssl)
  1338. {
  1339. FreeSSL_Ctx(ssl->ctx); /* will decrement and free underyling CTX if 0 */
  1340. SSL_ResourceFree(ssl);
  1341. XFREE(ssl, ssl->heap, DYNAMIC_TYPE_SSL);
  1342. }
  1343. #ifdef CYASSL_DTLS
  1344. int DtlsPoolInit(CYASSL* ssl)
  1345. {
  1346. if (ssl->dtls_pool == NULL) {
  1347. DtlsPool *pool = (DtlsPool*)XMALLOC(sizeof(DtlsPool),
  1348. ssl->heap, DYNAMIC_TYPE_DTLS_POOL);
  1349. if (pool == NULL) {
  1350. CYASSL_MSG("DTLS Buffer Pool Memory error");
  1351. return MEMORY_E;
  1352. }
  1353. else {
  1354. int i;
  1355. for (i = 0; i < DTLS_POOL_SZ; i++) {
  1356. pool->buf[i].length = 0;
  1357. pool->buf[i].buffer = NULL;
  1358. }
  1359. pool->used = 0;
  1360. ssl->dtls_pool = pool;
  1361. }
  1362. }
  1363. return 0;
  1364. }
  1365. int DtlsPoolSave(CYASSL* ssl, const byte *src, int sz)
  1366. {
  1367. DtlsPool *pool = ssl->dtls_pool;
  1368. if (pool != NULL && pool->used < DTLS_POOL_SZ) {
  1369. buffer *pBuf = &pool->buf[pool->used];
  1370. pBuf->buffer = (byte*)XMALLOC(sz, ssl->heap, DYNAMIC_TYPE_OUT_BUFFER);
  1371. if (pBuf->buffer == NULL) {
  1372. CYASSL_MSG("DTLS Buffer Memory error");
  1373. return MEMORY_ERROR;
  1374. }
  1375. XMEMCPY(pBuf->buffer, src, sz);
  1376. pBuf->length = (word32)sz;
  1377. pool->used++;
  1378. }
  1379. return 0;
  1380. }
  1381. void DtlsPoolReset(CYASSL* ssl)
  1382. {
  1383. DtlsPool *pool = ssl->dtls_pool;
  1384. if (pool != NULL) {
  1385. buffer *pBuf;
  1386. int i, used;
  1387. used = pool->used;
  1388. for (i = 0, pBuf = &pool->buf[0]; i < used; i++, pBuf++) {
  1389. XFREE(pBuf->buffer, ssl->heap, DYNAMIC_TYPE_OUT_BUFFER);
  1390. pBuf->buffer = NULL;
  1391. pBuf->length = 0;
  1392. }
  1393. pool->used = 0;
  1394. }
  1395. ssl->dtls_timeout = DTLS_DEFAULT_TIMEOUT;
  1396. }
  1397. int DtlsPoolTimeout(CYASSL* ssl)
  1398. {
  1399. int result = -1;
  1400. if (ssl->dtls_timeout < 64) {
  1401. ssl->dtls_timeout *= 2;
  1402. result = 0;
  1403. }
  1404. return result;
  1405. }
  1406. int DtlsPoolSend(CYASSL* ssl)
  1407. {
  1408. DtlsPool *pool = ssl->dtls_pool;
  1409. if (pool != NULL && pool->used > 0) {
  1410. int i;
  1411. for (i = 0; i < pool->used; i++) {
  1412. int sendResult;
  1413. buffer* buf = &pool->buf[i];
  1414. DtlsRecordLayerHeader* dtls = (DtlsRecordLayerHeader*)buf->buffer;
  1415. if (dtls->type == change_cipher_spec) {
  1416. ssl->keys.dtls_epoch++;
  1417. ssl->keys.dtls_sequence_number = 0;
  1418. }
  1419. c16toa(ssl->keys.dtls_epoch, dtls->epoch);
  1420. c32to48(ssl->keys.dtls_sequence_number++, dtls->sequence_number);
  1421. XMEMCPY(ssl->buffers.outputBuffer.buffer, buf->buffer, buf->length);
  1422. ssl->buffers.outputBuffer.idx = 0;
  1423. ssl->buffers.outputBuffer.length = buf->length;
  1424. sendResult = SendBuffered(ssl);
  1425. if (sendResult < 0) {
  1426. return sendResult;
  1427. }
  1428. }
  1429. }
  1430. return 0;
  1431. }
  1432. #endif
  1433. #ifndef NO_OLD_TLS
  1434. ProtocolVersion MakeSSLv3(void)
  1435. {
  1436. ProtocolVersion pv;
  1437. pv.major = SSLv3_MAJOR;
  1438. pv.minor = SSLv3_MINOR;
  1439. return pv;
  1440. }
  1441. #endif /* NO_OLD_TLS */
  1442. #ifdef CYASSL_DTLS
  1443. ProtocolVersion MakeDTLSv1(void)
  1444. {
  1445. ProtocolVersion pv;
  1446. pv.major = DTLS_MAJOR;
  1447. pv.minor = DTLS_MINOR;
  1448. return pv;
  1449. }
  1450. #endif /* CYASSL_DTLS */
  1451. #ifdef USE_WINDOWS_API
  1452. timer_d Timer(void)
  1453. {
  1454. static int init = 0;
  1455. static LARGE_INTEGER freq;
  1456. LARGE_INTEGER count;
  1457. if (!init) {
  1458. QueryPerformanceFrequency(&freq);
  1459. init = 1;
  1460. }
  1461. QueryPerformanceCounter(&count);
  1462. return (double)count.QuadPart / freq.QuadPart;
  1463. }
  1464. word32 LowResTimer(void)
  1465. {
  1466. return (word32)Timer();
  1467. }
  1468. #elif defined(THREADX)
  1469. #include "rtptime.h"
  1470. word32 LowResTimer(void)
  1471. {
  1472. return (word32)rtp_get_system_sec();
  1473. }
  1474. #elif defined(MICRIUM)
  1475. word32 LowResTimer(void)
  1476. {
  1477. NET_SECURE_OS_TICK clk;
  1478. #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
  1479. clk = NetSecure_OS_TimeGet();
  1480. #endif
  1481. return (word32)clk;
  1482. }
  1483. #elif defined(USER_TICKS)
  1484. word32 LowResTimer(void)
  1485. {
  1486. /*
  1487. write your own clock tick function if don't want time(0)
  1488. needs second accuracy but doesn't have to correlated to EPOCH
  1489. */
  1490. }
  1491. #else /* !USE_WINDOWS_API && !THREADX && !MICRIUM && !USER_TICKS */
  1492. #include <time.h>
  1493. word32 LowResTimer(void)
  1494. {
  1495. return (word32)time(0);
  1496. }
  1497. #endif /* USE_WINDOWS_API */
  1498. /* add output to md5 and sha handshake hashes, exclude record header */
  1499. static void HashOutput(CYASSL* ssl, const byte* output, int sz, int ivSz)
  1500. {
  1501. const byte* adj = output + RECORD_HEADER_SZ + ivSz;
  1502. sz -= RECORD_HEADER_SZ;
  1503. #ifdef CYASSL_DTLS
  1504. if (ssl->options.dtls) {
  1505. adj += DTLS_RECORD_EXTRA;
  1506. sz -= DTLS_RECORD_EXTRA;
  1507. }
  1508. #endif
  1509. ShaUpdate(&ssl->hashSha, adj, sz);
  1510. #ifndef NO_MD5
  1511. Md5Update(&ssl->hashMd5, adj, sz);
  1512. #endif
  1513. if (IsAtLeastTLSv1_2(ssl)) {
  1514. #ifndef NO_SHA256
  1515. Sha256Update(&ssl->hashSha256, adj, sz);
  1516. #endif
  1517. #ifdef CYASSL_SHA384
  1518. Sha384Update(&ssl->hashSha384, adj, sz);
  1519. #endif
  1520. }
  1521. }
  1522. /* add input to md5 and sha handshake hashes, include handshake header */
  1523. static void HashInput(CYASSL* ssl, const byte* input, int sz)
  1524. {
  1525. const byte* adj = input - HANDSHAKE_HEADER_SZ;
  1526. sz += HANDSHAKE_HEADER_SZ;
  1527. #ifdef CYASSL_DTLS
  1528. if (ssl->options.dtls) {
  1529. adj -= DTLS_HANDSHAKE_EXTRA;
  1530. sz += DTLS_HANDSHAKE_EXTRA;
  1531. }
  1532. #endif
  1533. ShaUpdate(&ssl->hashSha, adj, sz);
  1534. #ifndef NO_MD5
  1535. Md5Update(&ssl->hashMd5, adj, sz);
  1536. #endif
  1537. if (IsAtLeastTLSv1_2(ssl)) {
  1538. #ifndef NO_SHA256
  1539. Sha256Update(&ssl->hashSha256, adj, sz);
  1540. #endif
  1541. #ifdef CYASSL_SHA384
  1542. Sha384Update(&ssl->hashSha384, adj, sz);
  1543. #endif
  1544. }
  1545. }
  1546. /* add record layer header for message */
  1547. stati

Large files files are truncated, but you can click here to view the full file