PageRenderTime 88ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/src/ssl.c

https://github.com/andersmalm/cyassl
C | 8294 lines | 7910 code | 303 blank | 81 comment | 342 complexity | 47672171ab4876142cbdb34145c48e6a MD5 | raw file
Possible License(s): GPL-2.0
  1. /* ssl.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. #ifdef HAVE_ERRNO_H
  25. #include <errno.h>
  26. #endif
  27. #define TRUE 1
  28. #define FALSE 0
  29. #include <cyassl/ssl.h>
  30. #include <cyassl/internal.h>
  31. #include <cyassl/error.h>
  32. #include <cyassl/ctaocrypt/coding.h>
  33. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  34. #include <cyassl/openssl/evp.h>
  35. #endif
  36. #ifdef OPENSSL_EXTRA
  37. /* openssl headers begin */
  38. #include <cyassl/openssl/hmac.h>
  39. #include <cyassl/openssl/crypto.h>
  40. #include <cyassl/openssl/des.h>
  41. #include <cyassl/openssl/bn.h>
  42. #include <cyassl/openssl/dh.h>
  43. #include <cyassl/openssl/rsa.h>
  44. #include <cyassl/openssl/pem.h>
  45. /* openssl headers end, cyassl internal headers next */
  46. #include <cyassl/ctaocrypt/hmac.h>
  47. #include <cyassl/ctaocrypt/random.h>
  48. #include <cyassl/ctaocrypt/des3.h>
  49. #include <cyassl/ctaocrypt/md4.h>
  50. #include <cyassl/ctaocrypt/md5.h>
  51. #include <cyassl/ctaocrypt/arc4.h>
  52. #ifdef CYASSL_SHA512
  53. #include <cyassl/ctaocrypt/sha512.h>
  54. #endif
  55. #endif
  56. #ifndef NO_FILESYSTEM
  57. #if !defined(USE_WINDOWS_API) && !defined(NO_CYASSL_DIR) \
  58. && !defined(EBSNET)
  59. #include <dirent.h>
  60. #endif
  61. #ifdef EBSNET
  62. #include "vfapi.h"
  63. #include "vfile.h"
  64. #endif
  65. #endif /* NO_FILESYSTEM */
  66. #ifndef min
  67. static INLINE word32 min(word32 a, word32 b)
  68. {
  69. return a > b ? b : a;
  70. }
  71. #endif /* min */
  72. #ifndef CYASSL_LEANPSK
  73. char* mystrnstr(const char* s1, const char* s2, unsigned int n)
  74. {
  75. unsigned int s2_len = (unsigned int)XSTRLEN(s2);
  76. if (s2_len == 0)
  77. return (char*)s1;
  78. while (n >= s2_len && s1[0]) {
  79. if (s1[0] == s2[0])
  80. if (XMEMCMP(s1, s2, s2_len) == 0)
  81. return (char*)s1;
  82. s1++;
  83. n--;
  84. }
  85. return NULL;
  86. }
  87. #endif
  88. /* prevent multiple mutex initializations */
  89. static volatile int initRefCount = 0;
  90. static CyaSSL_Mutex count_mutex; /* init ref count mutex */
  91. CYASSL_CTX* CyaSSL_CTX_new(CYASSL_METHOD* method)
  92. {
  93. CYASSL_CTX* ctx = NULL;
  94. CYASSL_ENTER("CYASSL_CTX_new");
  95. if (initRefCount == 0)
  96. CyaSSL_Init(); /* user no longer forced to call Init themselves */
  97. if (method == NULL)
  98. return ctx;
  99. ctx = (CYASSL_CTX*) XMALLOC(sizeof(CYASSL_CTX), 0, DYNAMIC_TYPE_CTX);
  100. if (ctx) {
  101. if (InitSSL_Ctx(ctx, method) < 0) {
  102. CYASSL_MSG("Init CTX failed");
  103. CyaSSL_CTX_free(ctx);
  104. ctx = NULL;
  105. }
  106. }
  107. CYASSL_LEAVE("CYASSL_CTX_new", 0);
  108. return ctx;
  109. }
  110. void CyaSSL_CTX_free(CYASSL_CTX* ctx)
  111. {
  112. CYASSL_ENTER("SSL_CTX_free");
  113. if (ctx)
  114. FreeSSL_Ctx(ctx);
  115. CYASSL_LEAVE("SSL_CTX_free", 0);
  116. }
  117. CYASSL* CyaSSL_new(CYASSL_CTX* ctx)
  118. {
  119. CYASSL* ssl = NULL;
  120. CYASSL_ENTER("SSL_new");
  121. if (ctx == NULL)
  122. return ssl;
  123. ssl = (CYASSL*) XMALLOC(sizeof(CYASSL), ctx->heap,DYNAMIC_TYPE_SSL);
  124. if (ssl)
  125. if (InitSSL(ssl, ctx) < 0) {
  126. FreeSSL(ssl);
  127. ssl = 0;
  128. }
  129. CYASSL_LEAVE("SSL_new", 0);
  130. return ssl;
  131. }
  132. void CyaSSL_free(CYASSL* ssl)
  133. {
  134. CYASSL_ENTER("SSL_free");
  135. if (ssl)
  136. FreeSSL(ssl);
  137. CYASSL_LEAVE("SSL_free", 0);
  138. }
  139. #ifndef CYASSL_LEANPSK
  140. int CyaSSL_set_fd(CYASSL* ssl, int fd)
  141. {
  142. CYASSL_ENTER("SSL_set_fd");
  143. ssl->rfd = fd; /* not used directly to allow IO callbacks */
  144. ssl->wfd = fd;
  145. ssl->IOCB_ReadCtx = &ssl->rfd;
  146. ssl->IOCB_WriteCtx = &ssl->wfd;
  147. #ifdef CYASSL_DTLS
  148. if (ssl->options.dtls) {
  149. ssl->IOCB_ReadCtx = &ssl->buffers.dtlsCtx;
  150. ssl->IOCB_WriteCtx = &ssl->buffers.dtlsCtx;
  151. ssl->buffers.dtlsCtx.fd = fd;
  152. }
  153. #endif
  154. CYASSL_LEAVE("SSL_set_fd", SSL_SUCCESS);
  155. return SSL_SUCCESS;
  156. }
  157. int CyaSSL_get_fd(const CYASSL* ssl)
  158. {
  159. CYASSL_ENTER("SSL_get_fd");
  160. CYASSL_LEAVE("SSL_get_fd", ssl->rfd);
  161. return ssl->rfd;
  162. }
  163. #endif
  164. #ifndef CYASSL_LEANPSK
  165. void CyaSSL_set_using_nonblock(CYASSL* ssl, int nonblock)
  166. {
  167. CYASSL_ENTER("CyaSSL_set_using_nonblock");
  168. ssl->options.usingNonblock = (nonblock != 0);
  169. }
  170. int CyaSSL_get_using_nonblock(CYASSL* ssl)
  171. {
  172. CYASSL_ENTER("CyaSSL_get_using_nonblock");
  173. CYASSL_LEAVE("CyaSSL_get_using_nonblock", ssl->options.usingNonblock);
  174. return ssl->options.usingNonblock;
  175. }
  176. int CyaSSL_dtls(CYASSL* ssl)
  177. {
  178. return ssl->options.dtls;
  179. }
  180. int CyaSSL_dtls_set_peer(CYASSL* ssl, void* peer, unsigned int peerSz)
  181. {
  182. #ifdef CYASSL_DTLS
  183. void* sa = (void*)XMALLOC(peerSz, ssl->heap, DYNAMIC_TYPE_SOCKADDR);
  184. if (sa != NULL) {
  185. XMEMCPY(sa, peer, peerSz);
  186. ssl->buffers.dtlsCtx.peer.sa = sa;
  187. ssl->buffers.dtlsCtx.peer.sz = peerSz;
  188. return SSL_SUCCESS;
  189. }
  190. return SSL_FAILURE;
  191. #else
  192. (void)ssl;
  193. (void)peer;
  194. (void)peerSz;
  195. return SSL_NOT_IMPLEMENTED;
  196. #endif
  197. }
  198. int CyaSSL_dtls_get_peer(CYASSL* ssl, void* peer, unsigned int* peerSz)
  199. {
  200. #ifdef CYASSL_DTLS
  201. if (peer != NULL && peerSz != NULL
  202. && *peerSz >= ssl->buffers.dtlsCtx.peer.sz) {
  203. *peerSz = ssl->buffers.dtlsCtx.peer.sz;
  204. XMEMCPY(peer, ssl->buffers.dtlsCtx.peer.sa, *peerSz);
  205. return SSL_SUCCESS;
  206. }
  207. return SSL_FAILURE;
  208. #else
  209. (void)ssl;
  210. (void)peer;
  211. (void)peerSz;
  212. return SSL_NOT_IMPLEMENTED;
  213. #endif
  214. }
  215. #endif /* CYASSL_LEANPSK */
  216. int CyaSSL_negotiate(CYASSL* ssl)
  217. {
  218. int err = SSL_FATAL_ERROR;
  219. CYASSL_ENTER("CyaSSL_negotiate");
  220. #ifndef NO_CYASSL_SERVER
  221. if (ssl->options.side == SERVER_END)
  222. err = CyaSSL_accept(ssl);
  223. #endif
  224. #ifndef NO_CYASSL_CLIENT
  225. if (ssl->options.side == CLIENT_END)
  226. err = CyaSSL_connect(ssl);
  227. #endif
  228. CYASSL_LEAVE("CyaSSL_negotiate", err);
  229. if (err == SSL_SUCCESS)
  230. return 0;
  231. else
  232. return err;
  233. }
  234. #ifndef CYASSL_LEANPSK
  235. /* object size based on build */
  236. int CyaSSL_GetObjectSize(void)
  237. {
  238. #ifdef SHOW_SIZES
  239. printf("sizeof suites = %lu\n", sizeof(Suites));
  240. printf("sizeof ciphers(2) = %lu\n", sizeof(Ciphers));
  241. printf("\tsizeof arc4 = %lu\n", sizeof(Arc4));
  242. printf("\tsizeof aes = %lu\n", sizeof(Aes));
  243. printf("\tsizeof des3 = %lu\n", sizeof(Des3));
  244. printf("\tsizeof rabbit = %lu\n", sizeof(Rabbit));
  245. printf("sizeof cipher specs = %lu\n", sizeof(CipherSpecs));
  246. printf("sizeof keys = %lu\n", sizeof(Keys));
  247. printf("sizeof MD5 = %lu\n", sizeof(Md5));
  248. printf("sizeof SHA = %lu\n", sizeof(Sha));
  249. printf("sizeof SHA256 = %lu\n", sizeof(Sha256));
  250. printf("sizeof Hashes(2) = %lu\n", sizeof(Hashes));
  251. printf("sizeof Buffers = %lu\n", sizeof(Buffers));
  252. printf("sizeof Options = %lu\n", sizeof(Options));
  253. printf("sizeof Arrays = %lu\n", sizeof(Arrays));
  254. printf("sizeof Session = %lu\n", sizeof(CYASSL_SESSION));
  255. printf("sizeof peerKey = %lu\n", sizeof(RsaKey));
  256. printf("sizeof CYASSL_CIPHER = %lu\n", sizeof(CYASSL_CIPHER));
  257. #endif
  258. return sizeof(CYASSL);
  259. }
  260. #endif
  261. /* XXX should be NO_DH */
  262. #ifndef NO_CERTS
  263. /* server Diffie-Hellman parameters */
  264. int CyaSSL_SetTmpDH(CYASSL* ssl, const unsigned char* p, int pSz,
  265. const unsigned char* g, int gSz)
  266. {
  267. byte havePSK = 0;
  268. byte haveRSA = 1;
  269. CYASSL_ENTER("CyaSSL_SetTmpDH");
  270. if (ssl == NULL || p == NULL || g == NULL) return BAD_FUNC_ARG;
  271. if (ssl->options.side != SERVER_END)
  272. return SIDE_ERROR;
  273. if (ssl->buffers.serverDH_P.buffer && ssl->buffers.weOwnDH)
  274. XFREE(ssl->buffers.serverDH_P.buffer, ssl->ctx->heap, DYNAMIC_TYPE_DH);
  275. if (ssl->buffers.serverDH_G.buffer && ssl->buffers.weOwnDH)
  276. XFREE(ssl->buffers.serverDH_G.buffer, ssl->ctx->heap, DYNAMIC_TYPE_DH);
  277. ssl->buffers.weOwnDH = 1; /* SSL owns now */
  278. ssl->buffers.serverDH_P.buffer = (byte*)XMALLOC(pSz, ssl->ctx->heap,
  279. DYNAMIC_TYPE_DH);
  280. if (ssl->buffers.serverDH_P.buffer == NULL)
  281. return MEMORY_E;
  282. ssl->buffers.serverDH_G.buffer = (byte*)XMALLOC(gSz, ssl->ctx->heap,
  283. DYNAMIC_TYPE_DH);
  284. if (ssl->buffers.serverDH_G.buffer == NULL) {
  285. XFREE(ssl->buffers.serverDH_P.buffer, ssl->ctx->heap, DYNAMIC_TYPE_DH);
  286. return MEMORY_E;
  287. }
  288. ssl->buffers.serverDH_P.length = pSz;
  289. ssl->buffers.serverDH_G.length = gSz;
  290. XMEMCPY(ssl->buffers.serverDH_P.buffer, p, pSz);
  291. XMEMCPY(ssl->buffers.serverDH_G.buffer, g, gSz);
  292. ssl->options.haveDH = 1;
  293. #ifndef NO_PSK
  294. havePSK = ssl->options.havePSK;
  295. #endif
  296. #ifdef NO_RSA
  297. haveRSA = 0;
  298. #endif
  299. InitSuites(ssl->suites, ssl->version, haveRSA, havePSK, ssl->options.haveDH,
  300. ssl->options.haveNTRU, ssl->options.haveECDSAsig,
  301. ssl->options.haveStaticECC, ssl->options.side);
  302. CYASSL_LEAVE("CyaSSL_SetTmpDH", 0);
  303. return 0;
  304. }
  305. #endif /* !NO_CERTS */
  306. int CyaSSL_write(CYASSL* ssl, const void* data, int sz)
  307. {
  308. int ret;
  309. CYASSL_ENTER("SSL_write()");
  310. #ifdef HAVE_ERRNO_H
  311. errno = 0;
  312. #endif
  313. ret = SendData(ssl, data, sz);
  314. CYASSL_LEAVE("SSL_write()", ret);
  315. if (ret < 0)
  316. return SSL_FATAL_ERROR;
  317. else
  318. return ret;
  319. }
  320. static int CyaSSL_read_internal(CYASSL* ssl, void* data, int sz, int peek)
  321. {
  322. int ret;
  323. CYASSL_ENTER("CyaSSL_read_internal()");
  324. #ifdef HAVE_ERRNO_H
  325. errno = 0;
  326. #endif
  327. ret = ReceiveData(ssl, (byte*)data, min(sz, OUTPUT_RECORD_SIZE), peek);
  328. CYASSL_LEAVE("CyaSSL_read_internal()", ret);
  329. if (ret < 0)
  330. return SSL_FATAL_ERROR;
  331. else
  332. return ret;
  333. }
  334. int CyaSSL_peek(CYASSL* ssl, void* data, int sz)
  335. {
  336. CYASSL_ENTER("CyaSSL_peek()");
  337. return CyaSSL_read_internal(ssl, data, sz, TRUE);
  338. }
  339. int CyaSSL_read(CYASSL* ssl, void* data, int sz)
  340. {
  341. CYASSL_ENTER("CyaSSL_read()");
  342. return CyaSSL_read_internal(ssl, data, sz, FALSE);
  343. }
  344. #ifdef HAVE_CAVIUM
  345. int CyaSSL_UseCavium(CYASSL* ssl, int devId)
  346. {
  347. if (ssl == NULL)
  348. return BAD_FUNC_ARG;
  349. ssl->devId = devId;
  350. return 0;
  351. }
  352. int CyaSSL_CTX_UseCavium(CYASSL_CTX* ctx, int devId)
  353. {
  354. if (ctx == NULL)
  355. return BAD_FUNC_ARG;
  356. ctx->devId = devId;
  357. return 0;
  358. }
  359. #endif /* HAVE_CAVIUM */
  360. #ifndef CYASSL_LEANPSK
  361. int CyaSSL_send(CYASSL* ssl, const void* data, int sz, int flags)
  362. {
  363. int ret;
  364. int oldFlags = ssl->wflags;
  365. CYASSL_ENTER("CyaSSL_send()");
  366. ssl->wflags = flags;
  367. ret = CyaSSL_write(ssl, data, sz);
  368. ssl->wflags = oldFlags;
  369. CYASSL_LEAVE("CyaSSL_send()", ret);
  370. return ret;
  371. }
  372. int CyaSSL_recv(CYASSL* ssl, void* data, int sz, int flags)
  373. {
  374. int ret;
  375. int oldFlags = ssl->rflags;
  376. CYASSL_ENTER("CyaSSL_recv()");
  377. ssl->rflags = flags;
  378. ret = CyaSSL_read(ssl, data, sz);
  379. ssl->rflags = oldFlags;
  380. CYASSL_LEAVE("CyaSSL_recv()", ret);
  381. return ret;
  382. }
  383. #endif
  384. int CyaSSL_shutdown(CYASSL* ssl)
  385. {
  386. CYASSL_ENTER("SSL_shutdown()");
  387. if (ssl->options.quietShutdown) {
  388. CYASSL_MSG("quiet shutdown, no close notify sent");
  389. return 0;
  390. }
  391. /* try to send close notify, not an error if can't */
  392. if (!ssl->options.isClosed && !ssl->options.connReset &&
  393. !ssl->options.sentNotify) {
  394. ssl->error = SendAlert(ssl, alert_warning, close_notify);
  395. if (ssl->error < 0) {
  396. CYASSL_ERROR(ssl->error);
  397. return SSL_FATAL_ERROR;
  398. }
  399. ssl->options.sentNotify = 1; /* don't send close_notify twice */
  400. }
  401. CYASSL_LEAVE("SSL_shutdown()", ssl->error);
  402. ssl->error = SSL_ERROR_SYSCALL; /* simulate OpenSSL behavior */
  403. return 0;
  404. }
  405. int CyaSSL_get_error(CYASSL* ssl, int ret)
  406. {
  407. CYASSL_ENTER("SSL_get_error");
  408. CYASSL_LEAVE("SSL_get_error", ssl->error);
  409. if (ret > 0)
  410. return SSL_ERROR_NONE;
  411. /* make sure converted types are handled in SetErrorString() too */
  412. if (ssl->error == WANT_READ)
  413. return SSL_ERROR_WANT_READ; /* convert to OpenSSL type */
  414. else if (ssl->error == WANT_WRITE)
  415. return SSL_ERROR_WANT_WRITE; /* convert to OpenSSL type */
  416. else if (ssl->error == ZERO_RETURN)
  417. return SSL_ERROR_ZERO_RETURN; /* convert to OpenSSL type */
  418. return ssl->error;
  419. }
  420. int CyaSSL_want_read(CYASSL* ssl)
  421. {
  422. CYASSL_ENTER("SSL_want_read");
  423. if (ssl->error == WANT_READ)
  424. return 1;
  425. return 0;
  426. }
  427. int CyaSSL_want_write(CYASSL* ssl)
  428. {
  429. CYASSL_ENTER("SSL_want_write");
  430. if (ssl->error == WANT_WRITE)
  431. return 1;
  432. return 0;
  433. }
  434. char* CyaSSL_ERR_error_string(unsigned long errNumber, char* data)
  435. {
  436. static const char* msg = "Please supply a buffer for error string";
  437. CYASSL_ENTER("ERR_error_string");
  438. if (data) {
  439. SetErrorString((int)errNumber, data);
  440. return data;
  441. }
  442. return (char*)msg;
  443. }
  444. void CyaSSL_ERR_error_string_n(unsigned long e, char* buf, unsigned long len)
  445. {
  446. CYASSL_ENTER("CyaSSL_ERR_error_string_n");
  447. if (len) CyaSSL_ERR_error_string(e, buf);
  448. }
  449. /* don't free temporary arrays at end of handshake */
  450. void CyaSSL_KeepArrays(CYASSL* ssl)
  451. {
  452. if (ssl)
  453. ssl->options.saveArrays = 1;
  454. }
  455. /* user doesn't need temporary arrays anymore, Free */
  456. void CyaSSL_FreeArrays(CYASSL* ssl)
  457. {
  458. if (ssl && ssl->options.handShakeState == HANDSHAKE_DONE) {
  459. ssl->options.saveArrays = 0;
  460. FreeArrays(ssl, 1);
  461. }
  462. }
  463. #ifndef NO_CERTS
  464. CYASSL_CERT_MANAGER* CyaSSL_CertManagerNew(void)
  465. {
  466. CYASSL_CERT_MANAGER* cm = NULL;
  467. CYASSL_ENTER("CyaSSL_CertManagerNew");
  468. cm = (CYASSL_CERT_MANAGER*) XMALLOC(sizeof(CYASSL_CERT_MANAGER), 0,
  469. DYNAMIC_TYPE_CERT_MANAGER);
  470. if (cm) {
  471. cm->caList = NULL;
  472. cm->heap = NULL;
  473. cm->caCacheCallback = NULL;
  474. cm->crl = NULL;
  475. cm->crlEnabled = 0;
  476. cm->crlCheckAll = 0;
  477. cm->cbMissingCRL = NULL;
  478. if (InitMutex(&cm->caLock) != 0) {
  479. CYASSL_MSG("Bad mutex init");
  480. CyaSSL_CertManagerFree(cm);
  481. return NULL;
  482. }
  483. }
  484. return cm;
  485. }
  486. void CyaSSL_CertManagerFree(CYASSL_CERT_MANAGER* cm)
  487. {
  488. CYASSL_ENTER("CyaSSL_CertManagerFree");
  489. if (cm) {
  490. #ifdef HAVE_CRL
  491. if (cm->crl)
  492. FreeCRL(cm->crl, 1);
  493. #endif
  494. FreeSigners(cm->caList, NULL);
  495. FreeMutex(&cm->caLock);
  496. XFREE(cm, NULL, DYNAMIC_TYPE_CERT_MANAGER);
  497. }
  498. }
  499. #endif /* !NO_CERTS */
  500. #ifndef NO_FILESYSTEM
  501. void CyaSSL_ERR_print_errors_fp(FILE* fp, int err)
  502. {
  503. char data[MAX_ERROR_SZ + 1];
  504. CYASSL_ENTER("CyaSSL_ERR_print_errors_fp");
  505. SetErrorString(err, data);
  506. fprintf(fp, "%s", data);
  507. }
  508. #endif
  509. int CyaSSL_pending(CYASSL* ssl)
  510. {
  511. CYASSL_ENTER("SSL_pending");
  512. return ssl->buffers.clearOutputBuffer.length;
  513. }
  514. #ifndef CYASSL_LEANPSK
  515. /* trun on handshake group messages for context */
  516. int CyaSSL_CTX_set_group_messages(CYASSL_CTX* ctx)
  517. {
  518. if (ctx == NULL)
  519. return BAD_FUNC_ARG;
  520. ctx->groupMessages = 1;
  521. return SSL_SUCCESS;
  522. }
  523. #endif
  524. #ifndef NO_CYASSL_CLIENT
  525. /* connect enough to get peer cert chain */
  526. int CyaSSL_connect_cert(CYASSL* ssl)
  527. {
  528. int ret;
  529. if (ssl == NULL)
  530. return SSL_FAILURE;
  531. ssl->options.certOnly = 1;
  532. ret = CyaSSL_connect(ssl);
  533. ssl->options.certOnly = 0;
  534. return ret;
  535. }
  536. #endif
  537. #ifndef CYASSL_LEANPSK
  538. /* trun on handshake group messages for ssl object */
  539. int CyaSSL_set_group_messages(CYASSL* ssl)
  540. {
  541. if (ssl == NULL)
  542. return BAD_FUNC_ARG;
  543. ssl->options.groupMessages = 1;
  544. return SSL_SUCCESS;
  545. }
  546. int CyaSSL_SetVersion(CYASSL* ssl, int version)
  547. {
  548. byte haveRSA = 1;
  549. byte havePSK = 0;
  550. CYASSL_ENTER("CyaSSL_SetVersion");
  551. if (ssl == NULL) {
  552. CYASSL_MSG("Bad function argument");
  553. return BAD_FUNC_ARG;
  554. }
  555. switch (version) {
  556. #ifndef NO_OLD_TLS
  557. case CYASSL_SSLV3:
  558. ssl->version = MakeSSLv3();
  559. break;
  560. #endif
  561. #ifndef NO_TLS
  562. #ifndef NO_OLD_TLS
  563. case CYASSL_TLSV1:
  564. ssl->version = MakeTLSv1();
  565. break;
  566. case CYASSL_TLSV1_1:
  567. ssl->version = MakeTLSv1_1();
  568. break;
  569. #endif
  570. case CYASSL_TLSV1_2:
  571. ssl->version = MakeTLSv1_2();
  572. break;
  573. #endif
  574. default:
  575. CYASSL_MSG("Bad function argument");
  576. return BAD_FUNC_ARG;
  577. }
  578. #ifdef NO_RSA
  579. haveRSA = 0;
  580. #endif
  581. #ifndef NO_PSK
  582. havePSK = ssl->options.havePSK;
  583. #endif
  584. InitSuites(ssl->suites, ssl->version, haveRSA, havePSK, ssl->options.haveDH,
  585. ssl->options.haveNTRU, ssl->options.haveECDSAsig,
  586. ssl->options.haveStaticECC, ssl->options.side);
  587. return SSL_SUCCESS;
  588. }
  589. #endif
  590. #ifndef NO_CERTS
  591. /* does CA already exist on signer list */
  592. int AlreadySigner(CYASSL_CERT_MANAGER* cm, byte* hash)
  593. {
  594. Signer* signers;
  595. int ret = 0;
  596. if (LockMutex(&cm->caLock) != 0)
  597. return ret;
  598. signers = cm->caList;
  599. while (signers) {
  600. if (XMEMCMP(hash, signers->hash, SHA_DIGEST_SIZE) == 0) {
  601. ret = 1;
  602. break;
  603. }
  604. signers = signers->next;
  605. }
  606. UnLockMutex(&cm->caLock);
  607. return ret;
  608. }
  609. /* return CA if found, otherwise NULL */
  610. Signer* GetCA(void* vp, byte* hash)
  611. {
  612. CYASSL_CERT_MANAGER* cm = (CYASSL_CERT_MANAGER*)vp;
  613. Signer* ret = NULL;
  614. Signer* signers;
  615. if (cm == NULL)
  616. return NULL;
  617. signers = cm->caList;
  618. if (LockMutex(&cm->caLock) != 0)
  619. return ret;
  620. while (signers) {
  621. if (XMEMCMP(hash, signers->hash, SHA_DIGEST_SIZE) == 0) {
  622. ret = signers;
  623. break;
  624. }
  625. signers = signers->next;
  626. }
  627. UnLockMutex(&cm->caLock);
  628. return ret;
  629. }
  630. /* owns der, internal now uses too */
  631. /* type flag ids from user or from chain received during verify
  632. don't allow chain ones to be added w/o isCA extension */
  633. int AddCA(CYASSL_CERT_MANAGER* cm, buffer der, int type, int verify)
  634. {
  635. int ret;
  636. DecodedCert cert;
  637. Signer* signer = 0;
  638. CYASSL_MSG("Adding a CA");
  639. InitDecodedCert(&cert, der.buffer, der.length, cm->heap);
  640. ret = ParseCert(&cert, CA_TYPE, verify, cm);
  641. CYASSL_MSG(" Parsed new CA");
  642. if (ret == 0 && cert.isCA == 0 && type != CYASSL_USER_CA) {
  643. CYASSL_MSG(" Can't add as CA if not actually one");
  644. ret = NOT_CA_ERROR;
  645. }
  646. else if (ret == 0 && AlreadySigner(cm, cert.subjectHash)) {
  647. CYASSL_MSG(" Already have this CA, not adding again");
  648. (void)ret;
  649. }
  650. else if (ret == 0) {
  651. /* take over signer parts */
  652. signer = MakeSigner(cm->heap);
  653. if (!signer)
  654. ret = MEMORY_ERROR;
  655. else {
  656. signer->keyOID = cert.keyOID;
  657. signer->publicKey = cert.publicKey;
  658. signer->pubKeySize = cert.pubKeySize;
  659. signer->name = cert.subjectCN;
  660. XMEMCPY(signer->hash, cert.subjectHash, SHA_DIGEST_SIZE);
  661. signer->next = NULL; /* in case lock fails */
  662. cert.publicKey = 0; /* don't free here */
  663. cert.subjectCN = 0;
  664. if (LockMutex(&cm->caLock) == 0) {
  665. signer->next = cm->caList;
  666. cm->caList = signer; /* takes ownership */
  667. UnLockMutex(&cm->caLock);
  668. if (cm->caCacheCallback)
  669. cm->caCacheCallback(der.buffer, (int)der.length, type);
  670. }
  671. else {
  672. CYASSL_MSG(" CA Mutex Lock failed");
  673. ret = BAD_MUTEX_ERROR;
  674. FreeSigners(signer, cm->heap);
  675. }
  676. }
  677. }
  678. CYASSL_MSG(" Freeing Parsed CA");
  679. FreeDecodedCert(&cert);
  680. CYASSL_MSG(" Freeing der CA");
  681. XFREE(der.buffer, cm->heap, DYNAMIC_TYPE_CA);
  682. CYASSL_MSG(" OK Freeing der CA");
  683. CYASSL_LEAVE("AddCA", ret);
  684. if (ret == 0) return SSL_SUCCESS;
  685. return ret;
  686. }
  687. #endif /* !NO_CERTS */
  688. #ifndef NO_SESSION_CACHE
  689. /* basic config gives a cache with 33 sessions, adequate for clients and
  690. embedded servers
  691. MEDIUM_SESSION_CACHE allows 1055 sessions, adequate for servers that
  692. aren't under heavy load, basically allows 200 new sessions per minute
  693. BIG_SESSION_CACHE yields 20,0027 sessions
  694. HUGE_SESSION_CACHE yields 65,791 sessions, for servers under heavy load,
  695. allows over 13,000 new sessions per minute or over 200 new sessions per
  696. second
  697. SMALL_SESSION_CACHE only stores 6 sessions, good for embedded clients
  698. or systems where the default of nearly 3kB is too much RAM, this define
  699. uses less than 500 bytes RAM
  700. */
  701. #ifdef HUGE_SESSION_CACHE
  702. #define SESSIONS_PER_ROW 11
  703. #define SESSION_ROWS 5981
  704. #elif defined(BIG_SESSION_CACHE)
  705. #define SESSIONS_PER_ROW 7
  706. #define SESSION_ROWS 2861
  707. #elif defined(MEDIUM_SESSION_CACHE)
  708. #define SESSIONS_PER_ROW 5
  709. #define SESSION_ROWS 211
  710. #elif defined(SMALL_SESSION_CACHE)
  711. #define SESSIONS_PER_ROW 2
  712. #define SESSION_ROWS 3
  713. #else
  714. #define SESSIONS_PER_ROW 3
  715. #define SESSION_ROWS 11
  716. #endif
  717. typedef struct SessionRow {
  718. int nextIdx; /* where to place next one */
  719. int totalCount; /* sessions ever on this row */
  720. CYASSL_SESSION Sessions[SESSIONS_PER_ROW];
  721. } SessionRow;
  722. static SessionRow SessionCache[SESSION_ROWS];
  723. static CyaSSL_Mutex session_mutex; /* SessionCache mutex */
  724. #endif /* NO_SESSION_CACHE */
  725. int CyaSSL_Init(void)
  726. {
  727. int ret = 0;
  728. CYASSL_ENTER("CyaSSL_Init");
  729. if (initRefCount == 0) {
  730. #ifndef NO_SESSION_CACHE
  731. if (InitMutex(&session_mutex) != 0)
  732. ret = BAD_MUTEX_ERROR;
  733. #endif
  734. if (InitMutex(&count_mutex) != 0)
  735. ret = BAD_MUTEX_ERROR;
  736. }
  737. if (ret == 0) {
  738. LockMutex(&count_mutex);
  739. initRefCount++;
  740. UnLockMutex(&count_mutex);
  741. }
  742. return ret;
  743. }
  744. #ifndef NO_CERTS
  745. /* Remove PEM header/footer, convert to ASN1, store any encrypted data
  746. info->consumed tracks of PEM bytes consumed in case multiple parts */
  747. int PemToDer(const unsigned char* buff, long longSz, int type,
  748. buffer* der, void* heap, EncryptedInfo* info, int* eccKey)
  749. {
  750. char header[PEM_LINE_LEN];
  751. char footer[PEM_LINE_LEN];
  752. char* headerEnd;
  753. char* footerEnd;
  754. char* consumedEnd;
  755. char* bufferEnd = (char*)(buff + longSz);
  756. long neededSz;
  757. int pkcs8 = 0;
  758. int pkcs8Enc = 0;
  759. int dynamicType = 0;
  760. int sz = (int)longSz;
  761. (void)heap;
  762. (void)dynamicType;
  763. if (type == CERT_TYPE || type == CA_TYPE) {
  764. XSTRNCPY(header, "-----BEGIN CERTIFICATE-----", sizeof(header));
  765. XSTRNCPY(footer, "-----END CERTIFICATE-----", sizeof(footer));
  766. dynamicType = (type == CA_TYPE) ? DYNAMIC_TYPE_CA :
  767. DYNAMIC_TYPE_CERT;
  768. } else if (type == DH_PARAM_TYPE) {
  769. XSTRNCPY(header, "-----BEGIN DH PARAMETERS-----", sizeof(header));
  770. XSTRNCPY(footer, "-----END DH PARAMETERS-----", sizeof(footer));
  771. dynamicType = DYNAMIC_TYPE_KEY;
  772. } else if (type == CRL_TYPE) {
  773. XSTRNCPY(header, "-----BEGIN X509 CRL-----", sizeof(header));
  774. XSTRNCPY(footer, "-----END X509 CRL-----", sizeof(footer));
  775. dynamicType = DYNAMIC_TYPE_CRL;
  776. } else {
  777. XSTRNCPY(header, "-----BEGIN RSA PRIVATE KEY-----", sizeof(header));
  778. XSTRNCPY(footer, "-----END RSA PRIVATE KEY-----", sizeof(footer));
  779. dynamicType = DYNAMIC_TYPE_KEY;
  780. }
  781. /* find header */
  782. headerEnd = XSTRNSTR((char*)buff, header, sz);
  783. if (!headerEnd && type == PRIVATEKEY_TYPE) { /* may be pkcs8 */
  784. XSTRNCPY(header, "-----BEGIN PRIVATE KEY-----", sizeof(header));
  785. XSTRNCPY(footer, "-----END PRIVATE KEY-----", sizeof(footer));
  786. headerEnd = XSTRNSTR((char*)buff, header, sz);
  787. if (headerEnd)
  788. pkcs8 = 1;
  789. else {
  790. XSTRNCPY(header, "-----BEGIN ENCRYPTED PRIVATE KEY-----",
  791. sizeof(header));
  792. XSTRNCPY(footer, "-----END ENCRYPTED PRIVATE KEY-----",
  793. sizeof(footer));
  794. headerEnd = XSTRNSTR((char*)buff, header, sz);
  795. if (headerEnd) {
  796. pkcs8Enc = 1;
  797. (void)pkcs8Enc; /* only opensslextra will read */
  798. }
  799. }
  800. }
  801. if (!headerEnd && type == PRIVATEKEY_TYPE) { /* may be ecc */
  802. XSTRNCPY(header, "-----BEGIN EC PRIVATE KEY-----", sizeof(header));
  803. XSTRNCPY(footer, "-----END EC PRIVATE KEY-----", sizeof(footer));
  804. headerEnd = XSTRNSTR((char*)buff, header, sz);
  805. if (headerEnd)
  806. *eccKey = 1;
  807. }
  808. if (!headerEnd && type == PRIVATEKEY_TYPE) { /* may be dsa */
  809. XSTRNCPY(header, "-----BEGIN DSA PRIVATE KEY-----", sizeof(header));
  810. XSTRNCPY(footer, "-----END DSA PRIVATE KEY-----", sizeof(footer));
  811. headerEnd = XSTRNSTR((char*)buff, header, sz);
  812. }
  813. if (!headerEnd) {
  814. CYASSL_MSG("Couldn't find PEM header");
  815. return SSL_NO_PEM_HEADER;
  816. }
  817. headerEnd += XSTRLEN(header);
  818. /* eat end of line */
  819. if (headerEnd[0] == '\n')
  820. headerEnd++;
  821. else if (headerEnd[1] == '\n')
  822. headerEnd += 2;
  823. else
  824. return SSL_BAD_FILE;
  825. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  826. {
  827. /* remove encrypted header if there */
  828. char encHeader[] = "Proc-Type";
  829. char* line = XSTRNSTR((char*)buff, encHeader, PEM_LINE_LEN);
  830. if (line) {
  831. char* newline;
  832. char* finish;
  833. char* start = XSTRNSTR(line, "DES", PEM_LINE_LEN);
  834. if (!start)
  835. start = XSTRNSTR(line, "AES", PEM_LINE_LEN);
  836. if (!start) return SSL_BAD_FILE;
  837. if (!info) return SSL_BAD_FILE;
  838. finish = XSTRNSTR(start, ",", PEM_LINE_LEN);
  839. if (start && finish && (start < finish)) {
  840. newline = XSTRNSTR(finish, "\r", PEM_LINE_LEN);
  841. XMEMCPY(info->name, start, finish - start);
  842. info->name[finish - start] = 0;
  843. XMEMCPY(info->iv, finish + 1, sizeof(info->iv));
  844. if (!newline) newline = XSTRNSTR(finish, "\n", PEM_LINE_LEN);
  845. if (newline && (newline > finish)) {
  846. info->ivSz = (word32)(newline - (finish + 1));
  847. info->set = 1;
  848. }
  849. else
  850. return SSL_BAD_FILE;
  851. }
  852. else
  853. return SSL_BAD_FILE;
  854. /* eat blank line */
  855. while (*newline == '\r' || *newline == '\n')
  856. newline++;
  857. headerEnd = newline;
  858. }
  859. }
  860. #endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
  861. /* find footer */
  862. footerEnd = XSTRNSTR((char*)buff, footer, sz);
  863. if (!footerEnd) return SSL_BAD_FILE;
  864. consumedEnd = footerEnd + XSTRLEN(footer);
  865. if (consumedEnd < bufferEnd) { /* handle no end of line on last line */
  866. /* eat end of line */
  867. if (consumedEnd[0] == '\n')
  868. consumedEnd++;
  869. else if (consumedEnd[1] == '\n')
  870. consumedEnd += 2;
  871. else
  872. return SSL_BAD_FILE;
  873. }
  874. if (info)
  875. info->consumed = (long)(consumedEnd - (char*)buff);
  876. /* set up der buffer */
  877. neededSz = (long)(footerEnd - headerEnd);
  878. if (neededSz > sz || neededSz < 0) return SSL_BAD_FILE;
  879. der->buffer = (byte*) XMALLOC(neededSz, heap, dynamicType);
  880. if (!der->buffer) return MEMORY_ERROR;
  881. der->length = (word32)neededSz;
  882. if (Base64_Decode((byte*)headerEnd, (word32)neededSz, der->buffer,
  883. &der->length) < 0)
  884. return SSL_BAD_FILE;
  885. if (pkcs8)
  886. return ToTraditional(der->buffer, der->length);
  887. #ifdef OPENSSL_EXTRA
  888. if (pkcs8Enc) {
  889. int passwordSz;
  890. char password[80];
  891. if (!info || !info->ctx || !info->ctx->passwd_cb)
  892. return SSL_BAD_FILE; /* no callback error */
  893. passwordSz = info->ctx->passwd_cb(password, sizeof(password), 0,
  894. info->ctx->userdata);
  895. return ToTraditionalEnc(der->buffer, der->length, password,
  896. passwordSz);
  897. }
  898. #endif
  899. return 0;
  900. }
  901. /* process the buffer buff, legnth sz, into ctx of format and type
  902. used tracks bytes consumed, userChain specifies a user cert chain
  903. to pass during the handshake */
  904. static int ProcessBuffer(CYASSL_CTX* ctx, const unsigned char* buff,
  905. long sz, int format, int type, CYASSL* ssl,
  906. long* used, int userChain)
  907. {
  908. EncryptedInfo info;
  909. buffer der; /* holds DER or RAW (for NTRU) */
  910. int ret;
  911. int dynamicType = 0;
  912. int eccKey = 0;
  913. void* heap = ctx ? ctx->heap : NULL;
  914. info.set = 0;
  915. info.ctx = ctx;
  916. info.consumed = 0;
  917. der.buffer = 0;
  918. (void)dynamicType;
  919. if (used)
  920. *used = sz; /* used bytes default to sz, PEM chain may shorten*/
  921. if (format != SSL_FILETYPE_ASN1 && format != SSL_FILETYPE_PEM
  922. && format != SSL_FILETYPE_RAW)
  923. return SSL_BAD_FILETYPE;
  924. if (type == CA_TYPE)
  925. dynamicType = DYNAMIC_TYPE_CA;
  926. else if (type == CERT_TYPE)
  927. dynamicType = DYNAMIC_TYPE_CERT;
  928. else
  929. dynamicType = DYNAMIC_TYPE_KEY;
  930. if (format == SSL_FILETYPE_PEM) {
  931. ret = PemToDer(buff, sz, type, &der, heap, &info, &eccKey);
  932. if (ret < 0) {
  933. XFREE(der.buffer, heap, dynamicType);
  934. return ret;
  935. }
  936. if (used)
  937. *used = info.consumed;
  938. /* we may have a user cert chain, try to consume */
  939. if (userChain && type == CERT_TYPE && info.consumed < sz) {
  940. byte staticBuffer[FILE_BUFFER_SIZE]; /* tmp chain buffer */
  941. byte* chainBuffer = staticBuffer;
  942. int dynamicBuffer = 0;
  943. word32 bufferSz = sizeof(staticBuffer);
  944. long consumed = info.consumed;
  945. word32 idx = 0;
  946. int gotOne = 0;
  947. if ( (sz - consumed) > (int)bufferSz) {
  948. CYASSL_MSG("Growing Tmp Chain Buffer");
  949. bufferSz = (word32)(sz - consumed);
  950. /* will shrink to actual size */
  951. chainBuffer = (byte*)XMALLOC(bufferSz, heap,
  952. DYNAMIC_TYPE_FILE);
  953. if (chainBuffer == NULL) {
  954. XFREE(der.buffer, heap, dynamicType);
  955. return MEMORY_E;
  956. }
  957. dynamicBuffer = 1;
  958. }
  959. CYASSL_MSG("Processing Cert Chain");
  960. while (consumed < sz) {
  961. buffer part;
  962. info.consumed = 0;
  963. part.buffer = 0;
  964. ret = PemToDer(buff + consumed, sz - consumed, type, &part,
  965. heap, &info, &eccKey);
  966. if (ret == 0) {
  967. gotOne = 1;
  968. if ( (idx + part.length) > bufferSz) {
  969. CYASSL_MSG(" Cert Chain bigger than buffer");
  970. ret = BUFFER_E;
  971. }
  972. else {
  973. c32to24(part.length, &chainBuffer[idx]);
  974. idx += CERT_HEADER_SZ;
  975. XMEMCPY(&chainBuffer[idx], part.buffer,part.length);
  976. idx += part.length;
  977. consumed += info.consumed;
  978. if (used)
  979. *used += info.consumed;
  980. }
  981. }
  982. XFREE(part.buffer, heap, dynamicType);
  983. if (ret == SSL_NO_PEM_HEADER && gotOne) {
  984. CYASSL_MSG("We got one good PEM so stuff at end ok");
  985. break;
  986. }
  987. if (ret < 0) {
  988. CYASSL_MSG(" Error in Cert in Chain");
  989. XFREE(der.buffer, heap, dynamicType);
  990. return ret;
  991. }
  992. CYASSL_MSG(" Consumed another Cert in Chain");
  993. }
  994. CYASSL_MSG("Finished Processing Cert Chain");
  995. if (ctx == NULL) {
  996. CYASSL_MSG("certChain needs context");
  997. return BAD_FUNC_ARG;
  998. }
  999. ctx->certChain.buffer = (byte*)XMALLOC(idx, heap,
  1000. dynamicType);
  1001. if (ctx->certChain.buffer) {
  1002. ctx->certChain.length = idx;
  1003. XMEMCPY(ctx->certChain.buffer, chainBuffer, idx);
  1004. }
  1005. if (dynamicBuffer)
  1006. XFREE(chainBuffer, heap, DYNAMIC_TYPE_FILE);
  1007. if (ctx->certChain.buffer == NULL) {
  1008. XFREE(der.buffer, heap, dynamicType);
  1009. return MEMORY_E;
  1010. }
  1011. }
  1012. }
  1013. else { /* ASN1 (DER) or RAW (NTRU) */
  1014. der.buffer = (byte*) XMALLOC(sz, heap, dynamicType);
  1015. if (!der.buffer) return MEMORY_ERROR;
  1016. XMEMCPY(der.buffer, buff, sz);
  1017. der.length = (word32)sz;
  1018. }
  1019. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  1020. if (info.set) {
  1021. /* decrypt */
  1022. char password[80];
  1023. int passwordSz;
  1024. byte key[AES_256_KEY_SIZE];
  1025. byte iv[AES_IV_SIZE];
  1026. if (!ctx || !ctx->passwd_cb) {
  1027. XFREE(der.buffer, heap, dynamicType);
  1028. return NO_PASSWORD;
  1029. }
  1030. /* use file's salt for key derivation, hex decode first */
  1031. if (Base16_Decode(info.iv, info.ivSz, info.iv, &info.ivSz) != 0) {
  1032. XFREE(der.buffer, heap, dynamicType);
  1033. return ASN_INPUT_E;
  1034. }
  1035. passwordSz = ctx->passwd_cb(password, sizeof(password), 0,
  1036. ctx->userdata);
  1037. if ( (ret = EVP_BytesToKey(info.name, "MD5", info.iv,
  1038. (byte*)password, passwordSz, 1, key, iv)) <= 0) {
  1039. XFREE(der.buffer, heap, dynamicType);
  1040. return ret;
  1041. }
  1042. if (XSTRNCMP(info.name, "DES-CBC", 7) == 0) {
  1043. Des enc;
  1044. Des_SetKey(&enc, key, info.iv, DES_DECRYPTION);
  1045. Des_CbcDecrypt(&enc, der.buffer, der.buffer, der.length);
  1046. }
  1047. else if (XSTRNCMP(info.name, "DES-EDE3-CBC", 13) == 0) {
  1048. Des3 enc;
  1049. Des3_SetKey(&enc, key, info.iv, DES_DECRYPTION);
  1050. Des3_CbcDecrypt(&enc, der.buffer, der.buffer, der.length);
  1051. }
  1052. else if (XSTRNCMP(info.name, "AES-128-CBC", 13) == 0) {
  1053. Aes enc;
  1054. AesSetKey(&enc, key, AES_128_KEY_SIZE, info.iv, AES_DECRYPTION);
  1055. AesCbcDecrypt(&enc, der.buffer, der.buffer, der.length);
  1056. }
  1057. else if (XSTRNCMP(info.name, "AES-192-CBC", 13) == 0) {
  1058. Aes enc;
  1059. AesSetKey(&enc, key, AES_192_KEY_SIZE, info.iv, AES_DECRYPTION);
  1060. AesCbcDecrypt(&enc, der.buffer, der.buffer, der.length);
  1061. }
  1062. else if (XSTRNCMP(info.name, "AES-256-CBC", 13) == 0) {
  1063. Aes enc;
  1064. AesSetKey(&enc, key, AES_256_KEY_SIZE, info.iv, AES_DECRYPTION);
  1065. AesCbcDecrypt(&enc, der.buffer, der.buffer, der.length);
  1066. }
  1067. else {
  1068. XFREE(der.buffer, heap, dynamicType);
  1069. return SSL_BAD_FILE;
  1070. }
  1071. }
  1072. #endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
  1073. if (type == CA_TYPE) {
  1074. if (ctx == NULL) {
  1075. CYASSL_MSG("Need context for CA load");
  1076. XFREE(der.buffer, heap, dynamicType);
  1077. return BAD_FUNC_ARG;
  1078. }
  1079. return AddCA(ctx->cm, der, CYASSL_USER_CA, ctx->verifyPeer);
  1080. /* takes der over */
  1081. }
  1082. else if (type == CERT_TYPE) {
  1083. if (ssl) {
  1084. if (ssl->buffers.weOwnCert && ssl->buffers.certificate.buffer)
  1085. XFREE(ssl->buffers.certificate.buffer, heap,
  1086. dynamicType);
  1087. ssl->buffers.certificate = der;
  1088. ssl->buffers.weOwnCert = 1;
  1089. }
  1090. else if (ctx) {
  1091. if (ctx->certificate.buffer)
  1092. XFREE(ctx->certificate.buffer, heap, dynamicType);
  1093. ctx->certificate = der; /* takes der over */
  1094. }
  1095. }
  1096. else if (type == PRIVATEKEY_TYPE) {
  1097. if (ssl) {
  1098. if (ssl->buffers.weOwnKey && ssl->buffers.key.buffer)
  1099. XFREE(ssl->buffers.key.buffer, heap, dynamicType);
  1100. ssl->buffers.key = der;
  1101. ssl->buffers.weOwnKey = 1;
  1102. }
  1103. else if (ctx) {
  1104. if (ctx->privateKey.buffer)
  1105. XFREE(ctx->privateKey.buffer, heap, dynamicType);
  1106. ctx->privateKey = der; /* takes der over */
  1107. }
  1108. }
  1109. else {
  1110. XFREE(der.buffer, heap, dynamicType);
  1111. return SSL_BAD_CERTTYPE;
  1112. }
  1113. if (type == PRIVATEKEY_TYPE && format != SSL_FILETYPE_RAW) {
  1114. #ifndef NO_RSA
  1115. if (!eccKey) {
  1116. /* make sure RSA key can be used */
  1117. RsaKey key;
  1118. word32 idx = 0;
  1119. InitRsaKey(&key, 0);
  1120. if (RsaPrivateKeyDecode(der.buffer,&idx,&key,der.length) != 0) {
  1121. #ifdef HAVE_ECC
  1122. /* could have DER ECC (or pkcs8 ecc), no easy way to tell */
  1123. eccKey = 1; /* so try it out */
  1124. #endif
  1125. if (!eccKey) {
  1126. FreeRsaKey(&key);
  1127. return SSL_BAD_FILE;
  1128. }
  1129. }
  1130. FreeRsaKey(&key);
  1131. }
  1132. #endif
  1133. #ifdef HAVE_ECC
  1134. if (eccKey ) {
  1135. /* make sure ECC key can be used */
  1136. word32 idx = 0;
  1137. ecc_key key;
  1138. ecc_init(&key);
  1139. if (EccPrivateKeyDecode(der.buffer,&idx,&key,der.length) != 0) {
  1140. ecc_free(&key);
  1141. return SSL_BAD_FILE;
  1142. }
  1143. ecc_free(&key);
  1144. ctx->haveStaticECC = 1;
  1145. if (ssl)
  1146. ssl->options.haveStaticECC = 1;
  1147. }
  1148. #endif /* HAVE_ECC */
  1149. }
  1150. else if (type == CERT_TYPE) {
  1151. DecodedCert cert;
  1152. CYASSL_MSG("Checking cert signature type");
  1153. InitDecodedCert(&cert, der.buffer, der.length, heap);
  1154. if (DecodeToKey(&cert, 0) < 0) {
  1155. CYASSL_MSG("Decode to key failed");
  1156. return SSL_BAD_FILE;
  1157. }
  1158. switch (cert.signatureOID) {
  1159. case CTC_SHAwECDSA:
  1160. case CTC_SHA256wECDSA:
  1161. case CTC_SHA384wECDSA:
  1162. case CTC_SHA512wECDSA:
  1163. CYASSL_MSG("ECDSA cert signature");
  1164. if (ctx)
  1165. ctx->haveECDSAsig = 1;
  1166. if (ssl)
  1167. ssl->options.haveECDSAsig = 1;
  1168. break;
  1169. default:
  1170. CYASSL_MSG("Not ECDSA cert signature");
  1171. break;
  1172. }
  1173. FreeDecodedCert(&cert);
  1174. }
  1175. return SSL_SUCCESS;
  1176. }
  1177. /* CA PEM file for verification, may have multiple/chain certs to process */
  1178. static int ProcessChainBuffer(CYASSL_CTX* ctx, const unsigned char* buff,
  1179. long sz, int format, int type, CYASSL* ssl)
  1180. {
  1181. long used = 0;
  1182. int ret = 0;
  1183. int gotOne = 0;
  1184. CYASSL_MSG("Processing CA PEM file");
  1185. while (used < sz) {
  1186. long consumed = 0;
  1187. ret = ProcessBuffer(ctx, buff + used, sz - used, format, type, ssl,
  1188. &consumed, 0);
  1189. if (ret == SSL_NO_PEM_HEADER && gotOne) {
  1190. CYASSL_MSG("We got one good PEM file so stuff at end ok");
  1191. ret = SSL_SUCCESS;
  1192. break;
  1193. }
  1194. if (ret < 0)
  1195. break;
  1196. CYASSL_MSG(" Processed a CA");
  1197. gotOne = 1;
  1198. used += consumed;
  1199. }
  1200. return ret;
  1201. }
  1202. #ifndef NO_FILESYSTEM
  1203. #if defined(EBSNET)
  1204. #define XFILE int
  1205. #define XFOPEN(NAME, MODE) vf_open((const char *)NAME, VO_RDONLY, 0);
  1206. #define XFSEEK vf_lseek
  1207. #define XFTELL vf_tell
  1208. #define XREWIND vf_rewind
  1209. #define XFREAD(BUF, SZ, AMT, FD) vf_read(FD, BUF, SZ*AMT)
  1210. #define XFCLOSE vf_close
  1211. #define XSEEK_END VSEEK_END
  1212. #define XBADFILE -1
  1213. #elif defined(LSR_FS)
  1214. #include <fs.h>
  1215. #define XFILE struct fs_file*
  1216. #define XFOPEN(NAME, MODE) fs_open((char*)NAME);
  1217. #define XFSEEK(F, O, W) (void)F
  1218. #define XFTELL(F) (F)->len
  1219. #define XREWIND(F) (void)F
  1220. #define XFREAD(BUF, SZ, AMT, F) fs_read(F, (char*)BUF, SZ*AMT)
  1221. #define XFCLOSE fs_close
  1222. #define XSEEK_END 0
  1223. #define XBADFILE NULL
  1224. #elif defined(FREESCALE_MQX)
  1225. #define XFILE MQX_FILE_PTR
  1226. #define XFOPEN fopen
  1227. #define XFSEEK fseek
  1228. #define XFTELL ftell
  1229. #define XREWIND(F) fseek(F, 0, IO_SEEK_SET)
  1230. #define XFREAD fread
  1231. #define XFCLOSE fclose
  1232. #define XSEEK_END IO_SEEK_END
  1233. #define XBADFILE NULL
  1234. #elif defined(MICRIUM)
  1235. #include <fs.h>
  1236. #define XFILE FS_FILE*
  1237. #define XFOPEN fs_fopen
  1238. #define XFSEEK fs_fseek
  1239. #define XFTELL fs_ftell
  1240. #define XREWIND fs_rewind
  1241. #define XFREAD fs_fread
  1242. #define XFCLOSE fs_fclose
  1243. #define XSEEK_END FS_SEEK_END
  1244. #define XBADFILE NULL
  1245. #else
  1246. /* stdio, default case */
  1247. #define XFILE FILE*
  1248. #define XFOPEN fopen
  1249. #define XFSEEK fseek
  1250. #define XFTELL ftell
  1251. #define XREWIND rewind
  1252. #define XFREAD fread
  1253. #define XFCLOSE fclose
  1254. #define XSEEK_END SEEK_END
  1255. #define XBADFILE NULL
  1256. #endif
  1257. /* process a file with name fname into ctx of format and type
  1258. userChain specifies a user certificate chain to pass during handshake */
  1259. int ProcessFile(CYASSL_CTX* ctx, const char* fname, int format, int type,
  1260. CYASSL* ssl, int userChain, CYASSL_CRL* crl)
  1261. {
  1262. byte staticBuffer[FILE_BUFFER_SIZE];
  1263. byte* myBuffer = staticBuffer;
  1264. int dynamic = 0;
  1265. int ret;
  1266. long sz = 0;
  1267. XFILE file;
  1268. void* heapHint = ctx ? ctx->heap : NULL;
  1269. (void)crl;
  1270. (void)heapHint;
  1271. if (fname == NULL) return SSL_BAD_FILE;
  1272. file = XFOPEN(fname, "rb");
  1273. if (file == XBADFILE) return SSL_BAD_FILE;
  1274. XFSEEK(file, 0, XSEEK_END);
  1275. sz = XFTELL(file);
  1276. XREWIND(file);
  1277. if (sz > (long)sizeof(staticBuffer)) {
  1278. CYASSL_MSG("Getting dynamic buffer");
  1279. myBuffer = (byte*)XMALLOC(sz, heapHint, DYNAMIC_TYPE_FILE);
  1280. if (myBuffer == NULL) {
  1281. XFCLOSE(file);
  1282. return SSL_BAD_FILE;
  1283. }
  1284. dynamic = 1;
  1285. }
  1286. if ( (ret = (int)XFREAD(myBuffer, sz, 1, file)) < 0)
  1287. ret = SSL_BAD_FILE;
  1288. else {
  1289. if (type == CA_TYPE && format == SSL_FILETYPE_PEM)
  1290. ret = ProcessChainBuffer(ctx, myBuffer, sz, format, type, ssl);
  1291. #ifdef HAVE_CRL
  1292. else if (type == CRL_TYPE)
  1293. ret = BufferLoadCRL(crl, myBuffer, sz, format);
  1294. #endif
  1295. else
  1296. ret = ProcessBuffer(ctx, myBuffer, sz, format, type, ssl, NULL,
  1297. userChain);
  1298. }
  1299. XFCLOSE(file);
  1300. if (dynamic) XFREE(myBuffer, heapHint, DYNAMIC_TYPE_FILE);
  1301. return ret;
  1302. }
  1303. /* loads file then loads each file in path, no c_rehash */
  1304. int CyaSSL_CTX_load_verify_locations(CYASSL_CTX* ctx, const char* file,
  1305. const char* path)
  1306. {
  1307. int ret = SSL_SUCCESS;
  1308. CYASSL_ENTER("CyaSSL_CTX_load_verify_locations");
  1309. (void)path;
  1310. if (ctx == NULL || (file == NULL && path == NULL) )
  1311. return SSL_FAILURE;
  1312. if (file)
  1313. ret = ProcessFile(ctx, file, SSL_FILETYPE_PEM, CA_TYPE, NULL, 0, NULL);
  1314. if (ret == SSL_SUCCESS && path) {
  1315. /* try to load each regular file in path */
  1316. #ifdef USE_WINDOWS_API
  1317. WIN32_FIND_DATAA FindFileData;
  1318. HANDLE hFind;
  1319. char name[MAX_FILENAME_SZ];
  1320. XMEMSET(name, 0, sizeof(name));
  1321. XSTRNCPY(name, path, MAX_FILENAME_SZ - 4);
  1322. XSTRNCAT(name, "\\*", 3);
  1323. hFind = FindFirstFileA(name, &FindFileData);
  1324. if (hFind == INVALID_HANDLE_VALUE) {
  1325. CYASSL_MSG("FindFirstFile for path verify locations failed");
  1326. return BAD_PATH_ERROR;
  1327. }
  1328. do {
  1329. if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
  1330. XSTRNCPY(name, path, MAX_FILENAME_SZ/2 - 3);
  1331. XSTRNCAT(name, "\\", 2);
  1332. XSTRNCAT(name, FindFileData.cFileName, MAX_FILENAME_SZ/2);
  1333. ret = ProcessFile(ctx, name, SSL_FILETYPE_PEM, CA_TYPE, NULL,0,
  1334. NULL);
  1335. }
  1336. } while (ret == SSL_SUCCESS && FindNextFileA(hFind, &FindFileData));
  1337. FindClose(hFind);
  1338. #elif !defined(NO_CYASSL_DIR)
  1339. struct dirent* entry;
  1340. DIR* dir = opendir(path);
  1341. if (dir == NULL) {
  1342. CYASSL_MSG("opendir path verify locations failed");
  1343. return BAD_PATH_ERROR;
  1344. }
  1345. while ( ret == SSL_SUCCESS && (entry = readdir(dir)) != NULL) {
  1346. if (entry->d_type & DT_REG) {
  1347. char name[MAX_FILENAME_SZ];
  1348. XMEMSET(name, 0, sizeof(name));
  1349. XSTRNCPY(name, path, MAX_FILENAME_SZ/2 - 2);
  1350. XSTRNCAT(name, "/", 1);
  1351. XSTRNCAT(name, entry->d_name, MAX_FILENAME_SZ/2);
  1352. ret = ProcessFile(ctx, name, SSL_FILETYPE_PEM, CA_TYPE, NULL,0,
  1353. NULL);
  1354. }
  1355. }
  1356. closedir(dir);
  1357. #endif
  1358. }
  1359. return ret;
  1360. }
  1361. /* Verify the ceritficate, 1 for success, < 0 for error */
  1362. int CyaSSL_CertManagerVerifyBuffer(CYASSL_CERT_MANAGER* cm, const byte* buff,
  1363. int sz, int format)
  1364. {
  1365. int ret = 0;
  1366. int eccKey = 0; /* not used */
  1367. DecodedCert cert;
  1368. buffer der;
  1369. CYASSL_ENTER("CyaSSL_CertManagerVerifyBuffer");
  1370. der.buffer = NULL;
  1371. der.length = 0;
  1372. if (format == SSL_FILETYPE_PEM) {
  1373. EncryptedInfo info;
  1374. info.set = 0;
  1375. info.ctx = NULL;
  1376. info.consumed = 0;
  1377. ret = PemToDer(buff, sz, CERT_TYPE, &der, cm->heap, &info, &eccKey);
  1378. InitDecodedCert(&cert, der.buffer, der.length, cm->heap);
  1379. }
  1380. else
  1381. InitDecodedCert(&cert, (byte*)buff, sz, cm->heap);
  1382. if (ret == 0)
  1383. ret = ParseCertRelative(&cert, CERT_TYPE, 1, cm);
  1384. #ifdef HAVE_CRL
  1385. if (ret == 0 && cm->crlEnabled)
  1386. ret = CheckCertCRL(cm->crl, &cert);
  1387. #endif
  1388. FreeDecodedCert(&cert);
  1389. XFREE(der.buffer, cm->heap, DYNAMIC_TYPE_CERT);
  1390. return ret;
  1391. }
  1392. /* Verify the ceritficate, 1 for success, < 0 for error */
  1393. int CyaSSL_CertManagerVerify(CYASSL_CERT_MANAGER* cm, const char* fname,
  1394. int format)
  1395. {
  1396. int ret = SSL_FATAL_ERROR;
  1397. byte staticBuffer[FILE_BUFFER_SIZE];
  1398. byte* myBuffer = staticBuffer;
  1399. int dynamic = 0;
  1400. long sz = 0;
  1401. XFILE file = XFOPEN(fname, "rb");
  1402. CYASSL_ENTER("CyaSSL_CertManagerVerify");
  1403. if (file == XBADFILE) return SSL_BAD_FILE;
  1404. XFSEEK(file, 0, XSEEK_END);
  1405. sz = XFTELL(file);
  1406. XREWIND(file);
  1407. if (sz > (long)sizeof(staticBuffer)) {
  1408. CYASSL_MSG("Getting dynamic buffer");
  1409. myBuffer = (byte*) XMALLOC(sz, cm->heap, DYNAMIC_TYPE_FILE);
  1410. if (myBuffer == NULL) {
  1411. XFCLOSE(file);
  1412. return SSL_BAD_FILE;
  1413. }
  1414. dynamic = 1;
  1415. }
  1416. if ( (ret = (int)XFREAD(myBuffer, sz, 1, file)) < 0)
  1417. ret = SSL_BAD_FILE;
  1418. else
  1419. ret = CyaSSL_CertManagerVerifyBuffer(cm, myBuffer, (int)sz, format);
  1420. XFCLOSE(file);
  1421. if (dynamic) XFREE(myBuffer, cm->heap, DYNAMIC_TYPE_FILE);
  1422. if (ret == 0)
  1423. return SSL_SUCCESS;
  1424. return ret;
  1425. }
  1426. /* like load verify locations, 1 for success, < 0 for error */
  1427. int CyaSSL_CertManagerLoadCA(CYASSL_CERT_MANAGER* cm, const char* file,
  1428. const char* path)
  1429. {
  1430. int ret = SSL_FATAL_ERROR;
  1431. CYASSL_CTX* tmp;
  1432. CYASSL_ENTER("CyaSSL_CertManagerLoadCA");
  1433. if (cm == NULL) {
  1434. CYASSL_MSG("No CertManager error");
  1435. return ret;
  1436. }
  1437. tmp = CyaSSL_CTX_new(CyaSSLv3_client_method());
  1438. if (tmp == NULL) {
  1439. CYASSL_MSG("CTX new failed");
  1440. return ret;
  1441. }
  1442. /* for tmp use */
  1443. CyaSSL_CertManagerFree(tmp->cm);
  1444. tmp->cm = cm;
  1445. ret = CyaSSL_CTX_load_verify_locations(tmp, file, path);
  1446. /* don't loose our good one */
  1447. tmp->cm = NULL;
  1448. CyaSSL_CTX_free(tmp);
  1449. return ret;
  1450. }
  1451. /* turn on CRL if off and compiled in, set options */
  1452. int CyaSSL_CertManagerEnableCRL(CYASSL_CERT_MANAGER* cm, int options)
  1453. {
  1454. int ret = SSL_SUCCESS;
  1455. (void)options;
  1456. CYASSL_ENTER("CyaSSL_CertManagerEnableCRL");
  1457. if (cm == NULL)
  1458. return BAD_FUNC_ARG;
  1459. #ifdef HAVE_CRL
  1460. if (cm->crl == NULL) {
  1461. cm->crl = (CYASSL_CRL*)XMALLOC(sizeof(CYASSL_CRL), cm->heap,
  1462. DYNAMIC_TYPE_CRL);
  1463. if (cm->crl == NULL)
  1464. return MEMORY_E;
  1465. if (InitCRL(cm->crl, cm) != 0) {
  1466. CYASSL_MSG("Init CRL failed");
  1467. FreeCRL(cm->crl, 1);
  1468. cm->crl = NULL;
  1469. return SSL_FAILURE;
  1470. }
  1471. }
  1472. cm->crlEnabled = 1;
  1473. if (options & CYASSL_CRL_CHECKALL)
  1474. cm->crlCheckAll = 1;
  1475. #else
  1476. ret = NOT_COMPILED_IN;
  1477. #endif
  1478. return ret;
  1479. }
  1480. int CyaSSL_CertManagerDisableCRL(CYASSL_CERT_MANAGER* cm)
  1481. {
  1482. CYASSL_ENTER("CyaSSL_CertManagerDisableCRL");
  1483. if (cm == NULL)
  1484. return BAD_FUNC_ARG;
  1485. cm->crlEnabled = 0;
  1486. return SSL_SUCCESS;
  1487. }
  1488. int CyaSSL_CTX_check_private_key(CYASSL_CTX* ctx)
  1489. {
  1490. /* TODO: check private against public for RSA match */
  1491. (void)ctx;
  1492. CYASSL_ENTER("SSL_CTX_check_private_key");
  1493. return SSL_SUCCESS;
  1494. }
  1495. #ifdef HAVE_CRL
  1496. /* check CRL if enabled, SSL_SUCCESS */
  1497. int CyaSSL_CertManagerCheckCRL(CYASSL_CERT_MANAGER* cm, byte* der, int sz)
  1498. {
  1499. int ret;
  1500. DecodedCert cert;
  1501. CYASSL_ENTER("CyaSSL_CertManagerCheckCRL");
  1502. if (cm == NULL)
  1503. return BAD_FUNC_ARG;
  1504. if (cm->crlEnabled == 0)
  1505. return SSL_SUCCESS;
  1506. InitDecodedCert(&cert, der, sz, NULL);
  1507. ret = ParseCertRelative(&cert, CERT_TYPE, NO_VERIFY, cm);
  1508. if (ret != 0) {
  1509. CYASSL_MSG("ParseCert failed");
  1510. return ret;
  1511. }
  1512. else {
  1513. ret = CheckCertCRL(cm->crl, &cert);
  1514. if (ret != 0) {
  1515. CYASSL_MSG("CheckCertCRL failed");
  1516. }
  1517. }
  1518. FreeDecodedCert(&cert);
  1519. if (ret == 0)
  1520. return SSL_SUCCESS; /* convert */
  1521. return ret;
  1522. }
  1523. int CyaSSL_CertManagerSetCRL_Cb(CYASSL_CERT_MANAGER* cm, CbMissingCRL cb)
  1524. {
  1525. CYASSL_ENTER("CyaSSL_CertManagerSetCRL_Cb");
  1526. if (cm == NULL)
  1527. return BAD_FUNC_ARG;
  1528. cm->cbMissingCRL = cb;
  1529. return SSL_SUCCESS;
  1530. }
  1531. int CyaSSL_CertManagerLoadCRL(CYASSL_CERT_MANAGER* cm, const char* path,
  1532. int type, int monitor)
  1533. {
  1534. CYASSL_ENTER("CyaSSL_CertManagerLoadCRL");
  1535. if (cm == NULL)
  1536. return BAD_FUNC_ARG;
  1537. if (cm->crl == NULL) {
  1538. if (CyaSSL_CertManagerEnableCRL(cm, 0) != SSL_SUCCESS) {
  1539. CYASSL_MSG("Enable CRL failed");
  1540. return -1;
  1541. }
  1542. }
  1543. return LoadCRL(cm->crl, path, type, monitor);
  1544. }
  1545. int CyaSSL_EnableCRL(CYASSL* ssl, int options)
  1546. {
  1547. CYASSL_ENTER("CyaSSL_EnableCRL");
  1548. if (ssl)
  1549. return CyaSSL_CertManagerEnableCRL(ssl->ctx->cm, options);
  1550. else
  1551. return BAD_FUNC_ARG;
  1552. }
  1553. int CyaSSL_DisableCRL(CYASSL* ssl)
  1554. {
  1555. CYASSL_ENTER("CyaSSL_DisableCRL");
  1556. if (ssl)
  1557. return CyaSSL_CertManagerDisableCRL(ssl->ctx->cm);
  1558. else
  1559. return BAD_FUNC_ARG;
  1560. }
  1561. int CyaSSL_LoadCRL(CYASSL* ssl, const char* path, int type, int monitor)
  1562. {
  1563. CYASSL_ENTER("CyaSSL_LoadCRL");
  1564. if (ssl)
  1565. return CyaSSL_CertManagerLoadCRL(ssl->ctx->cm, path, type, monitor);
  1566. else
  1567. return BAD_FUNC_ARG;
  1568. }
  1569. int CyaSSL_SetCRL_Cb(CYASSL* ssl, CbMissingCRL cb)
  1570. {
  1571. CYASSL_ENTER("CyaSSL_SetCRL_Cb");
  1572. if (ssl)
  1573. return CyaSSL_CertManagerSetCRL_Cb(ssl->ctx->cm, cb);
  1574. else
  1575. return BAD_FUNC_ARG;
  1576. }
  1577. int CyaSSL_CTX_EnableCRL(CYASSL_CTX* ctx, int options)
  1578. {
  1579. CYASSL_ENTER("CyaSSL_CTX_EnableCRL");
  1580. if (ctx)
  1581. return CyaSSL_CertManagerEnableCRL(ctx->cm, options);
  1582. else
  1583. return BAD_FUNC_ARG;
  1584. }
  1585. int CyaSSL_CTX_DisableCRL(CYASSL_CTX* ctx)
  1586. {
  1587. CYASSL_ENTER("CyaSSL_CTX_DisableCRL");
  1588. if (ctx)
  1589. return CyaSSL_CertManagerDisableCRL(ctx->cm);
  1590. else
  1591. return BAD_FUNC_ARG;
  1592. }
  1593. int CyaSSL_CTX_LoadCRL(CYASSL_CTX* ctx, const char* path, int type, int monitor)
  1594. {
  1595. CYASSL_ENTER("CyaSSL_CTX_LoadCRL");
  1596. if (ctx)
  1597. return CyaSSL_CertManagerLoadCRL(ctx->cm, path, type, monitor);
  1598. else
  1599. return BAD_FUNC_ARG;
  1600. }
  1601. int CyaSSL_CTX_SetCRL_Cb(CYASSL_CTX* ctx, CbMissingCRL cb)
  1602. {
  1603. CYASSL_ENTER("CyaSSL_CTX_SetCRL_Cb");
  1604. if (ctx)
  1605. return CyaSSL_CertManagerSetCRL_Cb(ctx->cm, cb);
  1606. else
  1607. return BAD_FUNC_ARG;
  1608. }
  1609. #endif /* HAVE_CRL */
  1610. #ifdef CYASSL_DER_LOAD
  1611. /* Add format parameter to allow DER load of CA files */
  1612. int CyaSSL_CTX_der_load_verify_locations(CYASSL_CTX* ctx, const char* file,
  1613. int format)
  1614. {
  1615. CYASSL_ENTER("CyaSSL_CTX_der_load_verify_locations");
  1616. if (ctx == NULL || file == NULL)
  1617. return SSL_FAILURE;
  1618. if (ProcessFile(ctx, file, format, CA_TYPE, NULL, 0, NULL) == SSL_SUCCESS)
  1619. return SSL_SUCCESS;
  1620. return SSL_FAILURE;
  1621. }
  1622. #endif /* CYASSL_DER_LOAD */
  1623. #ifdef CYASSL_CERT_GEN
  1624. /* load pem cert from file into der buffer, return der size or error */
  1625. int CyaSSL_PemCertToDer(const char* fileName, unsigned char* derBuf, int derSz)
  1626. {
  1627. byte staticBuffer[FILE_BUFFER_SIZE];
  1628. byte* fileBuf = staticBuffer;
  1629. int dynamic = 0;
  1630. int ret;
  1631. int ecc = 0;
  1632. long sz = 0;
  1633. XFILE file = XFOPEN(fileName, "rb");
  1634. EncryptedInfo info;
  1635. buffer converted;
  1636. CYASSL_ENTER("CyaSSL_PemCertToDer");
  1637. converted.buffer = 0;
  1638. if (file == XBADFILE) return SSL_BAD_FILE;
  1639. XFSEEK(file, 0, XSEEK_END);
  1640. sz = XFTELL(file);
  1641. XREWIND(file);
  1642. if (sz > (long)sizeof(staticBuffer)) {
  1643. fileBuf = (byte*) XMALLOC(sz, 0, DYNAMIC_TYPE_FILE);
  1644. if (fileBuf == NULL) {
  1645. XFCLOSE(file);
  1646. return SSL_BAD_FILE;
  1647. }
  1648. dynamic = 1;
  1649. }
  1650. if ( (ret = (int)XFREAD(fileBuf, sz, 1, file)) < 0)
  1651. ret = SSL_BAD_FILE;
  1652. else
  1653. ret = PemToDer(fileBuf, sz, CA_TYPE, &converted, 0, &info, &ecc);
  1654. if (ret == 0) {
  1655. if (converted.length < (word32)derSz) {
  1656. XMEMCPY(derBuf, converted.buffer, converted.length);
  1657. ret = converted.length;
  1658. }
  1659. else
  1660. ret = BUFFER_E;
  1661. }
  1662. XFREE(converted.buffer, 0, DYNAMIC_TYPE_CA);
  1663. if (dynamic)
  1664. XFREE(fileBuf, 0, DYNAMIC_TYPE_FILE);
  1665. XFCLOSE(file);
  1666. return ret;
  1667. }
  1668. #endif /* CYASSL_CERT_GEN */
  1669. int CyaSSL_CTX_use_certificate_file(CYASSL_CTX* ctx, const char* file,
  1670. int format)
  1671. {
  1672. CYASSL_ENTER("CyaSSL_CTX_use_certificate_file");
  1673. if (ProcessFile(ctx, file, format, CERT_TYPE, NULL, 0, NULL) == SSL_SUCCESS)
  1674. return SSL_SUCCESS;
  1675. return SSL_FAILURE;
  1676. }
  1677. int CyaSSL_CTX_use_PrivateKey_file(CYASSL_CTX* ctx, const char* file,int format)
  1678. {
  1679. CYASSL_ENTER("CyaSSL_CTX_use_PrivateKey_file");
  1680. if (ProcessFile(ctx, file, format, PRIVATEKEY_TYPE, NULL, 0, NULL)
  1681. == SSL_SUCCESS)
  1682. return SSL_SUCCESS;
  1683. return SSL_FAILURE;
  1684. }
  1685. int CyaSSL_CTX_use_certificate_chain_file(CYASSL_CTX* ctx, const char* file)
  1686. {
  1687. /* procces up to MAX_CHAIN_DEPTH plus subject cert */
  1688. CYASSL_ENTER("CyaSSL_CTX_use_certificate_chain_file");
  1689. if (ProcessFile(ctx, file, SSL_FILETYPE_PEM,CERT_TYPE,NULL,1, NULL)
  1690. == SSL_SUCCESS)
  1691. return SSL_SUCCESS;
  1692. return SSL_FAILURE;
  1693. }
  1694. #ifdef OPENSSL_EXTRA
  1695. /* put SSL type in extra for now, not very common */
  1696. int CyaSSL_use_certificate_file(CYASSL* ssl, const char* file, int format)
  1697. {
  1698. CYASSL_ENTER("CyaSSL_use_certificate_file");
  1699. if (ProcessFile(ssl->ctx, file, format, CERT_TYPE, ssl, 0, NULL)
  1700. == SSL_SUCCESS)
  1701. return SSL_SUCCESS;
  1702. return SSL_FAILURE;
  1703. }
  1704. int CyaSSL_use_PrivateKey_file(CYASSL* ssl, const char* file, int format)
  1705. {
  1706. CYASSL_ENTER("CyaSSL_use_PrivateKey_file");
  1707. if (ProcessFile(ssl->ctx, file, format, PRIVATEKEY_TYPE, ssl, 0, NULL)
  1708. == SSL_SUCCESS)
  1709. return SSL_SUCCESS;
  1710. return SSL_FAILURE;
  1711. }
  1712. int CyaSSL_use_certificate_chain_file(CYASSL* ssl, const char* file)
  1713. {
  1714. /* procces up to MAX_CHAIN_DEPTH plus subject cert */
  1715. CYASSL_ENTER("CyaSSL_use_certificate_chain_file");
  1716. if (ProcessFile(ssl->ctx, file, SSL_FILETYPE_PEM, CERT_TYPE, ssl, 1, NULL)
  1717. == SSL_SUCCESS)
  1718. return SSL_SUCCESS;
  1719. return SSL_FAILURE;
  1720. }
  1721. /* server wrapper for ctx or ssl Diffie-Hellman parameters */
  1722. static int CyaSSL_SetTmpDH_buffer_wrapper(CYASSL_CTX* ctx, CYASSL* ssl,
  1723. const unsigned char* buf, long sz, int format)
  1724. {
  1725. buffer der;
  1726. int ret;
  1727. int weOwnDer = 0;
  1728. byte p[MAX_DH_SIZE];
  1729. byte g[MAX_DH_SIZE];
  1730. word32 pSz = sizeof(p);
  1731. word32 gSz = sizeof(g);
  1732. der.buffer = (byte*)buf;
  1733. der.length = (word32)sz;
  1734. if (format != SSL_FILETYPE_ASN1 && format != SSL_FILETYPE_PEM)
  1735. return SSL_BAD_FILETYPE;
  1736. if (format == SSL_FILETYPE_PEM) {
  1737. der.buffer = NULL;
  1738. ret = PemToDer(buf, sz, DH_PARAM_TYPE, &der, ctx->heap, NULL,NULL);
  1739. if (ret < 0) {
  1740. XFREE(der.buffer, ctx->heap, DYNAMIC_TYPE_KEY);
  1741. return ret;
  1742. }
  1743. weOwnDer = 1;
  1744. }
  1745. if (DhParamsLoad(der.buffer, der.length, p, &pSz, g, &gSz) < 0)
  1746. ret = SSL_BAD_FILETYPE;
  1747. else {
  1748. if (ssl)
  1749. ret = CyaSSL_SetTmpDH(ssl, p, pSz, g, gSz);
  1750. else
  1751. ret = CyaSSL_CTX_SetTmpDH(ctx, p, pSz, g, gSz);
  1752. }
  1753. if (weOwnDer)
  1754. XFREE(der.buffer, ctx->heap, DYNAMIC_TYPE_KEY);
  1755. return ret;
  1756. }
  1757. /* server Diffie-Hellman parameters */
  1758. int CyaSSL_SetTmpDH_buffer(CYASSL* ssl, const unsigned char* buf, long sz,
  1759. int format)
  1760. {
  1761. return CyaSSL_SetTmpDH_buffer_wrapper(ssl->ctx, ssl, buf, sz, format);
  1762. }
  1763. /* server ctx Diffie-Hellman parameters */
  1764. int CyaSSL_CTX_SetTmpDH_buffer(CYASSL_CTX* ctx, const unsigned char* buf,
  1765. long sz, int format)
  1766. {
  1767. return CyaSSL_SetTmpDH_buffer_wrapper(ctx, NULL, buf, sz, format);
  1768. }
  1769. #ifdef HAVE_ECC
  1770. /* Set Temp CTX EC-DHE size in octets, should be 20 - 66 for 160 - 521 bit */
  1771. int CyaSSL_CTX_SetTmpEC_DHE_Sz(CYASSL_CTX* ctx, word16 sz)
  1772. {
  1773. if (ctx == NULL || sz < ECC_MINSIZE || sz > ECC_MAXSIZE)
  1774. return BAD_FUNC_ARG;
  1775. ctx->eccTempKeySz = sz;
  1776. return SSL_SUCCESS;
  1777. }
  1778. /* Set Temp SSL EC-DHE size in octets, should be 20 - 66 for 160 - 521 bit */
  1779. int CyaSSL_SetTmpEC_DHE_Sz(CYASSL* ssl, word16 sz)
  1780. {
  1781. if (ssl == NULL || sz < ECC_MINSIZE || sz > ECC_MAXSIZE)
  1782. return BAD_FUNC_ARG;
  1783. ssl->eccTempKeySz = sz;
  1784. return SSL_SUCCESS;
  1785. }
  1786. #endif /* HAVE_ECC */
  1787. #if !defined(NO_FILESYSTEM)
  1788. /* server Diffie-Hellman parameters */
  1789. static int CyaSSL_SetTmpDH_file_wrapper(CYASSL_CTX* ctx, CYASSL* ssl,
  1790. const char* fname, int format)
  1791. {
  1792. byte staticBuffer[FILE_BUFFER_SIZE];
  1793. byte* myBuffer = staticBuffer;
  1794. int dynamic = 0;
  1795. int ret;
  1796. long sz = 0;
  1797. XFILE file = XFOPEN(fname, "rb");
  1798. if (file == XBADFILE) return SSL_BAD_FILE;
  1799. XFSEEK(file, 0, XSEEK_END);
  1800. sz = XFTELL(file);
  1801. XREWIND(file);
  1802. if (sz > (long)sizeof(staticBuffer)) {
  1803. CYASSL_MSG("Getting dynamic buffer");
  1804. myBuffer = (byte*) XMALLOC(sz, ctx->heap, DYNAMIC_TYPE_FILE);
  1805. if (myBuffer == NULL) {
  1806. XFCLOSE(file);
  1807. return SSL_BAD_FILE;
  1808. }
  1809. dynamic = 1;
  1810. }
  1811. if ( (ret = (int)XFREAD(myBuffer, sz, 1, file)) < 0)
  1812. ret = SSL_BAD_FILE;
  1813. else {
  1814. if (ssl)
  1815. ret = CyaSSL_SetTmpDH_buffer(ssl, myBuffer, sz, format);
  1816. else
  1817. ret = CyaSSL_CTX_SetTmpDH_buffer(ctx, myBuffer, sz, format);
  1818. }
  1819. XFCLOSE(file);
  1820. if (dynamic) XFREE(myBuffer, ctx->heap, DYNAMIC_TYPE_FILE);
  1821. return ret;
  1822. }
  1823. /* server Diffie-Hellman parameters */
  1824. int CyaSSL_SetTmpDH_file(CYASSL* ssl, const char* fname, int format)
  1825. {
  1826. return CyaSSL_SetTmpDH_file_wrapper(ssl->ctx, ssl, fname, format);
  1827. }
  1828. /* server Diffie-Hellman parameters */
  1829. int CyaSSL_CTX_SetTmpDH_file(CYASSL_CTX* ctx, const char* fname, int format)
  1830. {
  1831. return CyaSSL_SetTmpDH_file_wrapper(ctx, NULL, fname, format);
  1832. }
  1833. #endif /* !NO_FILESYSTEM */
  1834. #endif /* OPENSSL_EXTRA */
  1835. #ifdef HAVE_NTRU
  1836. int CyaSSL_CTX_use_NTRUPrivateKey_file(CYASSL_CTX* ctx, const char* file)
  1837. {
  1838. CYASSL_ENTER("CyaSSL_CTX_use_NTRUPrivateKey_file");
  1839. if (ProcessFile(ctx, file, SSL_FILETYPE_RAW, PRIVATEKEY_TYPE, NULL, 0, NULL)
  1840. == SSL_SUCCESS) {
  1841. ctx->haveNTRU = 1;
  1842. return SSL_SUCCESS;
  1843. }
  1844. return SSL_FAILURE;
  1845. }
  1846. #endif /* HAVE_NTRU */
  1847. #if defined(OPENSSL_EXTRA)
  1848. int CyaSSL_CTX_use_RSAPrivateKey_file(CYASSL_CTX* ctx,const char* file,
  1849. int format)
  1850. {
  1851. CYASSL_ENTER("SSL_CTX_use_RSAPrivateKey_file");
  1852. return CyaSSL_CTX_use_PrivateKey_file(ctx, file, format);
  1853. }
  1854. int CyaSSL_use_RSAPrivateKey_file(CYASSL* ssl, const char* file, int format)
  1855. {
  1856. CYASSL_ENTER("CyaSSL_use_RSAPrivateKey_file");
  1857. return CyaSSL_use_PrivateKey_file(ssl, file, format);
  1858. }
  1859. #endif /* OPENSSL_EXTRA */
  1860. #endif /* NO_FILESYSTEM */
  1861. void CyaSSL_CTX_set_verify(CYASSL_CTX* ctx, int mode, VerifyCallback vc)
  1862. {
  1863. CYASSL_ENTER("CyaSSL_CTX_set_verify");
  1864. if (mode & SSL_VERIFY_PEER) {
  1865. ctx->verifyPeer = 1;
  1866. ctx->verifyNone = 0; /* in case perviously set */
  1867. }
  1868. if (mode == SSL_VERIFY_NONE) {
  1869. ctx->verifyNone = 1;
  1870. ctx->verifyPeer = 0; /* in case previously set */
  1871. }
  1872. if (mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  1873. ctx->failNoCert = 1;
  1874. ctx->verifyCallback = vc;
  1875. }
  1876. void CyaSSL_set_verify(CYASSL* ssl, int mode, VerifyCallback vc)
  1877. {
  1878. CYASSL_ENTER("CyaSSL_set_verify");
  1879. if (mode & SSL_VERIFY_PEER) {
  1880. ssl->options.verifyPeer = 1;
  1881. ssl->options.verifyNone = 0; /* in case perviously set */
  1882. }
  1883. if (mode == SSL_VERIFY_NONE) {
  1884. ssl->options.verifyNone = 1;
  1885. ssl->options.verifyPeer = 0; /* in case previously set */
  1886. }
  1887. if (mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  1888. ssl->options.failNoCert = 1;
  1889. ssl->verifyCallback = vc;
  1890. }
  1891. /* store context CA Cache addition callback */
  1892. void CyaSSL_CTX_SetCACb(CYASSL_CTX* ctx, CallbackCACache cb)
  1893. {
  1894. if (ctx && ctx->cm)
  1895. ctx->cm->caCacheCallback = cb;
  1896. }
  1897. #endif /* !NO_CERTS */
  1898. #ifndef NO_SESSION_CACHE
  1899. CYASSL_SESSION* CyaSSL_get_session(CYASSL* ssl)
  1900. {
  1901. CYASSL_ENTER("SSL_get_session");
  1902. if (ssl)
  1903. return GetSession(ssl, 0);
  1904. return NULL;
  1905. }
  1906. int CyaSSL_set_session(CYASSL* ssl, CYASSL_SESSION* session)
  1907. {
  1908. CYASSL_ENTER("SSL_set_session");
  1909. if (session)
  1910. return SetSession(ssl, session);
  1911. return SSL_FAILURE;
  1912. }
  1913. #endif /* NO_SESSION_CACHE */
  1914. void CyaSSL_load_error_strings(void) /* compatibility only */
  1915. {}
  1916. int CyaSSL_library_init(void)
  1917. {
  1918. CYASSL_ENTER("SSL_library_init");
  1919. if (CyaSSL_Init() == 0)
  1920. return SSL_SUCCESS;
  1921. else
  1922. return SSL_FATAL_ERROR;
  1923. }
  1924. #ifndef NO_SESSION_CACHE
  1925. /* on by default if built in but allow user to turn off */
  1926. long CyaSSL_CTX_set_session_cache_mode(CYASSL_CTX* ctx, long mode)
  1927. {
  1928. CYASSL_ENTER("SSL_CTX_set_session_cache_mode");
  1929. if (mode == SSL_SESS_CACHE_OFF)
  1930. ctx->sessionCacheOff = 1;
  1931. if (mode == SSL_SESS_CACHE_NO_AUTO_CLEAR)
  1932. ctx->sessionCacheFlushOff = 1;
  1933. return SSL_SUCCESS;
  1934. }
  1935. #endif /* NO_SESSION_CACHE */
  1936. int CyaSSL_CTX_set_cipher_list(CYASSL_CTX* ctx, const char* list)
  1937. {
  1938. CYASSL_ENTER("CyaSSL_CTX_set_cipher_list");
  1939. if (SetCipherList(&ctx->suites, list))
  1940. return SSL_SUCCESS;
  1941. else
  1942. return SSL_FAILURE;
  1943. }
  1944. int CyaSSL_set_cipher_list(CYASSL* ssl, const char* list)
  1945. {
  1946. CYASSL_ENTER("CyaSSL_set_cipher_list");
  1947. if (SetCipherList(ssl->suites, list)) {
  1948. byte haveRSA = 1;
  1949. byte havePSK = 0;
  1950. #ifdef NO_RSA
  1951. haveRSA = 0;
  1952. #endif
  1953. #ifndef NO_PSK
  1954. havePSK = ssl->options.havePSK;
  1955. #endif
  1956. InitSuites(ssl->suites, ssl->version, haveRSA, havePSK,
  1957. ssl->options.haveDH, ssl->options.haveNTRU,
  1958. ssl->options.haveECDSAsig, ssl->options.haveStaticECC,
  1959. ssl->options.side);
  1960. return SSL_SUCCESS;
  1961. }
  1962. else
  1963. return SSL_FAILURE;
  1964. }
  1965. #ifndef CYASSL_LEANPSK
  1966. int CyaSSL_dtls_get_current_timeout(CYASSL* ssl)
  1967. {
  1968. (void)ssl;
  1969. #ifdef CYASSL_DTLS
  1970. return ssl->dtls_timeout;
  1971. #else
  1972. return NOT_COMPILED_IN;
  1973. #endif
  1974. }
  1975. int CyaSSL_dtls_got_timeout(CYASSL* ssl)
  1976. {
  1977. #ifdef CYASSL_DTLS
  1978. int result = SSL_SUCCESS;
  1979. if (DtlsPoolTimeout(ssl) < 0 || DtlsPoolSend(ssl) < 0) {
  1980. result = SSL_FATAL_ERROR;
  1981. }
  1982. return result;
  1983. #else
  1984. (void)ssl;
  1985. return NOT_COMPILED_IN;
  1986. #endif
  1987. }
  1988. #endif
  1989. /* client only parts */
  1990. #ifndef NO_CYASSL_CLIENT
  1991. #ifndef NO_OLD_TLS
  1992. CYASSL_METHOD* CyaSSLv3_client_method(void)
  1993. {
  1994. CYASSL_METHOD* method =
  1995. (CYASSL_METHOD*) XMALLOC(sizeof(CYASSL_METHOD), 0,
  1996. DYNAMIC_TYPE_METHOD);
  1997. CYASSL_ENTER("SSLv3_client_method");
  1998. if (method)
  1999. InitSSL_Method(method, MakeSSLv3());
  2000. return method;
  2001. }
  2002. #endif
  2003. #ifdef CYASSL_DTLS
  2004. CYASSL_METHOD* CyaDTLSv1_client_method(void)
  2005. {
  2006. CYASSL_METHOD* method =
  2007. (CYASSL_METHOD*) XMALLOC(sizeof(CYASSL_METHOD), 0,
  2008. DYNAMIC_TYPE_METHOD);
  2009. CYASSL_ENTER("DTLSv1_client_method");
  2010. if (method)
  2011. InitSSL_Method(method, MakeDTLSv1());
  2012. return method;
  2013. }
  2014. #endif
  2015. /* please see note at top of README if you get an error from connect */
  2016. int CyaSSL_connect(CYASSL* ssl)
  2017. {
  2018. int neededState;
  2019. CYASSL_ENTER("SSL_connect()");
  2020. #ifdef HAVE_ERRNO_H
  2021. errno = 0;
  2022. #endif
  2023. if (ssl->options.side != CLIENT_END) {
  2024. CYASSL_ERROR(ssl->error = SIDE_ERROR);
  2025. return SSL_FATAL_ERROR;
  2026. }
  2027. #ifdef CYASSL_DTLS
  2028. if (ssl->version.major == DTLS_MAJOR &&
  2029. ssl->version.minor == DTLS_MINOR) {
  2030. ssl->options.dtls = 1;
  2031. ssl->options.tls = 1;
  2032. ssl->options.tls1_1 = 1;
  2033. if (DtlsPoolInit(ssl) != 0) {
  2034. ssl->error = MEMORY_ERROR;
  2035. CYASSL_ERROR(ssl->error);
  2036. return SSL_FATAL_ERROR;
  2037. }
  2038. }
  2039. #endif
  2040. if (ssl->buffers.outputBuffer.length > 0) {
  2041. if ( (ssl->error = SendBuffered(ssl)) == 0) {
  2042. ssl->options.connectState++;
  2043. CYASSL_MSG("connect state: Advanced from buffered send");
  2044. }
  2045. else {
  2046. CYASSL_ERROR(ssl->error);
  2047. return SSL_FATAL_ERROR;
  2048. }
  2049. }
  2050. switch (ssl->options.connectState) {
  2051. case CONNECT_BEGIN :
  2052. /* always send client hello first */
  2053. if ( (ssl->error = SendClientHello(ssl)) != 0) {
  2054. CYASSL_ERROR(ssl->error);
  2055. return SSL_FATAL_ERROR;
  2056. }
  2057. ssl->options.connectState = CLIENT_HELLO_SENT;
  2058. CYASSL_MSG("connect state: CLIENT_HELLO_SENT");
  2059. case CLIENT_HELLO_SENT :
  2060. neededState = ssl->options.resuming ? SERVER_FINISHED_COMPLETE :
  2061. SERVER_HELLODONE_COMPLETE;
  2062. #ifdef CYASSL_DTLS
  2063. /* In DTLS, when resuming, we can go straight to FINISHED,
  2064. * or do a cookie exchange and then skip to FINISHED, assume
  2065. * we need the cookie exchange first. */
  2066. if (ssl->options.dtls)
  2067. neededState = SERVER_HELLOVERIFYREQUEST_COMPLETE;
  2068. #endif
  2069. /* get response */
  2070. while (ssl->options.serverState < neededState) {
  2071. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  2072. CYASSL_ERROR(ssl->error);
  2073. return SSL_FATAL_ERROR;
  2074. }
  2075. /* if resumption failed, reset needed state */
  2076. else if (neededState == SERVER_FINISHED_COMPLETE)
  2077. if (!ssl->options.resuming) {
  2078. if (!ssl->options.dtls)
  2079. neededState = SERVER_HELLODONE_COMPLETE;
  2080. else
  2081. neededState = SERVER_HELLOVERIFYREQUEST_COMPLETE;
  2082. }
  2083. }
  2084. ssl->options.connectState = HELLO_AGAIN;
  2085. CYASSL_MSG("connect state: HELLO_AGAIN");
  2086. case HELLO_AGAIN :
  2087. if (ssl->options.certOnly)
  2088. return SSL_SUCCESS;
  2089. #ifdef CYASSL_DTLS
  2090. if (ssl->options.dtls) {
  2091. /* re-init hashes, exclude first hello and verify request */
  2092. InitMd5(&ssl->hashMd5);
  2093. InitSha(&ssl->hashSha);
  2094. #ifndef NO_SHA256
  2095. if (IsAtLeastTLSv1_2(ssl))
  2096. InitSha256(&ssl->hashSha256);
  2097. #endif
  2098. if ( (ssl->error = SendClientHello(ssl)) != 0) {
  2099. CYASSL_ERROR(ssl->error);
  2100. return SSL_FATAL_ERROR;
  2101. }
  2102. }
  2103. #endif
  2104. ssl->options.connectState = HELLO_AGAIN_REPLY;
  2105. CYASSL_MSG("connect state: HELLO_AGAIN_REPLY");
  2106. case HELLO_AGAIN_REPLY :
  2107. #ifdef CYASSL_DTLS
  2108. if (ssl->options.dtls) {
  2109. neededState = ssl->options.resuming ?
  2110. SERVER_FINISHED_COMPLETE : SERVER_HELLODONE_COMPLETE;
  2111. /* get response */
  2112. while (ssl->options.serverState < neededState) {
  2113. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  2114. CYASSL_ERROR(ssl->error);
  2115. return SSL_FATAL_ERROR;
  2116. }
  2117. /* if resumption failed, reset needed state */
  2118. else if (neededState == SERVER_FINISHED_COMPLETE)
  2119. if (!ssl->options.resuming)
  2120. neededState = SERVER_HELLODONE_COMPLETE;
  2121. }
  2122. }
  2123. #endif
  2124. ssl->options.connectState = FIRST_REPLY_DONE;
  2125. CYASSL_MSG("connect state: FIRST_REPLY_DONE");
  2126. case FIRST_REPLY_DONE :
  2127. #ifndef NO_CERTS
  2128. if (ssl->options.sendVerify)
  2129. if ( (ssl->error = SendCertificate(ssl)) != 0) {
  2130. CYASSL_ERROR(ssl->error);
  2131. return SSL_FATAL_ERROR;
  2132. }
  2133. #endif
  2134. ssl->options.connectState = FIRST_REPLY_FIRST;
  2135. CYASSL_MSG("connect state: FIRST_REPLY_FIRST");
  2136. case FIRST_REPLY_FIRST :
  2137. if (!ssl->options.resuming)
  2138. if ( (ssl->error = SendClientKeyExchange(ssl)) != 0) {
  2139. CYASSL_ERROR(ssl->error);
  2140. return SSL_FATAL_ERROR;
  2141. }
  2142. ssl->options.connectState = FIRST_REPLY_SECOND;
  2143. CYASSL_MSG("connect state: FIRST_REPLY_SECOND");
  2144. case FIRST_REPLY_SECOND :
  2145. #ifndef NO_RSA
  2146. if (ssl->options.sendVerify)
  2147. if ( (ssl->error = SendCertificateVerify(ssl)) != 0) {
  2148. CYASSL_ERROR(ssl->error);
  2149. return SSL_FATAL_ERROR;
  2150. }
  2151. #endif
  2152. ssl->options.connectState = FIRST_REPLY_THIRD;
  2153. CYASSL_MSG("connect state: FIRST_REPLY_THIRD");
  2154. case FIRST_REPLY_THIRD :
  2155. if ( (ssl->error = SendChangeCipher(ssl)) != 0) {
  2156. CYASSL_ERROR(ssl->error);
  2157. return SSL_FATAL_ERROR;
  2158. }
  2159. ssl->options.connectState = FIRST_REPLY_FOURTH;
  2160. CYASSL_MSG("connect state: FIRST_REPLY_FOURTH");
  2161. case FIRST_REPLY_FOURTH :
  2162. if ( (ssl->error = SendFinished(ssl)) != 0) {
  2163. CYASSL_ERROR(ssl->error);
  2164. return SSL_FATAL_ERROR;
  2165. }
  2166. ssl->options.connectState = FINISHED_DONE;
  2167. CYASSL_MSG("connect state: FINISHED_DONE");
  2168. case FINISHED_DONE :
  2169. /* get response */
  2170. while (ssl->options.serverState < SERVER_FINISHED_COMPLETE)
  2171. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  2172. CYASSL_ERROR(ssl->error);
  2173. return SSL_FATAL_ERROR;
  2174. }
  2175. ssl->options.connectState = SECOND_REPLY_DONE;
  2176. CYASSL_MSG("connect state: SECOND_REPLY_DONE");
  2177. case SECOND_REPLY_DONE:
  2178. FreeHandshakeResources(ssl);
  2179. CYASSL_LEAVE("SSL_connect()", SSL_SUCCESS);
  2180. return SSL_SUCCESS;
  2181. default:
  2182. CYASSL_MSG("Unknown connect state ERROR");
  2183. return SSL_FATAL_ERROR; /* unknown connect state */
  2184. }
  2185. }
  2186. #endif /* NO_CYASSL_CLIENT */
  2187. /* server only parts */
  2188. #ifndef NO_CYASSL_SERVER
  2189. #ifndef NO_OLD_TLS
  2190. CYASSL_METHOD* CyaSSLv3_server_method(void)
  2191. {
  2192. CYASSL_METHOD* method =
  2193. (CYASSL_METHOD*) XMALLOC(sizeof(CYASSL_METHOD), 0,
  2194. DYNAMIC_TYPE_METHOD);
  2195. CYASSL_ENTER("SSLv3_server_method");
  2196. if (method) {
  2197. InitSSL_Method(method, MakeSSLv3());
  2198. method->side = SERVER_END;
  2199. }
  2200. return method;
  2201. }
  2202. #endif
  2203. #ifdef CYASSL_DTLS
  2204. CYASSL_METHOD* CyaDTLSv1_server_method(void)
  2205. {
  2206. CYASSL_METHOD* method =
  2207. (CYASSL_METHOD*) XMALLOC(sizeof(CYASSL_METHOD), 0,
  2208. DYNAMIC_TYPE_METHOD);
  2209. CYASSL_ENTER("DTLSv1_server_method");
  2210. if (method) {
  2211. InitSSL_Method(method, MakeDTLSv1());
  2212. method->side = SERVER_END;
  2213. }
  2214. return method;
  2215. }
  2216. #endif
  2217. int CyaSSL_accept(CYASSL* ssl)
  2218. {
  2219. byte havePSK = 0;
  2220. CYASSL_ENTER("SSL_accept()");
  2221. #ifdef HAVE_ERRNO_H
  2222. errno = 0;
  2223. #endif
  2224. #ifndef NO_PSK
  2225. havePSK = ssl->options.havePSK;
  2226. #endif
  2227. if (ssl->options.side != SERVER_END) {
  2228. CYASSL_ERROR(ssl->error = SIDE_ERROR);
  2229. return SSL_FATAL_ERROR;
  2230. }
  2231. #ifndef NO_CERTS
  2232. /* in case used set_accept_state after init */
  2233. if (!havePSK && (ssl->buffers.certificate.buffer == NULL ||
  2234. ssl->buffers.key.buffer == NULL)) {
  2235. CYASSL_MSG("accept error: don't have server cert and key");
  2236. ssl->error = NO_PRIVATE_KEY;
  2237. CYASSL_ERROR(ssl->error);
  2238. return SSL_FATAL_ERROR;
  2239. }
  2240. #endif
  2241. #ifdef HAVE_ECC
  2242. /* in case used set_accept_state after init */
  2243. if (ssl->eccTempKeyPresent == 0) {
  2244. if (ecc_make_key(ssl->rng, ssl->eccTempKeySz,
  2245. ssl->eccTempKey) != 0) {
  2246. ssl->error = ECC_MAKEKEY_ERROR;
  2247. CYASSL_ERROR(ssl->error);
  2248. return SSL_FATAL_ERROR;
  2249. }
  2250. ssl->eccTempKeyPresent = 1;
  2251. }
  2252. #endif
  2253. #ifdef CYASSL_DTLS
  2254. if (ssl->version.major == DTLS_MAJOR &&
  2255. ssl->version.minor == DTLS_MINOR) {
  2256. ssl->options.dtls = 1;
  2257. ssl->options.tls = 1;
  2258. ssl->options.tls1_1 = 1;
  2259. if (DtlsPoolInit(ssl) != 0) {
  2260. ssl->error = MEMORY_ERROR;
  2261. CYASSL_ERROR(ssl->error);
  2262. return SSL_FATAL_ERROR;
  2263. }
  2264. }
  2265. #endif
  2266. if (ssl->buffers.outputBuffer.length > 0) {
  2267. if ( (ssl->error = SendBuffered(ssl)) == 0) {
  2268. ssl->options.acceptState++;
  2269. CYASSL_MSG("accept state: Advanced from buffered send");
  2270. }
  2271. else {
  2272. CYASSL_ERROR(ssl->error);
  2273. return SSL_FATAL_ERROR;
  2274. }
  2275. }
  2276. switch (ssl->options.acceptState) {
  2277. case ACCEPT_BEGIN :
  2278. /* get response */
  2279. while (ssl->options.clientState < CLIENT_HELLO_COMPLETE)
  2280. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  2281. CYASSL_ERROR(ssl->error);
  2282. return SSL_FATAL_ERROR;
  2283. }
  2284. ssl->options.acceptState = ACCEPT_CLIENT_HELLO_DONE;
  2285. CYASSL_MSG("accept state ACCEPT_CLIENT_HELLO_DONE");
  2286. case ACCEPT_CLIENT_HELLO_DONE :
  2287. #ifdef CYASSL_DTLS
  2288. if (ssl->options.dtls)
  2289. if ( (ssl->error = SendHelloVerifyRequest(ssl)) != 0) {
  2290. CYASSL_ERROR(ssl->error);
  2291. return SSL_FATAL_ERROR;
  2292. }
  2293. #endif
  2294. ssl->options.acceptState = HELLO_VERIFY_SENT;
  2295. CYASSL_MSG("accept state HELLO_VERIFY_SENT");
  2296. case HELLO_VERIFY_SENT:
  2297. #ifdef CYASSL_DTLS
  2298. if (ssl->options.dtls) {
  2299. ssl->options.clientState = NULL_STATE; /* get again */
  2300. /* re-init hashes, exclude first hello and verify request */
  2301. InitMd5(&ssl->hashMd5);
  2302. InitSha(&ssl->hashSha);
  2303. #ifndef NO_SHA256
  2304. if (IsAtLeastTLSv1_2(ssl))
  2305. InitSha256(&ssl->hashSha256);
  2306. #endif
  2307. while (ssl->options.clientState < CLIENT_HELLO_COMPLETE)
  2308. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  2309. CYASSL_ERROR(ssl->error);
  2310. return SSL_FATAL_ERROR;
  2311. }
  2312. }
  2313. #endif
  2314. ssl->options.acceptState = ACCEPT_FIRST_REPLY_DONE;
  2315. CYASSL_MSG("accept state ACCEPT_FIRST_REPLY_DONE");
  2316. case ACCEPT_FIRST_REPLY_DONE :
  2317. if ( (ssl->error = SendServerHello(ssl)) != 0) {
  2318. CYASSL_ERROR(ssl->error);
  2319. return SSL_FATAL_ERROR;
  2320. }
  2321. ssl->options.acceptState = SERVER_HELLO_SENT;
  2322. CYASSL_MSG("accept state SERVER_HELLO_SENT");
  2323. case SERVER_HELLO_SENT :
  2324. #ifndef NO_CERTS
  2325. if (!ssl->options.resuming)
  2326. if ( (ssl->error = SendCertificate(ssl)) != 0) {
  2327. CYASSL_ERROR(ssl->error);
  2328. return SSL_FATAL_ERROR;
  2329. }
  2330. #endif
  2331. ssl->options.acceptState = CERT_SENT;
  2332. CYASSL_MSG("accept state CERT_SENT");
  2333. case CERT_SENT :
  2334. if (!ssl->options.resuming)
  2335. if ( (ssl->error = SendServerKeyExchange(ssl)) != 0) {
  2336. CYASSL_ERROR(ssl->error);
  2337. return SSL_FATAL_ERROR;
  2338. }
  2339. ssl->options.acceptState = KEY_EXCHANGE_SENT;
  2340. CYASSL_MSG("accept state KEY_EXCHANGE_SENT");
  2341. case KEY_EXCHANGE_SENT :
  2342. #ifndef NO_CERTS
  2343. if (!ssl->options.resuming)
  2344. if (ssl->options.verifyPeer)
  2345. if ( (ssl->error = SendCertificateRequest(ssl)) != 0) {
  2346. CYASSL_ERROR(ssl->error);
  2347. return SSL_FATAL_ERROR;
  2348. }
  2349. #endif
  2350. ssl->options.acceptState = CERT_REQ_SENT;
  2351. CYASSL_MSG("accept state CERT_REQ_SENT");
  2352. case CERT_REQ_SENT :
  2353. if (!ssl->options.resuming)
  2354. if ( (ssl->error = SendServerHelloDone(ssl)) != 0) {
  2355. CYASSL_ERROR(ssl->error);
  2356. return SSL_FATAL_ERROR;
  2357. }
  2358. ssl->options.acceptState = SERVER_HELLO_DONE;
  2359. CYASSL_MSG("accept state SERVER_HELLO_DONE");
  2360. case SERVER_HELLO_DONE :
  2361. if (!ssl->options.resuming) {
  2362. while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE)
  2363. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  2364. CYASSL_ERROR(ssl->error);
  2365. return SSL_FATAL_ERROR;
  2366. }
  2367. }
  2368. ssl->options.acceptState = ACCEPT_SECOND_REPLY_DONE;
  2369. CYASSL_MSG("accept state ACCEPT_SECOND_REPLY_DONE");
  2370. case ACCEPT_SECOND_REPLY_DONE :
  2371. if ( (ssl->error = SendChangeCipher(ssl)) != 0) {
  2372. CYASSL_ERROR(ssl->error);
  2373. return SSL_FATAL_ERROR;
  2374. }
  2375. ssl->options.acceptState = CHANGE_CIPHER_SENT;
  2376. CYASSL_MSG("accept state CHANGE_CIPHER_SENT");
  2377. case CHANGE_CIPHER_SENT :
  2378. if ( (ssl->error = SendFinished(ssl)) != 0) {
  2379. CYASSL_ERROR(ssl->error);
  2380. return SSL_FATAL_ERROR;
  2381. }
  2382. ssl->options.acceptState = ACCEPT_FINISHED_DONE;
  2383. CYASSL_MSG("accept state ACCEPT_FINISHED_DONE");
  2384. case ACCEPT_FINISHED_DONE :
  2385. if (ssl->options.resuming)
  2386. while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE)
  2387. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  2388. CYASSL_ERROR(ssl->error);
  2389. return SSL_FATAL_ERROR;
  2390. }
  2391. ssl->options.acceptState = ACCEPT_THIRD_REPLY_DONE;
  2392. CYASSL_MSG("accept state ACCEPT_THIRD_REPLY_DONE");
  2393. case ACCEPT_THIRD_REPLY_DONE :
  2394. FreeHandshakeResources(ssl);
  2395. CYASSL_LEAVE("SSL_accept()", SSL_SUCCESS);
  2396. return SSL_SUCCESS;
  2397. default :
  2398. CYASSL_MSG("Unknown accept state ERROR");
  2399. return SSL_FATAL_ERROR;
  2400. }
  2401. }
  2402. #endif /* NO_CYASSL_SERVER */
  2403. int CyaSSL_Cleanup(void)
  2404. {
  2405. int ret = 0;
  2406. int release = 0;
  2407. CYASSL_ENTER("CyaSSL_Cleanup");
  2408. LockMutex(&count_mutex);
  2409. release = initRefCount-- == 1;
  2410. if (initRefCount < 0)
  2411. initRefCount = 0;
  2412. UnLockMutex(&count_mutex);
  2413. if (!release)
  2414. return ret;
  2415. #ifndef NO_SESSION_CACHE
  2416. if (FreeMutex(&session_mutex) != 0)
  2417. ret = BAD_MUTEX_ERROR;
  2418. #endif
  2419. if (FreeMutex(&count_mutex) != 0)
  2420. ret = BAD_MUTEX_ERROR;
  2421. return ret;
  2422. }
  2423. #ifndef NO_SESSION_CACHE
  2424. static INLINE word32 HashSession(const byte* sessionID)
  2425. {
  2426. /* id is random, just make 32 bit number from first 4 bytes for now */
  2427. return (sessionID[0] << 24) | (sessionID[1] << 16) | (sessionID[2] << 8) |
  2428. sessionID[3];
  2429. }
  2430. void CyaSSL_flush_sessions(CYASSL_CTX* ctx, long tm)
  2431. {
  2432. /* static table now, no flusing needed */
  2433. (void)ctx;
  2434. (void)tm;
  2435. }
  2436. /* set ssl session timeout in seconds */
  2437. int CyaSSL_set_timeout(CYASSL* ssl, unsigned int to)
  2438. {
  2439. if (ssl == NULL)
  2440. return BAD_FUNC_ARG;
  2441. ssl->timeout = to;
  2442. return SSL_SUCCESS;
  2443. }
  2444. /* set ctx session timeout in seconds */
  2445. int CyaSSL_CTX_set_timeout(CYASSL_CTX* ctx, unsigned int to)
  2446. {
  2447. if (ctx == NULL)
  2448. return BAD_FUNC_ARG;
  2449. ctx->timeout = to;
  2450. return SSL_SUCCESS;
  2451. }
  2452. CYASSL_SESSION* GetSession(CYASSL* ssl, byte* masterSecret)
  2453. {
  2454. CYASSL_SESSION* ret = 0;
  2455. const byte* id = NULL;
  2456. word32 row;
  2457. int idx;
  2458. if (ssl->options.sessionCacheOff)
  2459. return NULL;
  2460. if (ssl->options.haveSessionId == 0)
  2461. return NULL;
  2462. if (ssl->arrays)
  2463. id = ssl->arrays->sessionID;
  2464. else
  2465. id = ssl->session.sessionID;
  2466. row = HashSession(id) % SESSION_ROWS;
  2467. if (LockMutex(&session_mutex) != 0)
  2468. return 0;
  2469. if (SessionCache[row].totalCount >= SESSIONS_PER_ROW)
  2470. idx = SESSIONS_PER_ROW - 1;
  2471. else
  2472. idx = SessionCache[row].nextIdx - 1;
  2473. for (; idx >= 0; idx--) {
  2474. CYASSL_SESSION* current;
  2475. if (idx >= SESSIONS_PER_ROW) /* server could have restarted, idx */
  2476. break; /* would be word32(-1) and seg fault */
  2477. current = &SessionCache[row].Sessions[idx];
  2478. if (XMEMCMP(current->sessionID, id, ID_LEN) == 0) {
  2479. if (LowResTimer() < (current->bornOn + current->timeout)) {
  2480. ret = current;
  2481. if (masterSecret)
  2482. XMEMCPY(masterSecret, current->masterSecret, SECRET_LEN);
  2483. }
  2484. break;
  2485. }
  2486. }
  2487. UnLockMutex(&session_mutex);
  2488. return ret;
  2489. }
  2490. int SetSession(CYASSL* ssl, CYASSL_SESSION* session)
  2491. {
  2492. if (ssl->options.sessionCacheOff)
  2493. return SSL_FAILURE;
  2494. if (LowResTimer() < (session->bornOn + session->timeout)) {
  2495. ssl->session = *session;
  2496. ssl->options.resuming = 1;
  2497. #ifdef SESSION_CERTS
  2498. ssl->version = session->version;
  2499. ssl->options.cipherSuite0 = session->cipherSuite0;
  2500. ssl->options.cipherSuite = session->cipherSuite;
  2501. #endif
  2502. return SSL_SUCCESS;
  2503. }
  2504. return SSL_FAILURE; /* session timed out */
  2505. }
  2506. int AddSession(CYASSL* ssl)
  2507. {
  2508. word32 row, idx;
  2509. if (ssl->options.sessionCacheOff)
  2510. return 0;
  2511. if (ssl->options.haveSessionId == 0)
  2512. return 0;
  2513. row = HashSession(ssl->arrays->sessionID) % SESSION_ROWS;
  2514. if (LockMutex(&session_mutex) != 0)
  2515. return BAD_MUTEX_ERROR;
  2516. idx = SessionCache[row].nextIdx++;
  2517. XMEMCPY(SessionCache[row].Sessions[idx].masterSecret,
  2518. ssl->arrays->masterSecret, SECRET_LEN);
  2519. XMEMCPY(SessionCache[row].Sessions[idx].sessionID, ssl->arrays->sessionID,
  2520. ID_LEN);
  2521. SessionCache[row].Sessions[idx].timeout = ssl->timeout;
  2522. SessionCache[row].Sessions[idx].bornOn = LowResTimer();
  2523. #ifdef SESSION_CERTS
  2524. SessionCache[row].Sessions[idx].chain.count = ssl->session.chain.count;
  2525. XMEMCPY(SessionCache[row].Sessions[idx].chain.certs,
  2526. ssl->session.chain.certs, sizeof(x509_buffer) * MAX_CHAIN_DEPTH);
  2527. SessionCache[row].Sessions[idx].version = ssl->version;
  2528. SessionCache[row].Sessions[idx].cipherSuite0 = ssl->options.cipherSuite0;
  2529. SessionCache[row].Sessions[idx].cipherSuite = ssl->options.cipherSuite;
  2530. #endif
  2531. SessionCache[row].totalCount++;
  2532. if (SessionCache[row].nextIdx == SESSIONS_PER_ROW)
  2533. SessionCache[row].nextIdx = 0;
  2534. if (UnLockMutex(&session_mutex) != 0)
  2535. return BAD_MUTEX_ERROR;
  2536. return 0;
  2537. }
  2538. #ifdef SESSION_STATS
  2539. CYASSL_API
  2540. void PrintSessionStats(void)
  2541. {
  2542. word32 totalSessionsSeen = 0;
  2543. word32 totalSessionsNow = 0;
  2544. word32 rowNow;
  2545. int i;
  2546. double E; /* expected freq */
  2547. double chiSquare = 0;
  2548. for (i = 0; i < SESSION_ROWS; i++) {
  2549. totalSessionsSeen += SessionCache[i].totalCount;
  2550. if (SessionCache[i].totalCount >= SESSIONS_PER_ROW)
  2551. rowNow = SESSIONS_PER_ROW;
  2552. else if (SessionCache[i].nextIdx == 0)
  2553. rowNow = 0;
  2554. else
  2555. rowNow = SessionCache[i].nextIdx;
  2556. totalSessionsNow += rowNow;
  2557. }
  2558. printf("Total Sessions Seen = %d\n", totalSessionsSeen);
  2559. printf("Total Sessions Now = %d\n", totalSessionsNow);
  2560. E = (double)totalSessionsSeen / SESSION_ROWS;
  2561. for (i = 0; i < SESSION_ROWS; i++) {
  2562. double diff = SessionCache[i].totalCount - E;
  2563. diff *= diff; /* square */
  2564. diff /= E; /* normalize */
  2565. chiSquare += diff;
  2566. }
  2567. printf(" chi-square = %5.1f, d.f. = %d\n", chiSquare,
  2568. SESSION_ROWS - 1);
  2569. if (SESSION_ROWS == 11)
  2570. printf(" .05 p value = 18.3, chi-square should be less\n");
  2571. else if (SESSION_ROWS == 211)
  2572. printf(".05 p value = 244.8, chi-square should be less\n");
  2573. else if (SESSION_ROWS == 5981)
  2574. printf(".05 p value = 6161.0, chi-square should be less\n");
  2575. else if (SESSION_ROWS == 3)
  2576. printf(".05 p value = 6.0, chi-square should be less\n");
  2577. else if (SESSION_ROWS == 2861)
  2578. printf(".05 p value = 2985.5, chi-square should be less\n");
  2579. printf("\n");
  2580. }
  2581. #endif /* SESSION_STATS */
  2582. #else /* NO_SESSION_CACHE */
  2583. /* No session cache version */
  2584. CYASSL_SESSION* GetSession(CYASSL* ssl, byte* masterSecret)
  2585. {
  2586. (void)ssl;
  2587. (void)masterSecret;
  2588. return NULL;
  2589. }
  2590. #endif /* NO_SESSION_CACHE */
  2591. /* call before SSL_connect, if verifying will add name check to
  2592. date check and signature check */
  2593. int CyaSSL_check_domain_name(CYASSL* ssl, const char* dn)
  2594. {
  2595. CYASSL_ENTER("CyaSSL_check_domain_name");
  2596. if (ssl->buffers.domainName.buffer)
  2597. XFREE(ssl->buffers.domainName.buffer, ssl->heap, DYNAMIC_TYPE_DOMAIN);
  2598. ssl->buffers.domainName.length = (word32)XSTRLEN(dn) + 1;
  2599. ssl->buffers.domainName.buffer = (byte*) XMALLOC(
  2600. ssl->buffers.domainName.length, ssl->heap, DYNAMIC_TYPE_DOMAIN);
  2601. if (ssl->buffers.domainName.buffer) {
  2602. XSTRNCPY((char*)ssl->buffers.domainName.buffer, dn,
  2603. ssl->buffers.domainName.length);
  2604. return SSL_SUCCESS;
  2605. }
  2606. else {
  2607. ssl->error = MEMORY_ERROR;
  2608. return SSL_FAILURE;
  2609. }
  2610. }
  2611. /* turn on CyaSSL zlib compression
  2612. returns 0 for success, else error (not built in)
  2613. */
  2614. int CyaSSL_set_compression(CYASSL* ssl)
  2615. {
  2616. CYASSL_ENTER("CyaSSL_set_compression");
  2617. (void)ssl;
  2618. #ifdef HAVE_LIBZ
  2619. ssl->options.usingCompression = 1;
  2620. return 0;
  2621. #else
  2622. return NOT_COMPILED_IN;
  2623. #endif
  2624. }
  2625. #ifndef USE_WINDOWS_API
  2626. #ifndef NO_WRITEV
  2627. /* simulate writev semantics, doesn't actually do block at a time though
  2628. because of SSL_write behavior and because front adds may be small */
  2629. int CyaSSL_writev(CYASSL* ssl, const struct iovec* iov, int iovcnt)
  2630. {
  2631. byte tmp[FILE_BUFFER_SIZE];
  2632. byte* myBuffer = tmp;
  2633. int sending = 0;
  2634. int newBuffer = 0;
  2635. int idx = 0;
  2636. int i;
  2637. int ret;
  2638. CYASSL_ENTER("CyaSSL_writev");
  2639. for (i = 0; i < iovcnt; i++)
  2640. sending += (int)iov[i].iov_len;
  2641. if (sending > (int)sizeof(tmp)) {
  2642. byte* tmp2 = (byte*) XMALLOC(sending, ssl->heap,
  2643. DYNAMIC_TYPE_WRITEV);
  2644. if (!tmp2)
  2645. return MEMORY_ERROR;
  2646. myBuffer = tmp2;
  2647. newBuffer = 1;
  2648. }
  2649. for (i = 0; i < iovcnt; i++) {
  2650. XMEMCPY(&myBuffer[idx], iov[i].iov_base, iov[i].iov_len);
  2651. idx += (int)iov[i].iov_len;
  2652. }
  2653. ret = CyaSSL_write(ssl, myBuffer, sending);
  2654. if (newBuffer) XFREE(myBuffer, ssl->heap, DYNAMIC_TYPE_WRITEV);
  2655. return ret;
  2656. }
  2657. #endif
  2658. #endif
  2659. #ifdef CYASSL_CALLBACKS
  2660. typedef struct itimerval Itimerval;
  2661. /* don't keep calling simple functions while setting up timer and singals
  2662. if no inlining these are the next best */
  2663. #define AddTimes(a, b, c) \
  2664. do { \
  2665. c.tv_sec = a.tv_sec + b.tv_sec; \
  2666. c.tv_usec = a.tv_usec + b.tv_usec; \
  2667. if (c.tv_sec >= 1000000) { \
  2668. c.tv_sec++; \
  2669. c.tv_usec -= 1000000; \
  2670. } \
  2671. } while (0)
  2672. #define SubtractTimes(a, b, c) \
  2673. do { \
  2674. c.tv_sec = a.tv_sec - b.tv_sec; \
  2675. c.tv_usec = a.tv_usec - b.tv_usec; \
  2676. if (c.tv_sec < 0) { \
  2677. c.tv_sec--; \
  2678. c.tv_usec += 1000000; \
  2679. } \
  2680. } while (0)
  2681. #define CmpTimes(a, b, cmp) \
  2682. ((a.tv_sec == b.tv_sec) ? \
  2683. (a.tv_usec cmp b.tv_usec) : \
  2684. (a.tv_sec cmp b.tv_sec)) \
  2685. /* do nothing handler */
  2686. static void myHandler(int signo)
  2687. {
  2688. return;
  2689. }
  2690. static int CyaSSL_ex_wrapper(CYASSL* ssl, HandShakeCallBack hsCb,
  2691. TimeoutCallBack toCb, Timeval timeout)
  2692. {
  2693. int ret = SSL_FATAL_ERROR;
  2694. int oldTimerOn = 0; /* was timer already on */
  2695. Timeval startTime;
  2696. Timeval endTime;
  2697. Timeval totalTime;
  2698. Itimerval myTimeout;
  2699. Itimerval oldTimeout; /* if old timer adjust from total time to reset */
  2700. struct sigaction act, oact;
  2701. #define ERR_OUT(x) { ssl->hsInfoOn = 0; ssl->toInfoOn = 0; return x; }
  2702. if (hsCb) {
  2703. ssl->hsInfoOn = 1;
  2704. InitHandShakeInfo(&ssl->handShakeInfo);
  2705. }
  2706. if (toCb) {
  2707. ssl->toInfoOn = 1;
  2708. InitTimeoutInfo(&ssl->timeoutInfo);
  2709. if (gettimeofday(&startTime, 0) < 0)
  2710. ERR_OUT(GETTIME_ERROR);
  2711. /* use setitimer to simulate getitimer, init 0 myTimeout */
  2712. myTimeout.it_interval.tv_sec = 0;
  2713. myTimeout.it_interval.tv_usec = 0;
  2714. myTimeout.it_value.tv_sec = 0;
  2715. myTimeout.it_value.tv_usec = 0;
  2716. if (setitimer(ITIMER_REAL, &myTimeout, &oldTimeout) < 0)
  2717. ERR_OUT(SETITIMER_ERROR);
  2718. if (oldTimeout.it_value.tv_sec || oldTimeout.it_value.tv_usec) {
  2719. oldTimerOn = 1;
  2720. /* is old timer going to expire before ours */
  2721. if (CmpTimes(oldTimeout.it_value, timeout, <)) {
  2722. timeout.tv_sec = oldTimeout.it_value.tv_sec;
  2723. timeout.tv_usec = oldTimeout.it_value.tv_usec;
  2724. }
  2725. }
  2726. myTimeout.it_value.tv_sec = timeout.tv_sec;
  2727. myTimeout.it_value.tv_usec = timeout.tv_usec;
  2728. /* set up signal handler, don't restart socket send/recv */
  2729. act.sa_handler = myHandler;
  2730. sigemptyset(&act.sa_mask);
  2731. act.sa_flags = 0;
  2732. #ifdef SA_INTERRUPT
  2733. act.sa_flags |= SA_INTERRUPT;
  2734. #endif
  2735. if (sigaction(SIGALRM, &act, &oact) < 0)
  2736. ERR_OUT(SIGACT_ERROR);
  2737. if (setitimer(ITIMER_REAL, &myTimeout, 0) < 0)
  2738. ERR_OUT(SETITIMER_ERROR);
  2739. }
  2740. /* do main work */
  2741. #ifndef NO_CYASSL_CLIENT
  2742. if (ssl->options.side == CLIENT_END)
  2743. ret = CyaSSL_connect(ssl);
  2744. #endif
  2745. #ifndef NO_CYASSL_SERVER
  2746. if (ssl->options.side == SERVER_END)
  2747. ret = CyaSSL_accept(ssl);
  2748. #endif
  2749. /* do callbacks */
  2750. if (toCb) {
  2751. if (oldTimerOn) {
  2752. gettimeofday(&endTime, 0);
  2753. SubtractTimes(endTime, startTime, totalTime);
  2754. /* adjust old timer for elapsed time */
  2755. if (CmpTimes(totalTime, oldTimeout.it_value, <))
  2756. SubtractTimes(oldTimeout.it_value, totalTime,
  2757. oldTimeout.it_value);
  2758. else {
  2759. /* reset value to interval, may be off */
  2760. oldTimeout.it_value.tv_sec = oldTimeout.it_interval.tv_sec;
  2761. oldTimeout.it_value.tv_usec =oldTimeout.it_interval.tv_usec;
  2762. }
  2763. /* keep iter the same whether there or not */
  2764. }
  2765. /* restore old handler */
  2766. if (sigaction(SIGALRM, &oact, 0) < 0)
  2767. ret = SIGACT_ERROR; /* more pressing error, stomp */
  2768. else
  2769. /* use old settings which may turn off (expired or not there) */
  2770. if (setitimer(ITIMER_REAL, &oldTimeout, 0) < 0)
  2771. ret = SETITIMER_ERROR;
  2772. /* if we had a timeout call callback */
  2773. if (ssl->timeoutInfo.timeoutName[0]) {
  2774. ssl->timeoutInfo.timeoutValue.tv_sec = timeout.tv_sec;
  2775. ssl->timeoutInfo.timeoutValue.tv_usec = timeout.tv_usec;
  2776. (toCb)(&ssl->timeoutInfo);
  2777. }
  2778. /* clean up */
  2779. FreeTimeoutInfo(&ssl->timeoutInfo, ssl->heap);
  2780. ssl->toInfoOn = 0;
  2781. }
  2782. if (hsCb) {
  2783. FinishHandShakeInfo(&ssl->handShakeInfo, ssl);
  2784. (hsCb)(&ssl->handShakeInfo);
  2785. ssl->hsInfoOn = 0;
  2786. }
  2787. return ret;
  2788. }
  2789. #ifndef NO_CYASSL_CLIENT
  2790. int CyaSSL_connect_ex(CYASSL* ssl, HandShakeCallBack hsCb,
  2791. TimeoutCallBack toCb, Timeval timeout)
  2792. {
  2793. CYASSL_ENTER("CyaSSL_connect_ex");
  2794. return CyaSSL_ex_wrapper(ssl, hsCb, toCb, timeout);
  2795. }
  2796. #endif
  2797. #ifndef NO_CYASSL_SERVER
  2798. int CyaSSL_accept_ex(CYASSL* ssl, HandShakeCallBack hsCb,
  2799. TimeoutCallBack toCb,Timeval timeout)
  2800. {
  2801. CYASSL_ENTER("CyaSSL_accept_ex");
  2802. return CyaSSL_ex_wrapper(ssl, hsCb, toCb, timeout);
  2803. }
  2804. #endif
  2805. #endif /* CYASSL_CALLBACKS */
  2806. #ifndef NO_PSK
  2807. void CyaSSL_CTX_set_psk_client_callback(CYASSL_CTX* ctx,
  2808. psk_client_callback cb)
  2809. {
  2810. CYASSL_ENTER("SSL_CTX_set_psk_client_callback");
  2811. ctx->havePSK = 1;
  2812. ctx->client_psk_cb = cb;
  2813. }
  2814. void CyaSSL_set_psk_client_callback(CYASSL* ssl, psk_client_callback cb)
  2815. {
  2816. byte haveRSA = 1;
  2817. CYASSL_ENTER("SSL_set_psk_client_callback");
  2818. ssl->options.havePSK = 1;
  2819. ssl->options.client_psk_cb = cb;
  2820. #ifdef NO_RSA
  2821. haveRSA = 0;
  2822. #endif
  2823. InitSuites(ssl->suites, ssl->version, haveRSA, TRUE,
  2824. ssl->options.haveDH, ssl->options.haveNTRU,
  2825. ssl->options.haveECDSAsig, ssl->options.haveStaticECC,
  2826. ssl->options.side);
  2827. }
  2828. void CyaSSL_CTX_set_psk_server_callback(CYASSL_CTX* ctx,
  2829. psk_server_callback cb)
  2830. {
  2831. CYASSL_ENTER("SSL_CTX_set_psk_server_callback");
  2832. ctx->havePSK = 1;
  2833. ctx->server_psk_cb = cb;
  2834. }
  2835. void CyaSSL_set_psk_server_callback(CYASSL* ssl, psk_server_callback cb)
  2836. {
  2837. byte haveRSA = 1;
  2838. CYASSL_ENTER("SSL_set_psk_server_callback");
  2839. ssl->options.havePSK = 1;
  2840. ssl->options.server_psk_cb = cb;
  2841. #ifdef NO_RSA
  2842. haveRSA = 0;
  2843. #endif
  2844. InitSuites(ssl->suites, ssl->version, haveRSA, TRUE,
  2845. ssl->options.haveDH, ssl->options.haveNTRU,
  2846. ssl->options.haveECDSAsig, ssl->options.haveStaticECC,
  2847. ssl->options.side);
  2848. }
  2849. const char* CyaSSL_get_psk_identity_hint(const CYASSL* ssl)
  2850. {
  2851. CYASSL_ENTER("SSL_get_psk_identity_hint");
  2852. if (ssl == NULL || ssl->arrays == NULL)
  2853. return NULL;
  2854. return ssl->arrays->server_hint;
  2855. }
  2856. const char* CyaSSL_get_psk_identity(const CYASSL* ssl)
  2857. {
  2858. CYASSL_ENTER("SSL_get_psk_identity");
  2859. if (ssl == NULL || ssl->arrays == NULL)
  2860. return NULL;
  2861. return ssl->arrays->client_identity;
  2862. }
  2863. int CyaSSL_CTX_use_psk_identity_hint(CYASSL_CTX* ctx, const char* hint)
  2864. {
  2865. CYASSL_ENTER("SSL_CTX_use_psk_identity_hint");
  2866. if (hint == 0)
  2867. ctx->server_hint[0] = 0;
  2868. else {
  2869. XSTRNCPY(ctx->server_hint, hint, MAX_PSK_ID_LEN);
  2870. ctx->server_hint[MAX_PSK_ID_LEN - 1] = '\0';
  2871. }
  2872. return SSL_SUCCESS;
  2873. }
  2874. int CyaSSL_use_psk_identity_hint(CYASSL* ssl, const char* hint)
  2875. {
  2876. CYASSL_ENTER("SSL_use_psk_identity_hint");
  2877. if (ssl == NULL || ssl->arrays == NULL)
  2878. return SSL_FAILURE;
  2879. if (hint == 0)
  2880. ssl->arrays->server_hint[0] = 0;
  2881. else {
  2882. XSTRNCPY(ssl->arrays->server_hint, hint, MAX_PSK_ID_LEN);
  2883. ssl->arrays->server_hint[MAX_PSK_ID_LEN - 1] = '\0';
  2884. }
  2885. return SSL_SUCCESS;
  2886. }
  2887. #endif /* NO_PSK */
  2888. #ifndef NO_CERTS
  2889. /* used to be defined on NO_FILESYSTEM only, but are generally useful */
  2890. /* CyaSSL extension allows DER files to be loaded from buffers as well */
  2891. int CyaSSL_CTX_load_verify_buffer(CYASSL_CTX* ctx, const unsigned char* in,
  2892. long sz, int format)
  2893. {
  2894. CYASSL_ENTER("CyaSSL_CTX_load_verify_buffer");
  2895. if (format == SSL_FILETYPE_PEM)
  2896. return ProcessChainBuffer(ctx, in, sz, format, CA_TYPE, NULL);
  2897. else
  2898. return ProcessBuffer(ctx, in, sz, format, CA_TYPE, NULL,NULL,0);
  2899. }
  2900. int CyaSSL_CTX_use_certificate_buffer(CYASSL_CTX* ctx,
  2901. const unsigned char* in, long sz, int format)
  2902. {
  2903. CYASSL_ENTER("CyaSSL_CTX_use_certificate_buffer");
  2904. return ProcessBuffer(ctx, in, sz, format, CERT_TYPE, NULL, NULL, 0);
  2905. }
  2906. int CyaSSL_CTX_use_PrivateKey_buffer(CYASSL_CTX* ctx,
  2907. const unsigned char* in, long sz, int format)
  2908. {
  2909. CYASSL_ENTER("CyaSSL_CTX_use_PrivateKey_buffer");
  2910. return ProcessBuffer(ctx, in, sz, format, PRIVATEKEY_TYPE, NULL,NULL,0);
  2911. }
  2912. int CyaSSL_CTX_use_certificate_chain_buffer(CYASSL_CTX* ctx,
  2913. const unsigned char* in, long sz)
  2914. {
  2915. CYASSL_ENTER("CyaSSL_CTX_use_certificate_chain_buffer");
  2916. return ProcessBuffer(ctx, in, sz, SSL_FILETYPE_PEM, CERT_TYPE, NULL,
  2917. NULL, 1);
  2918. }
  2919. int CyaSSL_use_certificate_buffer(CYASSL* ssl,
  2920. const unsigned char* in, long sz, int format)
  2921. {
  2922. CYASSL_ENTER("CyaSSL_use_certificate_buffer");
  2923. return ProcessBuffer(ssl->ctx, in, sz, format,CERT_TYPE,ssl,NULL,0);
  2924. }
  2925. int CyaSSL_use_PrivateKey_buffer(CYASSL* ssl,
  2926. const unsigned char* in, long sz, int format)
  2927. {
  2928. CYASSL_ENTER("CyaSSL_use_PrivateKey_buffer");
  2929. return ProcessBuffer(ssl->ctx, in, sz, format, PRIVATEKEY_TYPE,
  2930. ssl, NULL, 0);
  2931. }
  2932. int CyaSSL_use_certificate_chain_buffer(CYASSL* ssl,
  2933. const unsigned char* in, long sz)
  2934. {
  2935. CYASSL_ENTER("CyaSSL_use_certificate_chain_buffer");
  2936. return ProcessBuffer(ssl->ctx, in, sz, SSL_FILETYPE_PEM, CERT_TYPE,
  2937. ssl, NULL, 1);
  2938. }
  2939. /* old NO_FILESYSTEM end */
  2940. #endif /* !NO_CERTS */
  2941. #if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)
  2942. int CyaSSL_add_all_algorithms(void)
  2943. {
  2944. CYASSL_ENTER("CyaSSL_add_all_algorithms");
  2945. CyaSSL_Init();
  2946. return SSL_SUCCESS;
  2947. }
  2948. long CyaSSL_CTX_sess_set_cache_size(CYASSL_CTX* ctx, long sz)
  2949. {
  2950. /* cache size fixed at compile time in CyaSSL */
  2951. (void)ctx;
  2952. (void)sz;
  2953. return 0;
  2954. }
  2955. void CyaSSL_CTX_set_quiet_shutdown(CYASSL_CTX* ctx, int mode)
  2956. {
  2957. CYASSL_ENTER("CyaSSL_CTX_set_quiet_shutdown");
  2958. if (mode)
  2959. ctx->quietShutdown = 1;
  2960. }
  2961. void CyaSSL_set_quiet_shutdown(CYASSL* ssl, int mode)
  2962. {
  2963. CYASSL_ENTER("CyaSSL_CTX_set_quiet_shutdown");
  2964. if (mode)
  2965. ssl->options.quietShutdown = 1;
  2966. }
  2967. void CyaSSL_set_bio(CYASSL* ssl, CYASSL_BIO* rd, CYASSL_BIO* wr)
  2968. {
  2969. CYASSL_ENTER("SSL_set_bio");
  2970. CyaSSL_set_rfd(ssl, rd->fd);
  2971. CyaSSL_set_wfd(ssl, wr->fd);
  2972. ssl->biord = rd;
  2973. ssl->biowr = wr;
  2974. }
  2975. void CyaSSL_CTX_set_client_CA_list(CYASSL_CTX* ctx,
  2976. STACK_OF(CYASSL_X509_NAME)* names)
  2977. {
  2978. (void)ctx;
  2979. (void)names;
  2980. }
  2981. STACK_OF(CYASSL_X509_NAME)* CyaSSL_load_client_CA_file(const char* fname)
  2982. {
  2983. (void)fname;
  2984. return 0;
  2985. }
  2986. int CyaSSL_CTX_set_default_verify_paths(CYASSL_CTX* ctx)
  2987. {
  2988. /* TODO:, not needed in goahead */
  2989. (void)ctx;
  2990. return SSL_NOT_IMPLEMENTED;
  2991. }
  2992. /* keyblock size in bytes or -1 */
  2993. int CyaSSL_get_keyblock_size(CYASSL* ssl)
  2994. {
  2995. if (ssl == NULL)
  2996. return -1;
  2997. return 2 * (ssl->specs.key_size + ssl->specs.iv_size +
  2998. ssl->specs.hash_size);
  2999. }
  3000. /* store keys returns 0 or -1 on error */
  3001. int CyaSSL_get_keys(CYASSL* ssl, unsigned char** ms, unsigned int* msLen,
  3002. unsigned char** sr, unsigned int* srLen,
  3003. unsigned char** cr, unsigned int* crLen)
  3004. {
  3005. if (ssl == NULL || ssl->arrays == NULL)
  3006. return -1;
  3007. *ms = ssl->arrays->masterSecret;
  3008. *sr = ssl->arrays->serverRandom;
  3009. *cr = ssl->arrays->clientRandom;
  3010. *msLen = SECRET_LEN;
  3011. *srLen = RAN_LEN;
  3012. *crLen = RAN_LEN;
  3013. return 0;
  3014. }
  3015. void CyaSSL_set_accept_state(CYASSL* ssl)
  3016. {
  3017. byte haveRSA = 1;
  3018. byte havePSK = 0;
  3019. CYASSL_ENTER("SSL_set_accept_state");
  3020. ssl->options.side = SERVER_END;
  3021. /* reset suites in case user switched */
  3022. #ifdef NO_RSA
  3023. haveRSA = 0;
  3024. #endif
  3025. #ifndef NO_PSK
  3026. havePSK = ssl->options.havePSK;
  3027. #endif
  3028. InitSuites(ssl->suites, ssl->version, haveRSA, havePSK,
  3029. ssl->options.haveDH, ssl->options.haveNTRU,
  3030. ssl->options.haveECDSAsig, ssl->options.haveStaticECC,
  3031. ssl->options.side);
  3032. }
  3033. /* return true if connection established */
  3034. int CyaSSL_is_init_finished(CYASSL* ssl)
  3035. {
  3036. if (ssl == NULL)
  3037. return 0;
  3038. if (ssl->options.handShakeState == HANDSHAKE_DONE)
  3039. return 1;
  3040. return 0;
  3041. }
  3042. void CyaSSL_CTX_set_tmp_rsa_callback(CYASSL_CTX* ctx,
  3043. CYASSL_RSA*(*f)(CYASSL*, int, int))
  3044. {
  3045. /* CyaSSL verifies all these internally */
  3046. (void)ctx;
  3047. (void)f;
  3048. }
  3049. void CyaSSL_set_shutdown(CYASSL* ssl, int opt)
  3050. {
  3051. (void)ssl;
  3052. (void)opt;
  3053. }
  3054. long CyaSSL_CTX_set_options(CYASSL_CTX* ctx, long opt)
  3055. {
  3056. /* goahead calls with 0, do nothing */
  3057. CYASSL_ENTER("SSL_CTX_set_options");
  3058. (void)ctx;
  3059. return opt;
  3060. }
  3061. int CyaSSL_set_rfd(CYASSL* ssl, int rfd)
  3062. {
  3063. CYASSL_ENTER("SSL_set_rfd");
  3064. ssl->rfd = rfd; /* not used directly to allow IO callbacks */
  3065. ssl->IOCB_ReadCtx = &ssl->rfd;
  3066. return SSL_SUCCESS;
  3067. }
  3068. int CyaSSL_set_wfd(CYASSL* ssl, int wfd)
  3069. {
  3070. CYASSL_ENTER("SSL_set_wfd");
  3071. ssl->wfd = wfd; /* not used directly to allow IO callbacks */
  3072. ssl->IOCB_WriteCtx = &ssl->wfd;
  3073. return SSL_SUCCESS;
  3074. }
  3075. CYASSL_RSA* CyaSSL_RSA_generate_key(int len, unsigned long bits,
  3076. void(*f)(int, int, void*), void* data)
  3077. {
  3078. /* no tmp key needed, actual generation not supported */
  3079. CYASSL_ENTER("RSA_generate_key");
  3080. (void)len;
  3081. (void)bits;
  3082. (void)f;
  3083. (void)data;
  3084. return NULL;
  3085. }
  3086. /* return the next, if any, altname from the peer cert */
  3087. char* CyaSSL_X509_get_next_altname(CYASSL_X509* cert)
  3088. {
  3089. char* ret = NULL;
  3090. CYASSL_ENTER("CyaSSL_X509_get_next_altname");
  3091. /* don't have any to work with */
  3092. if (cert == NULL || cert->altNames == NULL)
  3093. return NULL;
  3094. /* already went through them */
  3095. if (cert->altNamesNext == NULL)
  3096. return NULL;
  3097. ret = cert->altNamesNext->name;
  3098. cert->altNamesNext = cert->altNamesNext->next;
  3099. return ret;
  3100. }
  3101. CYASSL_X509_NAME* CyaSSL_X509_get_issuer_name(CYASSL_X509* cert)
  3102. {
  3103. CYASSL_ENTER("X509_get_issuer_name");
  3104. return &cert->issuer;
  3105. }
  3106. CYASSL_X509_NAME* CyaSSL_X509_get_subject_name(CYASSL_X509* cert)
  3107. {
  3108. CYASSL_ENTER("X509_get_subject_name");
  3109. return &cert->subject;
  3110. }
  3111. /* copy name into in buffer, at most sz bytes, if buffer is null will
  3112. malloc buffer, call responsible for freeing */
  3113. char* CyaSSL_X509_NAME_oneline(CYASSL_X509_NAME* name, char* in, int sz)
  3114. {
  3115. int copySz = min(sz, name->sz);
  3116. CYASSL_ENTER("CyaSSL_X509_NAME_oneline");
  3117. if (!name->sz) return in;
  3118. if (!in) {
  3119. in = (char*)XMALLOC(name->sz, 0, DYNAMIC_TYPE_OPENSSL);
  3120. if (!in ) return in;
  3121. copySz = name->sz;
  3122. }
  3123. if (copySz == 0)
  3124. return in;
  3125. XMEMCPY(in, name->name, copySz - 1);
  3126. in[copySz - 1] = 0;
  3127. return in;
  3128. }
  3129. CYASSL_X509* CyaSSL_X509_STORE_CTX_get_current_cert(
  3130. CYASSL_X509_STORE_CTX* ctx)
  3131. {
  3132. (void)ctx;
  3133. return 0;
  3134. }
  3135. int CyaSSL_X509_STORE_CTX_get_error(CYASSL_X509_STORE_CTX* ctx)
  3136. {
  3137. (void)ctx;
  3138. return 0;
  3139. }
  3140. int CyaSSL_X509_STORE_CTX_get_error_depth(CYASSL_X509_STORE_CTX* ctx)
  3141. {
  3142. (void)ctx;
  3143. return 0;
  3144. }
  3145. CYASSL_BIO_METHOD* CyaSSL_BIO_f_buffer(void)
  3146. {
  3147. static CYASSL_BIO_METHOD meth;
  3148. CYASSL_ENTER("BIO_f_buffer");
  3149. meth.type = BIO_BUFFER;
  3150. return &meth;
  3151. }
  3152. long CyaSSL_BIO_set_write_buffer_size(CYASSL_BIO* bio, long size)
  3153. {
  3154. /* CyaSSL has internal buffer, compatibility only */
  3155. CYASSL_ENTER("BIO_set_write_buffer_size");
  3156. (void)bio;
  3157. return size;
  3158. }
  3159. CYASSL_BIO_METHOD* CyaSSL_BIO_f_ssl(void)
  3160. {
  3161. static CYASSL_BIO_METHOD meth;
  3162. CYASSL_ENTER("BIO_f_ssl");
  3163. meth.type = BIO_SSL;
  3164. return &meth;
  3165. }
  3166. CYASSL_BIO* CyaSSL_BIO_new_socket(int sfd, int closeF)
  3167. {
  3168. CYASSL_BIO* bio = (CYASSL_BIO*) XMALLOC(sizeof(CYASSL_BIO), 0,
  3169. DYNAMIC_TYPE_OPENSSL);
  3170. CYASSL_ENTER("BIO_new_socket");
  3171. if (bio) {
  3172. bio->type = BIO_SOCKET;
  3173. bio->close = (byte)closeF;
  3174. bio->eof = 0;
  3175. bio->ssl = 0;
  3176. bio->fd = sfd;
  3177. bio->prev = 0;
  3178. bio->next = 0;
  3179. }
  3180. return bio;
  3181. }
  3182. int CyaSSL_BIO_eof(CYASSL_BIO* b)
  3183. {
  3184. CYASSL_ENTER("BIO_eof");
  3185. if (b->eof)
  3186. return 1;
  3187. return 0;
  3188. }
  3189. long CyaSSL_BIO_set_ssl(CYASSL_BIO* b, CYASSL* ssl, int closeF)
  3190. {
  3191. CYASSL_ENTER("BIO_set_ssl");
  3192. b->ssl = ssl;
  3193. b->close = (byte)closeF;
  3194. /* add to ssl for bio free if SSL_free called before/instead of free_all? */
  3195. return 0;
  3196. }
  3197. CYASSL_BIO* CyaSSL_BIO_new(CYASSL_BIO_METHOD* method)
  3198. {
  3199. CYASSL_BIO* bio = (CYASSL_BIO*) XMALLOC(sizeof(CYASSL_BIO), 0,
  3200. DYNAMIC_TYPE_OPENSSL);
  3201. CYASSL_ENTER("BIO_new");
  3202. if (bio) {
  3203. bio->type = method->type;
  3204. bio->close = 0;
  3205. bio->eof = 0;
  3206. bio->ssl = NULL;
  3207. bio->mem = NULL;
  3208. bio->memLen = 0;
  3209. bio->fd = 0;
  3210. bio->prev = NULL;
  3211. bio->next = NULL;
  3212. }
  3213. return bio;
  3214. }
  3215. int CyaSSL_BIO_get_mem_data(CYASSL_BIO* bio, const byte** p)
  3216. {
  3217. if (bio == NULL || p == NULL)
  3218. return -1;
  3219. *p = bio->mem;
  3220. return bio->memLen;
  3221. }
  3222. CYASSL_BIO* CyaSSL_BIO_new_mem_buf(void* buf, int len)
  3223. {
  3224. CYASSL_BIO* bio = NULL;
  3225. if (buf == NULL)
  3226. return bio;
  3227. bio = CyaSSL_BIO_new(CyaSSL_BIO_s_mem());
  3228. if (bio == NULL)
  3229. return bio;
  3230. bio->memLen = len;
  3231. bio->mem = (byte*)XMALLOC(len, 0, DYNAMIC_TYPE_OPENSSL);
  3232. if (bio->mem == NULL) {
  3233. XFREE(bio, 0, DYNAMIC_TYPE_OPENSSL);
  3234. return NULL;
  3235. }
  3236. XMEMCPY(bio->mem, buf, len);
  3237. return bio;
  3238. }
  3239. #ifdef USE_WINDOWS_API
  3240. #define CloseSocket(s) closesocket(s)
  3241. #else
  3242. #define CloseSocket(s) close(s)
  3243. #endif
  3244. int CyaSSL_BIO_free(CYASSL_BIO* bio)
  3245. {
  3246. /* unchain?, doesn't matter in goahead since from free all */
  3247. CYASSL_ENTER("BIO_free");
  3248. if (bio) {
  3249. if (bio->close) {
  3250. if (bio->ssl)
  3251. CyaSSL_free(bio->ssl);
  3252. if (bio->fd)
  3253. CloseSocket(bio->fd);
  3254. }
  3255. if (bio->mem)
  3256. XFREE(bio->mem, 0, DYNAMIC_TYPE_OPENSSL);
  3257. XFREE(bio, 0, DYNAMIC_TYPE_OPENSSL);
  3258. }
  3259. return 0;
  3260. }
  3261. int CyaSSL_BIO_free_all(CYASSL_BIO* bio)
  3262. {
  3263. CYASSL_ENTER("BIO_free_all");
  3264. while (bio) {
  3265. CYASSL_BIO* next = bio->next;
  3266. CyaSSL_BIO_free(bio);
  3267. bio = next;
  3268. }
  3269. return 0;
  3270. }
  3271. int CyaSSL_BIO_read(CYASSL_BIO* bio, void* buf, int len)
  3272. {
  3273. int ret;
  3274. CYASSL* ssl = 0;
  3275. CYASSL_BIO* front = bio;
  3276. CYASSL_ENTER("BIO_read");
  3277. /* already got eof, again is error */
  3278. if (front->eof)
  3279. return SSL_FATAL_ERROR;
  3280. while(bio && ((ssl = bio->ssl) == 0) )
  3281. bio = bio->next;
  3282. if (ssl == 0) return BAD_FUNC_ARG;
  3283. ret = CyaSSL_read(ssl, buf, len);
  3284. if (ret == 0)
  3285. front->eof = 1;
  3286. else if (ret < 0) {
  3287. int err = CyaSSL_get_error(ssl, 0);
  3288. if ( !(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) )
  3289. front->eof = 1;
  3290. }
  3291. return ret;
  3292. }
  3293. int CyaSSL_BIO_write(CYASSL_BIO* bio, const void* data, int len)
  3294. {
  3295. int ret;
  3296. CYASSL* ssl = 0;
  3297. CYASSL_BIO* front = bio;
  3298. CYASSL_ENTER("BIO_write");
  3299. /* already got eof, again is error */
  3300. if (front->eof)
  3301. return SSL_FATAL_ERROR;
  3302. while(bio && ((ssl = bio->ssl) == 0) )
  3303. bio = bio->next;
  3304. if (ssl == 0) return BAD_FUNC_ARG;
  3305. ret = CyaSSL_write(ssl, data, len);
  3306. if (ret == 0)
  3307. front->eof = 1;
  3308. else if (ret < 0) {
  3309. int err = CyaSSL_get_error(ssl, 0);
  3310. if ( !(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) )
  3311. front->eof = 1;
  3312. }
  3313. return ret;
  3314. }
  3315. CYASSL_BIO* CyaSSL_BIO_push(CYASSL_BIO* top, CYASSL_BIO* append)
  3316. {
  3317. CYASSL_ENTER("BIO_push");
  3318. top->next = append;
  3319. append->prev = top;
  3320. return top;
  3321. }
  3322. int CyaSSL_BIO_flush(CYASSL_BIO* bio)
  3323. {
  3324. /* for CyaSSL no flushing needed */
  3325. CYASSL_ENTER("BIO_flush");
  3326. (void)bio;
  3327. return 1;
  3328. }
  3329. #endif /* OPENSSL_EXTRA || GOAHEAD_WS */
  3330. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  3331. void CyaSSL_CTX_set_default_passwd_cb_userdata(CYASSL_CTX* ctx,
  3332. void* userdata)
  3333. {
  3334. CYASSL_ENTER("SSL_CTX_set_default_passwd_cb_userdata");
  3335. ctx->userdata = userdata;
  3336. }
  3337. void CyaSSL_CTX_set_default_passwd_cb(CYASSL_CTX* ctx, pem_password_cb cb)
  3338. {
  3339. CYASSL_ENTER("SSL_CTX_set_default_passwd_cb");
  3340. ctx->passwd_cb = cb;
  3341. }
  3342. int CyaSSL_num_locks(void)
  3343. {
  3344. return 0;
  3345. }
  3346. void CyaSSL_set_locking_callback(void (*f)(int, int, const char*, int))
  3347. {
  3348. (void)f;
  3349. }
  3350. void CyaSSL_set_id_callback(unsigned long (*f)(void))
  3351. {
  3352. (void)f;
  3353. }
  3354. unsigned long CyaSSL_ERR_get_error(void)
  3355. {
  3356. /* TODO: */
  3357. return 0;
  3358. }
  3359. int CyaSSL_EVP_BytesToKey(const CYASSL_EVP_CIPHER* type,
  3360. const CYASSL_EVP_MD* md, const byte* salt,
  3361. const byte* data, int sz, int count, byte* key, byte* iv)
  3362. {
  3363. int keyLen = 0;
  3364. int ivLen = 0;
  3365. Md5 myMD;
  3366. byte digest[MD5_DIGEST_SIZE];
  3367. int j;
  3368. int keyLeft;
  3369. int ivLeft;
  3370. int keyOutput = 0;
  3371. CYASSL_ENTER("EVP_BytesToKey");
  3372. InitMd5(&myMD);
  3373. /* only support MD5 for now */
  3374. if (XSTRNCMP(md, "MD5", 3) != 0) return 0;
  3375. /* only support CBC DES and AES for now */
  3376. if (XSTRNCMP(type, "DES-CBC", 7) == 0) {
  3377. keyLen = DES_KEY_SIZE;
  3378. ivLen = DES_IV_SIZE;
  3379. }
  3380. else if (XSTRNCMP(type, "DES-EDE3-CBC", 12) == 0) {
  3381. keyLen = DES3_KEY_SIZE;
  3382. ivLen = DES_IV_SIZE;
  3383. }
  3384. else if (XSTRNCMP(type, "AES-128-CBC", 11) == 0) {
  3385. keyLen = AES_128_KEY_SIZE;
  3386. ivLen = AES_IV_SIZE;
  3387. }
  3388. else if (XSTRNCMP(type, "AES-192-CBC", 11) == 0) {
  3389. keyLen = AES_192_KEY_SIZE;
  3390. ivLen = AES_IV_SIZE;
  3391. }
  3392. else if (XSTRNCMP(type, "AES-256-CBC", 11) == 0) {
  3393. keyLen = AES_256_KEY_SIZE;
  3394. ivLen = AES_IV_SIZE;
  3395. }
  3396. else
  3397. return 0;
  3398. keyLeft = keyLen;
  3399. ivLeft = ivLen;
  3400. while (keyOutput < (keyLen + ivLen)) {
  3401. int digestLeft = MD5_DIGEST_SIZE;
  3402. /* D_(i - 1) */
  3403. if (keyOutput) /* first time D_0 is empty */
  3404. Md5Update(&myMD, digest, MD5_DIGEST_SIZE);
  3405. /* data */
  3406. Md5Update(&myMD, data, sz);
  3407. /* salt */
  3408. if (salt)
  3409. Md5Update(&myMD, salt, EVP_SALT_SIZE);
  3410. Md5Final(&myMD, digest);
  3411. /* count */
  3412. for (j = 1; j < count; j++) {
  3413. Md5Update(&myMD, digest, MD5_DIGEST_SIZE);
  3414. Md5Final(&myMD, digest);
  3415. }
  3416. if (keyLeft) {
  3417. int store = min(keyLeft, MD5_DIGEST_SIZE);
  3418. XMEMCPY(&key[keyLen - keyLeft], digest, store);
  3419. keyOutput += store;
  3420. keyLeft -= store;
  3421. digestLeft -= store;
  3422. }
  3423. if (ivLeft && digestLeft) {
  3424. int store = min(ivLeft, digestLeft);
  3425. XMEMCPY(&iv[ivLen - ivLeft], &digest[MD5_DIGEST_SIZE -
  3426. digestLeft], store);
  3427. keyOutput += store;
  3428. ivLeft -= store;
  3429. }
  3430. }
  3431. if (keyOutput != (keyLen + ivLen))
  3432. return 0;
  3433. return keyOutput;
  3434. }
  3435. #endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
  3436. #ifdef OPENSSL_EXTRA
  3437. unsigned long CyaSSLeay(void)
  3438. {
  3439. return SSLEAY_VERSION_NUMBER;
  3440. }
  3441. const char* CyaSSLeay_version(int type)
  3442. {
  3443. static const char* version = "SSLeay CyaSSL compatibility";
  3444. (void)type;
  3445. return version;
  3446. }
  3447. void CyaSSL_MD5_Init(CYASSL_MD5_CTX* md5)
  3448. {
  3449. typedef char md5_test[sizeof(MD5_CTX) >= sizeof(Md5) ? 1 : -1];
  3450. (void)sizeof(md5_test);
  3451. CYASSL_ENTER("MD5_Init");
  3452. InitMd5((Md5*)md5);
  3453. }
  3454. void CyaSSL_MD5_Update(CYASSL_MD5_CTX* md5, const void* input,
  3455. unsigned long sz)
  3456. {
  3457. CYASSL_ENTER("CyaSSL_MD5_Update");
  3458. Md5Update((Md5*)md5, (const byte*)input, (word32)sz);
  3459. }
  3460. void CyaSSL_MD5_Final(byte* input, CYASSL_MD5_CTX* md5)
  3461. {
  3462. CYASSL_ENTER("MD5_Final");
  3463. Md5Final((Md5*)md5, input);
  3464. }
  3465. void CyaSSL_SHA_Init(CYASSL_SHA_CTX* sha)
  3466. {
  3467. typedef char sha_test[sizeof(SHA_CTX) >= sizeof(Sha) ? 1 : -1];
  3468. (void)sizeof(sha_test);
  3469. CYASSL_ENTER("SHA_Init");
  3470. InitSha((Sha*)sha);
  3471. }
  3472. void CyaSSL_SHA_Update(CYASSL_SHA_CTX* sha, const void* input,
  3473. unsigned long sz)
  3474. {
  3475. CYASSL_ENTER("SHA_Update");
  3476. ShaUpdate((Sha*)sha, (const byte*)input, (word32)sz);
  3477. }
  3478. void CyaSSL_SHA_Final(byte* input, CYASSL_SHA_CTX* sha)
  3479. {
  3480. CYASSL_ENTER("SHA_Final");
  3481. ShaFinal((Sha*)sha, input);
  3482. }
  3483. void CyaSSL_SHA1_Init(CYASSL_SHA_CTX* sha)
  3484. {
  3485. CYASSL_ENTER("SHA1_Init");
  3486. SHA_Init(sha);
  3487. }
  3488. void CyaSSL_SHA1_Update(CYASSL_SHA_CTX* sha, const void* input,
  3489. unsigned long sz)
  3490. {
  3491. CYASSL_ENTER("SHA1_Update");
  3492. SHA_Update(sha, input, sz);
  3493. }
  3494. void CyaSSL_SHA1_Final(byte* input, CYASSL_SHA_CTX* sha)
  3495. {
  3496. CYASSL_ENTER("SHA1_Final");
  3497. SHA_Final(input, sha);
  3498. }
  3499. void CyaSSL_SHA256_Init(CYASSL_SHA256_CTX* sha256)
  3500. {
  3501. typedef char sha_test[sizeof(SHA256_CTX) >= sizeof(Sha256) ? 1 : -1];
  3502. (void)sizeof(sha_test);
  3503. CYASSL_ENTER("SHA256_Init");
  3504. InitSha256((Sha256*)sha256);
  3505. }
  3506. void CyaSSL_SHA256_Update(CYASSL_SHA256_CTX* sha, const void* input,
  3507. unsigned long sz)
  3508. {
  3509. CYASSL_ENTER("SHA256_Update");
  3510. Sha256Update((Sha256*)sha, (const byte*)input, (word32)sz);
  3511. }
  3512. void CyaSSL_SHA256_Final(byte* input, CYASSL_SHA256_CTX* sha)
  3513. {
  3514. CYASSL_ENTER("SHA256_Final");
  3515. Sha256Final((Sha256*)sha, input);
  3516. }
  3517. #ifdef CYASSL_SHA384
  3518. void CyaSSL_SHA384_Init(CYASSL_SHA384_CTX* sha)
  3519. {
  3520. typedef char sha_test[sizeof(SHA384_CTX) >= sizeof(Sha384) ? 1 : -1];
  3521. (void)sizeof(sha_test);
  3522. CYASSL_ENTER("SHA384_Init");
  3523. InitSha384((Sha384*)sha);
  3524. }
  3525. void CyaSSL_SHA384_Update(CYASSL_SHA384_CTX* sha, const void* input,
  3526. unsigned long sz)
  3527. {
  3528. CYASSL_ENTER("SHA384_Update");
  3529. Sha384Update((Sha384*)sha, (const byte*)input, (word32)sz);
  3530. }
  3531. void CyaSSL_SHA384_Final(byte* input, CYASSL_SHA384_CTX* sha)
  3532. {
  3533. CYASSL_ENTER("SHA384_Final");
  3534. Sha384Final((Sha384*)sha, input);
  3535. }
  3536. #endif /* CYASSL_SHA384 */
  3537. #ifdef CYASSL_SHA512
  3538. void CyaSSL_SHA512_Init(CYASSL_SHA512_CTX* sha)
  3539. {
  3540. typedef char sha_test[sizeof(SHA512_CTX) >= sizeof(Sha512) ? 1 : -1];
  3541. (void)sizeof(sha_test);
  3542. CYASSL_ENTER("SHA512_Init");
  3543. InitSha512((Sha512*)sha);
  3544. }
  3545. void CyaSSL_SHA512_Update(CYASSL_SHA512_CTX* sha, const void* input,
  3546. unsigned long sz)
  3547. {
  3548. CYASSL_ENTER("SHA512_Update");
  3549. Sha512Update((Sha512*)sha, (const byte*)input, (word32)sz);
  3550. }
  3551. void CyaSSL_SHA512_Final(byte* input, CYASSL_SHA512_CTX* sha)
  3552. {
  3553. CYASSL_ENTER("SHA512_Final");
  3554. Sha512Final((Sha512*)sha, input);
  3555. }
  3556. #endif /* CYASSL_SHA512 */
  3557. const CYASSL_EVP_MD* CyaSSL_EVP_md5(void)
  3558. {
  3559. static const char* type = "MD5";
  3560. CYASSL_ENTER("EVP_md5");
  3561. return type;
  3562. }
  3563. const CYASSL_EVP_MD* CyaSSL_EVP_sha1(void)
  3564. {
  3565. static const char* type = "SHA";
  3566. CYASSL_ENTER("EVP_sha1");
  3567. return type;
  3568. }
  3569. const CYASSL_EVP_MD* CyaSSL_EVP_sha256(void)
  3570. {
  3571. static const char* type = "SHA256";
  3572. CYASSL_ENTER("EVP_sha256");
  3573. return type;
  3574. }
  3575. #ifdef CYASSL_SHA384
  3576. const CYASSL_EVP_MD* CyaSSL_EVP_sha384(void)
  3577. {
  3578. static const char* type = "SHA384";
  3579. CYASSL_ENTER("EVP_sha384");
  3580. return type;
  3581. }
  3582. #endif /* CYASSL_SHA384 */
  3583. #ifdef CYASSL_SHA512
  3584. const CYASSL_EVP_MD* CyaSSL_EVP_sha512(void)
  3585. {
  3586. static const char* type = "SHA512";
  3587. CYASSL_ENTER("EVP_sha512");
  3588. return type;
  3589. }
  3590. #endif /* CYASSL_SHA512 */
  3591. void CyaSSL_EVP_MD_CTX_init(CYASSL_EVP_MD_CTX* ctx)
  3592. {
  3593. CYASSL_ENTER("EVP_CIPHER_MD_CTX_init");
  3594. (void)ctx;
  3595. /* do nothing */
  3596. }
  3597. const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_128_cbc(void)
  3598. {
  3599. static const char* type = "AES128-CBC";
  3600. CYASSL_ENTER("CyaSSL_EVP_aes_128_cbc");
  3601. return type;
  3602. }
  3603. const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_192_cbc(void)
  3604. {
  3605. static const char* type = "AES192-CBC";
  3606. CYASSL_ENTER("CyaSSL_EVP_aes_192_cbc");
  3607. return type;
  3608. }
  3609. const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_256_cbc(void)
  3610. {
  3611. static const char* type = "AES256-CBC";
  3612. CYASSL_ENTER("CyaSSL_EVP_aes_256_cbc");
  3613. return type;
  3614. }
  3615. const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_128_ctr(void)
  3616. {
  3617. static const char* type = "AES128-CTR";
  3618. CYASSL_ENTER("CyaSSL_EVP_aes_128_ctr");
  3619. return type;
  3620. }
  3621. const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_192_ctr(void)
  3622. {
  3623. static const char* type = "AES192-CTR";
  3624. CYASSL_ENTER("CyaSSL_EVP_aes_192_ctr");
  3625. return type;
  3626. }
  3627. const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_256_ctr(void)
  3628. {
  3629. static const char* type = "AES256-CTR";
  3630. CYASSL_ENTER("CyaSSL_EVP_aes_256_ctr");
  3631. return type;
  3632. }
  3633. const CYASSL_EVP_CIPHER* CyaSSL_EVP_des_cbc(void)
  3634. {
  3635. static const char* type = "DES-CBC";
  3636. CYASSL_ENTER("CyaSSL_EVP_des_cbc");
  3637. return type;
  3638. }
  3639. const CYASSL_EVP_CIPHER* CyaSSL_EVP_des_ede3_cbc(void)
  3640. {
  3641. static const char* type = "DES-EDE3-CBC";
  3642. CYASSL_ENTER("CyaSSL_EVP_des_ede3_cbc");
  3643. return type;
  3644. }
  3645. const CYASSL_EVP_CIPHER* CyaSSL_EVP_rc4(void)
  3646. {
  3647. static const char* type = "ARC4";
  3648. CYASSL_ENTER("CyaSSL_EVP_rc4");
  3649. return type;
  3650. }
  3651. const CYASSL_EVP_CIPHER* CyaSSL_EVP_enc_null(void)
  3652. {
  3653. static const char* type = "NULL";
  3654. CYASSL_ENTER("CyaSSL_EVP_enc_null");
  3655. return type;
  3656. }
  3657. int CyaSSL_EVP_MD_CTX_cleanup(CYASSL_EVP_MD_CTX* ctx)
  3658. {
  3659. CYASSL_ENTER("EVP_MD_CTX_cleanup");
  3660. (void)ctx;
  3661. return 0;
  3662. }
  3663. void CyaSSL_EVP_CIPHER_CTX_init(CYASSL_EVP_CIPHER_CTX* ctx)
  3664. {
  3665. CYASSL_ENTER("EVP_CIPHER_CTX_init");
  3666. if (ctx) {
  3667. ctx->cipherType = 0xff; /* no init */
  3668. ctx->keyLen = 0;
  3669. ctx->enc = 1; /* start in encrypt mode */
  3670. }
  3671. }
  3672. int CyaSSL_EVP_CIPHER_CTX_cleanup(CYASSL_EVP_CIPHER_CTX* ctx)
  3673. {
  3674. CYASSL_ENTER("EVP_CIPHER_CTX_cleanup");
  3675. if (ctx) {
  3676. ctx->cipherType = 0xff; /* no more init */
  3677. ctx->keyLen = 0;
  3678. }
  3679. return 1; /* success */
  3680. }
  3681. int CyaSSL_EVP_CipherInit(CYASSL_EVP_CIPHER_CTX* ctx,
  3682. const CYASSL_EVP_CIPHER* type, byte* key,
  3683. byte* iv, int enc)
  3684. {
  3685. CYASSL_ENTER("CyaSSL_EVP_CipherInit");
  3686. if (ctx == NULL) {
  3687. CYASSL_MSG("no ctx");
  3688. return 0; /* failure */
  3689. }
  3690. if (type == NULL && ctx->cipherType == 0xff) {
  3691. CYASSL_MSG("no type set");
  3692. return 0; /* failure */
  3693. }
  3694. if (ctx->cipherType == AES_128_CBC_TYPE || (type &&
  3695. XSTRNCMP(type, "AES128-CBC", 10) == 0)) {
  3696. CYASSL_MSG("AES-128-CBC");
  3697. ctx->cipherType = AES_128_CBC_TYPE;
  3698. ctx->keyLen = 16;
  3699. if (enc == 0 || enc == 1)
  3700. ctx->enc = enc ? 1 : 0;
  3701. if (key)
  3702. AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  3703. ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
  3704. if (iv && key == NULL)
  3705. AesSetIV(&ctx->cipher.aes, iv);
  3706. }
  3707. else if (ctx->cipherType == AES_192_CBC_TYPE || (type &&
  3708. XSTRNCMP(type, "AES192-CBC", 10) == 0)) {
  3709. CYASSL_MSG("AES-192-CBC");
  3710. ctx->cipherType = AES_192_CBC_TYPE;
  3711. ctx->keyLen = 24;
  3712. if (enc == 0 || enc == 1)
  3713. ctx->enc = enc ? 1 : 0;
  3714. if (key)
  3715. AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  3716. ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
  3717. if (iv && key == NULL)
  3718. AesSetIV(&ctx->cipher.aes, iv);
  3719. }
  3720. else if (ctx->cipherType == AES_256_CBC_TYPE || (type &&
  3721. XSTRNCMP(type, "AES256-CBC", 10) == 0)) {
  3722. CYASSL_MSG("AES-256-CBC");
  3723. ctx->cipherType = AES_256_CBC_TYPE;
  3724. ctx->keyLen = 32;
  3725. if (enc == 0 || enc == 1)
  3726. ctx->enc = enc ? 1 : 0;
  3727. if (key)
  3728. AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  3729. ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
  3730. if (iv && key == NULL)
  3731. AesSetIV(&ctx->cipher.aes, iv);
  3732. }
  3733. #ifdef CYASSL_AES_COUNTER
  3734. else if (ctx->cipherType == AES_128_CTR_TYPE || (type &&
  3735. XSTRNCMP(type, "AES128-CTR", 10) == 0)) {
  3736. CYASSL_MSG("AES-128-CTR");
  3737. ctx->cipherType = AES_128_CTR_TYPE;
  3738. ctx->keyLen = 16;
  3739. if (enc == 0 || enc == 1)
  3740. ctx->enc = enc ? 1 : 0;
  3741. if (key)
  3742. AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  3743. AES_ENCRYPTION);
  3744. if (iv && key == NULL)
  3745. AesSetIV(&ctx->cipher.aes, iv);
  3746. }
  3747. else if (ctx->cipherType == AES_192_CTR_TYPE || (type &&
  3748. XSTRNCMP(type, "AES192-CTR", 10) == 0)) {
  3749. CYASSL_MSG("AES-192-CTR");
  3750. ctx->cipherType = AES_192_CTR_TYPE;
  3751. ctx->keyLen = 24;
  3752. if (enc == 0 || enc == 1)
  3753. ctx->enc = enc ? 1 : 0;
  3754. if (key)
  3755. AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  3756. AES_ENCRYPTION);
  3757. if (iv && key == NULL)
  3758. AesSetIV(&ctx->cipher.aes, iv);
  3759. }
  3760. else if (ctx->cipherType == AES_256_CTR_TYPE || (type &&
  3761. XSTRNCMP(type, "AES256-CTR", 10) == 0)) {
  3762. CYASSL_MSG("AES-256-CTR");
  3763. ctx->cipherType = AES_256_CTR_TYPE;
  3764. ctx->keyLen = 32;
  3765. if (enc == 0 || enc == 1)
  3766. ctx->enc = enc ? 1 : 0;
  3767. if (key)
  3768. AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  3769. AES_ENCRYPTION);
  3770. if (iv && key == NULL)
  3771. AesSetIV(&ctx->cipher.aes, iv);
  3772. }
  3773. #endif /* CYASSL_AES_CTR */
  3774. else if (ctx->cipherType == DES_CBC_TYPE || (type &&
  3775. XSTRNCMP(type, "DES-CBC", 7) == 0)) {
  3776. CYASSL_MSG("DES-CBC");
  3777. ctx->cipherType = DES_CBC_TYPE;
  3778. ctx->keyLen = 8;
  3779. if (enc == 0 || enc == 1)
  3780. ctx->enc = enc ? 1 : 0;
  3781. if (key)
  3782. Des_SetKey(&ctx->cipher.des, key, iv,
  3783. ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION);
  3784. if (iv && key == NULL)
  3785. Des_SetIV(&ctx->cipher.des, iv);
  3786. }
  3787. else if (ctx->cipherType == DES_EDE3_CBC_TYPE || (type &&
  3788. XSTRNCMP(type, "DES-EDE3-CBC", 11) == 0)) {
  3789. CYASSL_MSG("DES-EDE3-CBC");
  3790. ctx->cipherType = DES_EDE3_CBC_TYPE;
  3791. ctx->keyLen = 24;
  3792. if (enc == 0 || enc == 1)
  3793. ctx->enc = enc ? 1 : 0;
  3794. if (key)
  3795. Des3_SetKey(&ctx->cipher.des3, key, iv,
  3796. ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION);
  3797. if (iv && key == NULL)
  3798. Des3_SetIV(&ctx->cipher.des3, iv);
  3799. }
  3800. else if (ctx->cipherType == ARC4_TYPE || (type &&
  3801. XSTRNCMP(type, "ARC4", 4) == 0)) {
  3802. CYASSL_MSG("ARC4");
  3803. ctx->cipherType = ARC4_TYPE;
  3804. if (ctx->keyLen == 0) /* user may have already set */
  3805. ctx->keyLen = 16; /* default to 128 */
  3806. if (key)
  3807. Arc4SetKey(&ctx->cipher.arc4, key, ctx->keyLen);
  3808. }
  3809. else if (ctx->cipherType == NULL_CIPHER_TYPE || (type &&
  3810. XSTRNCMP(type, "NULL", 4) == 0)) {
  3811. CYASSL_MSG("NULL cipher");
  3812. ctx->cipherType = NULL_CIPHER_TYPE;
  3813. ctx->keyLen = 0;
  3814. }
  3815. else
  3816. return 0; /* failure */
  3817. return 1; /* success */
  3818. }
  3819. int CyaSSL_EVP_CIPHER_CTX_key_length(CYASSL_EVP_CIPHER_CTX* ctx)
  3820. {
  3821. CYASSL_ENTER("CyaSSL_EVP_CIPHER_CTX_key_length");
  3822. if (ctx)
  3823. return ctx->keyLen;
  3824. return 0; /* failure */
  3825. }
  3826. int CyaSSL_EVP_CIPHER_CTX_set_key_length(CYASSL_EVP_CIPHER_CTX* ctx,
  3827. int keylen)
  3828. {
  3829. CYASSL_ENTER("CyaSSL_EVP_CIPHER_CTX_set_key_length");
  3830. if (ctx)
  3831. ctx->keyLen = keylen;
  3832. else
  3833. return 0; /* failure */
  3834. return 1; /* success */
  3835. }
  3836. int CyaSSL_EVP_Cipher(CYASSL_EVP_CIPHER_CTX* ctx, byte* dst, byte* src,
  3837. word32 len)
  3838. {
  3839. CYASSL_ENTER("CyaSSL_EVP_Cipher");
  3840. if (ctx == NULL || dst == NULL || src == NULL) {
  3841. CYASSL_MSG("Bad function argument");
  3842. return 0; /* failure */
  3843. }
  3844. if (ctx->cipherType == 0xff) {
  3845. CYASSL_MSG("no init");
  3846. return 0; /* failure */
  3847. }
  3848. switch (ctx->cipherType) {
  3849. case AES_128_CBC_TYPE :
  3850. case AES_192_CBC_TYPE :
  3851. case AES_256_CBC_TYPE :
  3852. CYASSL_MSG("AES CBC");
  3853. if (ctx->enc)
  3854. AesCbcEncrypt(&ctx->cipher.aes, dst, src, len);
  3855. else
  3856. AesCbcDecrypt(&ctx->cipher.aes, dst, src, len);
  3857. break;
  3858. #ifdef CYASSL_AES_COUNTER
  3859. case AES_128_CTR_TYPE :
  3860. case AES_192_CTR_TYPE :
  3861. case AES_256_CTR_TYPE :
  3862. CYASSL_MSG("AES CTR");
  3863. AesCtrEncrypt(&ctx->cipher.aes, dst, src, len);
  3864. break;
  3865. #endif
  3866. case DES_CBC_TYPE :
  3867. if (ctx->enc)
  3868. Des_CbcEncrypt(&ctx->cipher.des, dst, src, len);
  3869. else
  3870. Des_CbcDecrypt(&ctx->cipher.des, dst, src, len);
  3871. break;
  3872. case DES_EDE3_CBC_TYPE :
  3873. if (ctx->enc)
  3874. Des3_CbcEncrypt(&ctx->cipher.des3, dst, src, len);
  3875. else
  3876. Des3_CbcDecrypt(&ctx->cipher.des3, dst, src, len);
  3877. break;
  3878. case ARC4_TYPE :
  3879. Arc4Process(&ctx->cipher.arc4, dst, src, len);
  3880. break;
  3881. case NULL_CIPHER_TYPE :
  3882. XMEMCPY(dst, src, len);
  3883. break;
  3884. default: {
  3885. CYASSL_MSG("bad type");
  3886. return 0; /* failure */
  3887. }
  3888. }
  3889. CYASSL_MSG("CyaSSL_EVP_Cipher success");
  3890. return 1; /* success */
  3891. }
  3892. /* store for external read of iv, 0 on success */
  3893. int CyaSSL_StoreExternalIV(CYASSL_EVP_CIPHER_CTX* ctx)
  3894. {
  3895. CYASSL_ENTER("CyaSSL_StoreExternalIV");
  3896. if (ctx == NULL) {
  3897. CYASSL_MSG("Bad function argument");
  3898. return -1;
  3899. }
  3900. switch (ctx->cipherType) {
  3901. case AES_128_CBC_TYPE :
  3902. case AES_192_CBC_TYPE :
  3903. case AES_256_CBC_TYPE :
  3904. CYASSL_MSG("AES CBC");
  3905. memcpy(ctx->iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
  3906. break;
  3907. #ifdef CYASSL_AES_COUNTER
  3908. case AES_128_CTR_TYPE :
  3909. case AES_192_CTR_TYPE :
  3910. case AES_256_CTR_TYPE :
  3911. CYASSL_MSG("AES CTR");
  3912. memcpy(ctx->iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
  3913. break;
  3914. #endif
  3915. case DES_CBC_TYPE :
  3916. CYASSL_MSG("DES CBC");
  3917. memcpy(ctx->iv, &ctx->cipher.des.reg, DES_BLOCK_SIZE);
  3918. break;
  3919. case DES_EDE3_CBC_TYPE :
  3920. CYASSL_MSG("DES EDE3 CBC");
  3921. memcpy(ctx->iv, &ctx->cipher.des.reg, DES_BLOCK_SIZE);
  3922. break;
  3923. case ARC4_TYPE :
  3924. CYASSL_MSG("ARC4");
  3925. break;
  3926. case NULL_CIPHER_TYPE :
  3927. CYASSL_MSG("NULL");
  3928. break;
  3929. default: {
  3930. CYASSL_MSG("bad type");
  3931. return -1; /* failure */
  3932. }
  3933. }
  3934. return 0; /* success */
  3935. }
  3936. /* set internal IV from external, 0 on success */
  3937. int CyaSSL_SetInternalIV(CYASSL_EVP_CIPHER_CTX* ctx)
  3938. {
  3939. CYASSL_ENTER("CyaSSL_SetInternalIV");
  3940. if (ctx == NULL) {
  3941. CYASSL_MSG("Bad function argument");
  3942. return -1;
  3943. }
  3944. switch (ctx->cipherType) {
  3945. case AES_128_CBC_TYPE :
  3946. case AES_192_CBC_TYPE :
  3947. case AES_256_CBC_TYPE :
  3948. CYASSL_MSG("AES CBC");
  3949. memcpy(&ctx->cipher.aes.reg, ctx->iv, AES_BLOCK_SIZE);
  3950. break;
  3951. #ifdef CYASSL_AES_COUNTER
  3952. case AES_128_CTR_TYPE :
  3953. case AES_192_CTR_TYPE :
  3954. case AES_256_CTR_TYPE :
  3955. CYASSL_MSG("AES CTR");
  3956. memcpy(&ctx->cipher.aes.reg, ctx->iv, AES_BLOCK_SIZE);
  3957. break;
  3958. #endif
  3959. case DES_CBC_TYPE :
  3960. CYASSL_MSG("DES CBC");
  3961. memcpy(&ctx->cipher.des.reg, ctx->iv, DES_BLOCK_SIZE);
  3962. break;
  3963. case DES_EDE3_CBC_TYPE :
  3964. CYASSL_MSG("DES EDE3 CBC");
  3965. memcpy(&ctx->cipher.des.reg, ctx->iv, DES_BLOCK_SIZE);
  3966. break;
  3967. case ARC4_TYPE :
  3968. CYASSL_MSG("ARC4");
  3969. break;
  3970. case NULL_CIPHER_TYPE :
  3971. CYASSL_MSG("NULL");
  3972. break;
  3973. default: {
  3974. CYASSL_MSG("bad type");
  3975. return -1; /* failure */
  3976. }
  3977. }
  3978. return 0; /* success */
  3979. }
  3980. int CyaSSL_EVP_DigestInit(CYASSL_EVP_MD_CTX* ctx, const CYASSL_EVP_MD* type)
  3981. {
  3982. CYASSL_ENTER("EVP_DigestInit");
  3983. if (XSTRNCMP(type, "MD5", 3) == 0) {
  3984. ctx->macType = MD5;
  3985. CyaSSL_MD5_Init((MD5_CTX*)&ctx->hash);
  3986. }
  3987. else if (XSTRNCMP(type, "SHA256", 6) == 0) {
  3988. ctx->macType = SHA256;
  3989. CyaSSL_SHA256_Init((SHA256_CTX*)&ctx->hash);
  3990. }
  3991. #ifdef CYASSL_SHA384
  3992. else if (XSTRNCMP(type, "SHA384", 6) == 0) {
  3993. ctx->macType = SHA384;
  3994. CyaSSL_SHA384_Init((SHA384_CTX*)&ctx->hash);
  3995. }
  3996. #endif
  3997. #ifdef CYASSL_SHA512
  3998. else if (XSTRNCMP(type, "SHA512", 6) == 0) {
  3999. ctx->macType = SHA512;
  4000. CyaSSL_SHA512_Init((SHA512_CTX*)&ctx->hash);
  4001. }
  4002. #endif
  4003. /* has to be last since would pick or 256, 384, or 512 too */
  4004. else if (XSTRNCMP(type, "SHA", 3) == 0) {
  4005. ctx->macType = SHA;
  4006. CyaSSL_SHA_Init((SHA_CTX*)&ctx->hash);
  4007. }
  4008. else
  4009. return BAD_FUNC_ARG;
  4010. return 0;
  4011. }
  4012. int CyaSSL_EVP_DigestUpdate(CYASSL_EVP_MD_CTX* ctx, const void* data,
  4013. unsigned long sz)
  4014. {
  4015. CYASSL_ENTER("EVP_DigestUpdate");
  4016. if (ctx->macType == MD5)
  4017. CyaSSL_MD5_Update((MD5_CTX*)&ctx->hash, data, (unsigned long)sz);
  4018. else if (ctx->macType == SHA)
  4019. CyaSSL_SHA_Update((SHA_CTX*)&ctx->hash, data, (unsigned long)sz);
  4020. else if (ctx->macType == SHA256)
  4021. CyaSSL_SHA256_Update((SHA256_CTX*)&ctx->hash, data,
  4022. (unsigned long)sz);
  4023. #ifdef CYASSL_SHA384
  4024. else if (ctx->macType == SHA384)
  4025. CyaSSL_SHA384_Update((SHA384_CTX*)&ctx->hash, data,
  4026. (unsigned long)sz);
  4027. #endif
  4028. #ifdef CYASSL_SHA512
  4029. else if (ctx->macType == SHA512)
  4030. CyaSSL_SHA512_Update((SHA512_CTX*)&ctx->hash, data,
  4031. (unsigned long)sz);
  4032. #endif
  4033. else
  4034. return BAD_FUNC_ARG;
  4035. return 0;
  4036. }
  4037. int CyaSSL_EVP_DigestFinal(CYASSL_EVP_MD_CTX* ctx, unsigned char* md,
  4038. unsigned int* s)
  4039. {
  4040. CYASSL_ENTER("EVP_DigestFinal");
  4041. if (ctx->macType == MD5) {
  4042. CyaSSL_MD5_Final(md, (MD5_CTX*)&ctx->hash);
  4043. if (s) *s = MD5_DIGEST_SIZE;
  4044. }
  4045. else if (ctx->macType == SHA) {
  4046. CyaSSL_SHA_Final(md, (SHA_CTX*)&ctx->hash);
  4047. if (s) *s = SHA_DIGEST_SIZE;
  4048. }
  4049. else if (ctx->macType == SHA256) {
  4050. CyaSSL_SHA256_Final(md, (SHA256_CTX*)&ctx->hash);
  4051. if (s) *s = SHA256_DIGEST_SIZE;
  4052. }
  4053. #ifdef CYASSL_SHA384
  4054. else if (ctx->macType == SHA384) {
  4055. CyaSSL_SHA384_Final(md, (SHA384_CTX*)&ctx->hash);
  4056. if (s) *s = SHA384_DIGEST_SIZE;
  4057. }
  4058. #endif
  4059. #ifdef CYASSL_SHA512
  4060. else if (ctx->macType == SHA512) {
  4061. CyaSSL_SHA512_Final(md, (SHA512_CTX*)&ctx->hash);
  4062. if (s) *s = SHA512_DIGEST_SIZE;
  4063. }
  4064. #endif
  4065. else
  4066. return BAD_FUNC_ARG;
  4067. return 0;
  4068. }
  4069. int CyaSSL_EVP_DigestFinal_ex(CYASSL_EVP_MD_CTX* ctx, unsigned char* md,
  4070. unsigned int* s)
  4071. {
  4072. CYASSL_ENTER("EVP_DigestFinal_ex");
  4073. return EVP_DigestFinal(ctx, md, s);
  4074. }
  4075. unsigned char* CyaSSL_HMAC(const CYASSL_EVP_MD* evp_md, const void* key,
  4076. int key_len, const unsigned char* d, int n,
  4077. unsigned char* md, unsigned int* md_len)
  4078. {
  4079. Hmac hmac;
  4080. CYASSL_ENTER("HMAC");
  4081. if (!md) return 0; /* no static buffer support */
  4082. if (XSTRNCMP(evp_md, "MD5", 3) == 0) {
  4083. HmacSetKey(&hmac, MD5, (const byte*)key, key_len);
  4084. if (md_len) *md_len = MD5_DIGEST_SIZE;
  4085. }
  4086. else if (XSTRNCMP(evp_md, "SHA", 3) == 0) {
  4087. HmacSetKey(&hmac, SHA, (const byte*)key, key_len);
  4088. if (md_len) *md_len = SHA_DIGEST_SIZE;
  4089. }
  4090. else
  4091. return 0;
  4092. HmacUpdate(&hmac, d, n);
  4093. HmacFinal(&hmac, md);
  4094. return md;
  4095. }
  4096. void CyaSSL_ERR_clear_error(void)
  4097. {
  4098. /* TODO: */
  4099. }
  4100. int CyaSSL_RAND_status(void)
  4101. {
  4102. return 1; /* CTaoCrypt provides enough seed internally */
  4103. }
  4104. void CyaSSL_RAND_add(const void* add, int len, double entropy)
  4105. {
  4106. (void)add;
  4107. (void)len;
  4108. (void)entropy;
  4109. /* CyaSSL seeds/adds internally, use explicit RNG if you want
  4110. to take control */
  4111. }
  4112. int CyaSSL_DES_key_sched(CYASSL_const_DES_cblock* key,
  4113. CYASSL_DES_key_schedule* schedule)
  4114. {
  4115. CYASSL_ENTER("DES_key_sched");
  4116. XMEMCPY(schedule, key, sizeof(const_DES_cblock));
  4117. return 0;
  4118. }
  4119. void CyaSSL_DES_cbc_encrypt(const unsigned char* input,
  4120. unsigned char* output, long length,
  4121. CYASSL_DES_key_schedule* schedule, CYASSL_DES_cblock* ivec,
  4122. int enc)
  4123. {
  4124. Des myDes;
  4125. CYASSL_ENTER("DES_cbc_encrypt");
  4126. Des_SetKey(&myDes, (const byte*)schedule, (const byte*)ivec, !enc);
  4127. if (enc)
  4128. Des_CbcEncrypt(&myDes, output, input, (word32)length);
  4129. else
  4130. Des_CbcDecrypt(&myDes, output, input, (word32)length);
  4131. }
  4132. /* correctly sets ivec for next call */
  4133. void CyaSSL_DES_ncbc_encrypt(const unsigned char* input,
  4134. unsigned char* output, long length,
  4135. CYASSL_DES_key_schedule* schedule, CYASSL_DES_cblock* ivec,
  4136. int enc)
  4137. {
  4138. Des myDes;
  4139. CYASSL_ENTER("DES_ncbc_encrypt");
  4140. Des_SetKey(&myDes, (const byte*)schedule, (const byte*)ivec, !enc);
  4141. if (enc)
  4142. Des_CbcEncrypt(&myDes, output, input, (word32)length);
  4143. else
  4144. Des_CbcDecrypt(&myDes, output, input, (word32)length);
  4145. XMEMCPY(ivec, output + length - sizeof(DES_cblock), sizeof(DES_cblock));
  4146. }
  4147. void CyaSSL_ERR_free_strings(void)
  4148. {
  4149. /* handled internally */
  4150. }
  4151. void CyaSSL_ERR_remove_state(unsigned long state)
  4152. {
  4153. /* TODO: GetErrors().Remove(); */
  4154. (void)state;
  4155. }
  4156. void CyaSSL_EVP_cleanup(void)
  4157. {
  4158. /* nothing to do here */
  4159. }
  4160. void CyaSSL_cleanup_all_ex_data(void)
  4161. {
  4162. /* nothing to do here */
  4163. }
  4164. long CyaSSL_CTX_set_mode(CYASSL_CTX* ctx, long mode)
  4165. {
  4166. /* SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER is CyaSSL default mode */
  4167. CYASSL_ENTER("SSL_CTX_set_mode");
  4168. if (mode == SSL_MODE_ENABLE_PARTIAL_WRITE)
  4169. ctx->partialWrite = 1;
  4170. return mode;
  4171. }
  4172. long CyaSSL_CTX_get_mode(CYASSL_CTX* ctx)
  4173. {
  4174. /* TODO: */
  4175. (void)ctx;
  4176. return 0;
  4177. }
  4178. void CyaSSL_CTX_set_default_read_ahead(CYASSL_CTX* ctx, int m)
  4179. {
  4180. /* TODO: maybe? */
  4181. (void)ctx;
  4182. (void)m;
  4183. }
  4184. int CyaSSL_CTX_set_session_id_context(CYASSL_CTX* ctx,
  4185. const unsigned char* sid_ctx,
  4186. unsigned int sid_ctx_len)
  4187. {
  4188. /* No application specific context needed for cyaSSL */
  4189. (void)ctx;
  4190. (void)sid_ctx;
  4191. (void)sid_ctx_len;
  4192. return SSL_SUCCESS;
  4193. }
  4194. long CyaSSL_CTX_sess_get_cache_size(CYASSL_CTX* ctx)
  4195. {
  4196. /* TODO: maybe? */
  4197. (void)ctx;
  4198. return (~0);
  4199. }
  4200. unsigned long CyaSSL_ERR_get_error_line_data(const char** file, int* line,
  4201. const char** data, int *flags)
  4202. {
  4203. /* Not implemented */
  4204. (void)file;
  4205. (void)line;
  4206. (void)data;
  4207. (void)flags;
  4208. return 0;
  4209. }
  4210. CYASSL_X509* CyaSSL_get_peer_certificate(CYASSL* ssl)
  4211. {
  4212. CYASSL_ENTER("SSL_get_peer_certificate");
  4213. if (ssl->peerCert.issuer.sz)
  4214. return &ssl->peerCert;
  4215. else
  4216. return 0;
  4217. }
  4218. int CyaSSL_set_ex_data(CYASSL* ssl, int idx, void* data)
  4219. {
  4220. #ifdef FORTRESS
  4221. if (ssl != NULL && idx < MAX_EX_DATA)
  4222. {
  4223. ssl->ex_data[idx] = data;
  4224. return SSL_SUCCESS;
  4225. }
  4226. #else
  4227. (void)ssl;
  4228. (void)idx;
  4229. (void)data;
  4230. #endif
  4231. return SSL_FAILURE;
  4232. }
  4233. int CyaSSL_get_shutdown(const CYASSL* ssl)
  4234. {
  4235. (void)ssl;
  4236. return 0;
  4237. }
  4238. int CyaSSL_set_session_id_context(CYASSL* ssl, const unsigned char* id,
  4239. unsigned int len)
  4240. {
  4241. (void)ssl;
  4242. (void)id;
  4243. (void)len;
  4244. return 0;
  4245. }
  4246. void CyaSSL_set_connect_state(CYASSL* ssl)
  4247. {
  4248. (void)ssl;
  4249. /* client by default */
  4250. }
  4251. int CyaSSL_session_reused(CYASSL* ssl)
  4252. {
  4253. return ssl->options.resuming;
  4254. }
  4255. void CyaSSL_SESSION_free(CYASSL_SESSION* session)
  4256. {
  4257. (void)session;
  4258. }
  4259. const char* CyaSSL_get_version(CYASSL* ssl)
  4260. {
  4261. CYASSL_ENTER("SSL_get_version");
  4262. if (ssl->version.major == SSLv3_MAJOR) {
  4263. switch (ssl->version.minor) {
  4264. case SSLv3_MINOR :
  4265. return "SSLv3";
  4266. case TLSv1_MINOR :
  4267. return "TLSv1";
  4268. case TLSv1_1_MINOR :
  4269. return "TLSv1.1";
  4270. case TLSv1_2_MINOR :
  4271. return "TLSv1.2";
  4272. default:
  4273. return "unknown";
  4274. }
  4275. }
  4276. else if (ssl->version.major == DTLS_MAJOR)
  4277. return "DTLS";
  4278. return "unknown";
  4279. }
  4280. CYASSL_CIPHER* CyaSSL_get_current_cipher(CYASSL* ssl)
  4281. {
  4282. CYASSL_ENTER("SSL_get_current_cipher");
  4283. if (ssl)
  4284. return &ssl->cipher;
  4285. else
  4286. return NULL;
  4287. }
  4288. const char* CyaSSL_CIPHER_get_name(const CYASSL_CIPHER* cipher)
  4289. {
  4290. CYASSL_ENTER("SSL_CIPHER_get_name");
  4291. if (cipher) {
  4292. #ifdef HAVE_ECC
  4293. if (cipher->ssl->options.cipherSuite0 == ECC_BYTE) {
  4294. /* ECC suites */
  4295. switch (cipher->ssl->options.cipherSuite) {
  4296. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA :
  4297. return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA";
  4298. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA :
  4299. return "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA";
  4300. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA :
  4301. return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA";
  4302. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA :
  4303. return "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA";
  4304. case TLS_ECDHE_RSA_WITH_RC4_128_SHA :
  4305. return "TLS_ECDHE_RSA_WITH_RC4_128_SHA";
  4306. case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA :
  4307. return "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA";
  4308. case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA :
  4309. return "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA";
  4310. case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA :
  4311. return "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA";
  4312. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA :
  4313. return "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA";
  4314. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA :
  4315. return "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA";
  4316. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA :
  4317. return "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA";
  4318. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA :
  4319. return "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA";
  4320. case TLS_ECDH_RSA_WITH_RC4_128_SHA :
  4321. return "TLS_ECDH_RSA_WITH_RC4_128_SHA";
  4322. case TLS_ECDH_ECDSA_WITH_RC4_128_SHA :
  4323. return "TLS_ECDH_ECDSA_WITH_RC4_128_SHA";
  4324. case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA :
  4325. return "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA";
  4326. case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA :
  4327. return "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA";
  4328. case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 :
  4329. return "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256";
  4330. case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 :
  4331. return "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384";
  4332. case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 :
  4333. return "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256";
  4334. case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 :
  4335. return "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384";
  4336. case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 :
  4337. return "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256";
  4338. case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 :
  4339. return "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384";
  4340. case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 :
  4341. return "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256";
  4342. case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 :
  4343. return "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384";
  4344. case TLS_RSA_WITH_AES_128_CCM_8_SHA256 :
  4345. return "TLS_RSA_WITH_AES_128_CCM_8_SHA256";
  4346. case TLS_RSA_WITH_AES_256_CCM_8_SHA384 :
  4347. return "TLS_RSA_WITH_AES_256_CCM_8_SHA384";
  4348. case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_SHA256 :
  4349. return "TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_SHA256";
  4350. case TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8_SHA384 :
  4351. return "TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8_SHA384";
  4352. default:
  4353. return "NONE";
  4354. }
  4355. }
  4356. #endif
  4357. if (cipher->ssl->options.cipherSuite0 != ECC_BYTE) {
  4358. /* normal suites */
  4359. switch (cipher->ssl->options.cipherSuite) {
  4360. case SSL_RSA_WITH_RC4_128_SHA :
  4361. return "SSL_RSA_WITH_RC4_128_SHA";
  4362. case SSL_RSA_WITH_RC4_128_MD5 :
  4363. return "SSL_RSA_WITH_RC4_128_MD5";
  4364. case SSL_RSA_WITH_3DES_EDE_CBC_SHA :
  4365. return "SSL_RSA_WITH_3DES_EDE_CBC_SHA";
  4366. case TLS_RSA_WITH_AES_128_CBC_SHA :
  4367. return "TLS_RSA_WITH_AES_128_CBC_SHA";
  4368. case TLS_RSA_WITH_AES_256_CBC_SHA :
  4369. return "TLS_RSA_WITH_AES_256_CBC_SHA";
  4370. case TLS_RSA_WITH_AES_128_CBC_SHA256 :
  4371. return "TLS_RSA_WITH_AES_128_CBC_SHA256";
  4372. case TLS_RSA_WITH_AES_256_CBC_SHA256 :
  4373. return "TLS_RSA_WITH_AES_256_CBC_SHA256";
  4374. case TLS_RSA_WITH_NULL_SHA :
  4375. return "TLS_RSA_WITH_NULL_SHA";
  4376. case TLS_RSA_WITH_NULL_SHA256 :
  4377. return "TLS_RSA_WITH_NULL_SHA256";
  4378. case TLS_PSK_WITH_AES_128_CBC_SHA256 :
  4379. return "TLS_PSK_WITH_AES_128_CBC_SHA256";
  4380. case TLS_PSK_WITH_AES_128_CBC_SHA :
  4381. return "TLS_PSK_WITH_AES_128_CBC_SHA";
  4382. case TLS_PSK_WITH_AES_256_CBC_SHA :
  4383. return "TLS_PSK_WITH_AES_256_CBC_SHA";
  4384. case TLS_PSK_WITH_NULL_SHA256 :
  4385. return "TLS_PSK_WITH_NULL_SHA256";
  4386. case TLS_PSK_WITH_NULL_SHA :
  4387. return "TLS_PSK_WITH_NULL_SHA";
  4388. case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 :
  4389. return "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256";
  4390. case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 :
  4391. return "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256";
  4392. case TLS_DHE_RSA_WITH_AES_128_CBC_SHA :
  4393. return "TLS_DHE_RSA_WITH_AES_128_CBC_SHA";
  4394. case TLS_DHE_RSA_WITH_AES_256_CBC_SHA :
  4395. return "TLS_DHE_RSA_WITH_AES_256_CBC_SHA";
  4396. case TLS_RSA_WITH_HC_128_CBC_MD5 :
  4397. return "TLS_RSA_WITH_HC_128_CBC_MD5";
  4398. case TLS_RSA_WITH_HC_128_CBC_SHA :
  4399. return "TLS_RSA_WITH_HC_128_CBC_SHA";
  4400. case TLS_RSA_WITH_RABBIT_CBC_SHA :
  4401. return "TLS_RSA_WITH_RABBIT_CBC_SHA";
  4402. case TLS_NTRU_RSA_WITH_RC4_128_SHA :
  4403. return "TLS_NTRU_RSA_WITH_RC4_128_SHA";
  4404. case TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA :
  4405. return "TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA";
  4406. case TLS_NTRU_RSA_WITH_AES_128_CBC_SHA :
  4407. return "TLS_NTRU_RSA_WITH_AES_128_CBC_SHA";
  4408. case TLS_NTRU_RSA_WITH_AES_256_CBC_SHA :
  4409. return "TLS_NTRU_RSA_WITH_AES_256_CBC_SHA";
  4410. case TLS_RSA_WITH_AES_128_GCM_SHA256 :
  4411. return "TLS_RSA_WITH_AES_128_GCM_SHA256";
  4412. case TLS_RSA_WITH_AES_256_GCM_SHA384 :
  4413. return "TLS_RSA_WITH_AES_256_GCM_SHA384";
  4414. case TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 :
  4415. return "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256";
  4416. case TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 :
  4417. return "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384";
  4418. case TLS_RSA_WITH_CAMELLIA_128_CBC_SHA :
  4419. return "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA";
  4420. case TLS_RSA_WITH_CAMELLIA_256_CBC_SHA :
  4421. return "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA";
  4422. case TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 :
  4423. return "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256";
  4424. case TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 :
  4425. return "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256";
  4426. case TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA :
  4427. return "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA";
  4428. case TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA :
  4429. return "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA";
  4430. case TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 :
  4431. return "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256";
  4432. case TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 :
  4433. return "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256";
  4434. default:
  4435. return "NONE";
  4436. } /* switch */
  4437. } /* normal / ECC */
  4438. }
  4439. return "NONE";
  4440. }
  4441. const char* CyaSSL_get_cipher(CYASSL* ssl)
  4442. {
  4443. CYASSL_ENTER("CyaSSL_get_cipher");
  4444. return CyaSSL_CIPHER_get_name(CyaSSL_get_current_cipher(ssl));
  4445. }
  4446. /* XXX shuld be NO_DH */
  4447. #ifndef NO_CERTS
  4448. /* server ctx Diffie-Hellman parameters */
  4449. int CyaSSL_CTX_SetTmpDH(CYASSL_CTX* ctx, const unsigned char* p, int pSz,
  4450. const unsigned char* g, int gSz)
  4451. {
  4452. CYASSL_ENTER("CyaSSL_CTX_SetTmpDH");
  4453. if (ctx == NULL || p == NULL || g == NULL) return BAD_FUNC_ARG;
  4454. XFREE(ctx->serverDH_P.buffer, ctx->heap, DYNAMIC_TYPE_DH);
  4455. XFREE(ctx->serverDH_G.buffer, ctx->heap, DYNAMIC_TYPE_DH);
  4456. ctx->serverDH_P.buffer = (byte*)XMALLOC(pSz, ctx->heap,DYNAMIC_TYPE_DH);
  4457. if (ctx->serverDH_P.buffer == NULL)
  4458. return MEMORY_E;
  4459. ctx->serverDH_G.buffer = (byte*)XMALLOC(gSz, ctx->heap,DYNAMIC_TYPE_DH);
  4460. if (ctx->serverDH_G.buffer == NULL) {
  4461. XFREE(ctx->serverDH_P.buffer, ctx->heap, DYNAMIC_TYPE_DH);
  4462. return MEMORY_E;
  4463. }
  4464. ctx->serverDH_P.length = pSz;
  4465. ctx->serverDH_G.length = gSz;
  4466. XMEMCPY(ctx->serverDH_P.buffer, p, pSz);
  4467. XMEMCPY(ctx->serverDH_G.buffer, g, gSz);
  4468. ctx->haveDH = 1;
  4469. CYASSL_LEAVE("CyaSSL_CTX_SetTmpDH", 0);
  4470. return 0;
  4471. }
  4472. #endif /* !NO_CERTS */
  4473. char* CyaSSL_CIPHER_description(CYASSL_CIPHER* cipher, char* in, int len)
  4474. {
  4475. (void)cipher;
  4476. (void)in;
  4477. (void)len;
  4478. return 0;
  4479. }
  4480. CYASSL_SESSION* CyaSSL_get1_session(CYASSL* ssl) /* what's ref count */
  4481. {
  4482. (void)ssl;
  4483. return 0;
  4484. }
  4485. void CyaSSL_X509_free(CYASSL_X509* buf)
  4486. {
  4487. (void)buf;
  4488. }
  4489. /* was do nothing */
  4490. /*
  4491. void OPENSSL_free(void* buf)
  4492. {
  4493. (void)buf;
  4494. }
  4495. */
  4496. int CyaSSL_OCSP_parse_url(char* url, char** host, char** port, char** path,
  4497. int* ssl)
  4498. {
  4499. (void)url;
  4500. (void)host;
  4501. (void)port;
  4502. (void)path;
  4503. (void)ssl;
  4504. return 0;
  4505. }
  4506. CYASSL_METHOD* CyaSSLv2_client_method(void)
  4507. {
  4508. return 0;
  4509. }
  4510. CYASSL_METHOD* CyaSSLv2_server_method(void)
  4511. {
  4512. return 0;
  4513. }
  4514. #ifndef NO_MD4
  4515. void CyaSSL_MD4_Init(CYASSL_MD4_CTX* md4)
  4516. {
  4517. /* make sure we have a big enough buffer */
  4518. typedef char ok[sizeof(md4->buffer) >= sizeof(Md4) ? 1 : -1];
  4519. (void) sizeof(ok);
  4520. CYASSL_ENTER("MD4_Init");
  4521. InitMd4((Md4*)md4);
  4522. }
  4523. void CyaSSL_MD4_Update(CYASSL_MD4_CTX* md4, const void* data,
  4524. unsigned long len)
  4525. {
  4526. CYASSL_ENTER("MD4_Update");
  4527. Md4Update((Md4*)md4, (const byte*)data, (word32)len);
  4528. }
  4529. void CyaSSL_MD4_Final(unsigned char* digest, CYASSL_MD4_CTX* md4)
  4530. {
  4531. CYASSL_ENTER("MD4_Final");
  4532. Md4Final((Md4*)md4, digest);
  4533. }
  4534. #endif /* NO_MD4 */
  4535. CYASSL_BIO* CyaSSL_BIO_pop(CYASSL_BIO* top)
  4536. {
  4537. (void)top;
  4538. return 0;
  4539. }
  4540. int CyaSSL_BIO_pending(CYASSL_BIO* bio)
  4541. {
  4542. (void)bio;
  4543. return 0;
  4544. }
  4545. CYASSL_BIO_METHOD* CyaSSL_BIO_s_mem(void)
  4546. {
  4547. static CYASSL_BIO_METHOD meth;
  4548. CYASSL_ENTER("BIO_s_mem");
  4549. meth.type = BIO_MEMORY;
  4550. return &meth;
  4551. }
  4552. CYASSL_BIO_METHOD* CyaSSL_BIO_f_base64(void)
  4553. {
  4554. return 0;
  4555. }
  4556. void CyaSSL_BIO_set_flags(CYASSL_BIO* bio, int flags)
  4557. {
  4558. (void)bio;
  4559. (void)flags;
  4560. }
  4561. void CyaSSL_RAND_screen(void)
  4562. {
  4563. }
  4564. const char* CyaSSL_RAND_file_name(char* fname, unsigned long len)
  4565. {
  4566. (void)fname;
  4567. (void)len;
  4568. return 0;
  4569. }
  4570. int CyaSSL_RAND_write_file(const char* fname)
  4571. {
  4572. (void)fname;
  4573. return 0;
  4574. }
  4575. int CyaSSL_RAND_load_file(const char* fname, long len)
  4576. {
  4577. (void)fname;
  4578. /* CTaoCrypt provides enough entropy internally or will report error */
  4579. if (len == -1)
  4580. return 1024;
  4581. else
  4582. return (int)len;
  4583. }
  4584. int CyaSSL_RAND_egd(const char* path)
  4585. {
  4586. (void)path;
  4587. return 0;
  4588. }
  4589. CYASSL_COMP_METHOD* CyaSSL_COMP_zlib(void)
  4590. {
  4591. return 0;
  4592. }
  4593. CYASSL_COMP_METHOD* CyaSSL_COMP_rle(void)
  4594. {
  4595. return 0;
  4596. }
  4597. int CyaSSL_COMP_add_compression_method(int method, void* data)
  4598. {
  4599. (void)method;
  4600. (void)data;
  4601. return 0;
  4602. }
  4603. int CyaSSL_get_ex_new_index(long idx, void* data, void* cb1, void* cb2,
  4604. void* cb3)
  4605. {
  4606. (void)idx;
  4607. (void)data;
  4608. (void)cb1;
  4609. (void)cb2;
  4610. (void)cb3;
  4611. return 0;
  4612. }
  4613. void CyaSSL_set_dynlock_create_callback(CYASSL_dynlock_value* (*f)(
  4614. const char*, int))
  4615. {
  4616. (void)f;
  4617. }
  4618. void CyaSSL_set_dynlock_lock_callback(
  4619. void (*f)(int, CYASSL_dynlock_value*, const char*, int))
  4620. {
  4621. (void)f;
  4622. }
  4623. void CyaSSL_set_dynlock_destroy_callback(
  4624. void (*f)(CYASSL_dynlock_value*, const char*, int))
  4625. {
  4626. (void)f;
  4627. }
  4628. const char* CyaSSL_X509_verify_cert_error_string(long err)
  4629. {
  4630. (void)err;
  4631. return 0;
  4632. }
  4633. int CyaSSL_X509_LOOKUP_add_dir(CYASSL_X509_LOOKUP* lookup, const char* dir,
  4634. long len)
  4635. {
  4636. (void)lookup;
  4637. (void)dir;
  4638. (void)len;
  4639. return 0;
  4640. }
  4641. int CyaSSL_X509_LOOKUP_load_file(CYASSL_X509_LOOKUP* lookup,
  4642. const char* file, long len)
  4643. {
  4644. (void)lookup;
  4645. (void)file;
  4646. (void)len;
  4647. return 0;
  4648. }
  4649. CYASSL_X509_LOOKUP_METHOD* CyaSSL_X509_LOOKUP_hash_dir(void)
  4650. {
  4651. return 0;
  4652. }
  4653. CYASSL_X509_LOOKUP_METHOD* CyaSSL_X509_LOOKUP_file(void)
  4654. {
  4655. return 0;
  4656. }
  4657. CYASSL_X509_LOOKUP* CyaSSL_X509_STORE_add_lookup(CYASSL_X509_STORE* store,
  4658. CYASSL_X509_LOOKUP_METHOD* m)
  4659. {
  4660. (void)store;
  4661. (void)m;
  4662. return 0;
  4663. }
  4664. CYASSL_X509_STORE* CyaSSL_X509_STORE_new(void)
  4665. {
  4666. return 0;
  4667. }
  4668. int CyaSSL_X509_STORE_get_by_subject(CYASSL_X509_STORE_CTX* ctx, int idx,
  4669. CYASSL_X509_NAME* name, CYASSL_X509_OBJECT* obj)
  4670. {
  4671. (void)ctx;
  4672. (void)idx;
  4673. (void)name;
  4674. (void)obj;
  4675. return 0;
  4676. }
  4677. int CyaSSL_X509_STORE_CTX_init(CYASSL_X509_STORE_CTX* ctx,
  4678. CYASSL_X509_STORE* store, CYASSL_X509* x509, STACK_OF(CYASSL_X509)* sk)
  4679. {
  4680. (void)ctx;
  4681. (void)store;
  4682. (void)x509;
  4683. (void)sk;
  4684. return 0;
  4685. }
  4686. void CyaSSL_X509_STORE_CTX_cleanup(CYASSL_X509_STORE_CTX* ctx)
  4687. {
  4688. (void)ctx;
  4689. }
  4690. CYASSL_ASN1_TIME* CyaSSL_X509_CRL_get_lastUpdate(CYASSL_X509_CRL* crl)
  4691. {
  4692. (void)crl;
  4693. return 0;
  4694. }
  4695. CYASSL_ASN1_TIME* CyaSSL_X509_CRL_get_nextUpdate(CYASSL_X509_CRL* crl)
  4696. {
  4697. (void)crl;
  4698. return 0;
  4699. }
  4700. CYASSL_EVP_PKEY* CyaSSL_X509_get_pubkey(CYASSL_X509* x509)
  4701. {
  4702. (void)x509;
  4703. return 0;
  4704. }
  4705. int CyaSSL_X509_CRL_verify(CYASSL_X509_CRL* crl, CYASSL_EVP_PKEY* key)
  4706. {
  4707. (void)crl;
  4708. (void)key;
  4709. return 0;
  4710. }
  4711. void CyaSSL_X509_STORE_CTX_set_error(CYASSL_X509_STORE_CTX* ctx, int err)
  4712. {
  4713. (void)ctx;
  4714. (void)err;
  4715. }
  4716. void CyaSSL_X509_OBJECT_free_contents(CYASSL_X509_OBJECT* obj)
  4717. {
  4718. (void)obj;
  4719. }
  4720. void CyaSSL_EVP_PKEY_free(CYASSL_EVP_PKEY* key)
  4721. {
  4722. (void)key;
  4723. }
  4724. int CyaSSL_X509_cmp_current_time(const CYASSL_ASN1_TIME* asnTime)
  4725. {
  4726. (void)asnTime;
  4727. return 0;
  4728. }
  4729. int CyaSSL_sk_X509_REVOKED_num(CYASSL_X509_REVOKED* revoked)
  4730. {
  4731. (void)revoked;
  4732. return 0;
  4733. }
  4734. CYASSL_X509_REVOKED* CyaSSL_X509_CRL_get_REVOKED(CYASSL_X509_CRL* crl)
  4735. {
  4736. (void)crl;
  4737. return 0;
  4738. }
  4739. CYASSL_X509_REVOKED* CyaSSL_sk_X509_REVOKED_value(
  4740. CYASSL_X509_REVOKED* revoked, int value)
  4741. {
  4742. (void)revoked;
  4743. (void)value;
  4744. return 0;
  4745. }
  4746. CYASSL_ASN1_INTEGER* CyaSSL_X509_get_serialNumber(CYASSL_X509* x509)
  4747. {
  4748. (void)x509;
  4749. return 0;
  4750. }
  4751. int CyaSSL_ASN1_TIME_print(CYASSL_BIO* bio, const CYASSL_ASN1_TIME* asnTime)
  4752. {
  4753. (void)bio;
  4754. (void)asnTime;
  4755. return 0;
  4756. }
  4757. int CyaSSL_ASN1_INTEGER_cmp(const CYASSL_ASN1_INTEGER* a,
  4758. const CYASSL_ASN1_INTEGER* b)
  4759. {
  4760. (void)a;
  4761. (void)b;
  4762. return 0;
  4763. }
  4764. long CyaSSL_ASN1_INTEGER_get(const CYASSL_ASN1_INTEGER* i)
  4765. {
  4766. (void)i;
  4767. return 0;
  4768. }
  4769. void* CyaSSL_X509_STORE_CTX_get_ex_data(CYASSL_X509_STORE_CTX* ctx, int idx)
  4770. {
  4771. #ifdef FORTRESS
  4772. if (ctx != NULL && idx == 0)
  4773. return ctx->ex_data;
  4774. #else
  4775. (void)ctx;
  4776. (void)idx;
  4777. #endif
  4778. return 0;
  4779. }
  4780. int CyaSSL_get_ex_data_X509_STORE_CTX_idx(void)
  4781. {
  4782. return 0;
  4783. }
  4784. void* CyaSSL_get_ex_data(const CYASSL* ssl, int idx)
  4785. {
  4786. #ifdef FORTRESS
  4787. if (ssl != NULL && idx < MAX_EX_DATA)
  4788. return ssl->ex_data[idx];
  4789. #else
  4790. (void)ssl;
  4791. (void)idx;
  4792. #endif
  4793. return 0;
  4794. }
  4795. void CyaSSL_CTX_set_info_callback(CYASSL_CTX* ctx, void (*f)(void))
  4796. {
  4797. (void)ctx;
  4798. (void)f;
  4799. }
  4800. unsigned long CyaSSL_ERR_peek_error(void)
  4801. {
  4802. return 0;
  4803. }
  4804. int CyaSSL_ERR_GET_REASON(int err)
  4805. {
  4806. (void)err;
  4807. return 0;
  4808. }
  4809. char* CyaSSL_alert_type_string_long(int alertID)
  4810. {
  4811. (void)alertID;
  4812. return 0;
  4813. }
  4814. char* CyaSSL_alert_desc_string_long(int alertID)
  4815. {
  4816. (void)alertID;
  4817. return 0;
  4818. }
  4819. char* CyaSSL_state_string_long(CYASSL* ssl)
  4820. {
  4821. (void)ssl;
  4822. return 0;
  4823. }
  4824. int CyaSSL_PEM_def_callback(char* name, int num, int w, void* key)
  4825. {
  4826. (void)name;
  4827. (void)num;
  4828. (void)w;
  4829. (void)key;
  4830. return 0;
  4831. }
  4832. long CyaSSL_CTX_sess_accept(CYASSL_CTX* ctx)
  4833. {
  4834. (void)ctx;
  4835. return 0;
  4836. }
  4837. long CyaSSL_CTX_sess_connect(CYASSL_CTX* ctx)
  4838. {
  4839. (void)ctx;
  4840. return 0;
  4841. }
  4842. long CyaSSL_CTX_sess_accept_good(CYASSL_CTX* ctx)
  4843. {
  4844. (void)ctx;
  4845. return 0;
  4846. }
  4847. long CyaSSL_CTX_sess_connect_good(CYASSL_CTX* ctx)
  4848. {
  4849. (void)ctx;
  4850. return 0;
  4851. }
  4852. long CyaSSL_CTX_sess_accept_renegotiate(CYASSL_CTX* ctx)
  4853. {
  4854. (void)ctx;
  4855. return 0;
  4856. }
  4857. long CyaSSL_CTX_sess_connect_renegotiate(CYASSL_CTX* ctx)
  4858. {
  4859. (void)ctx;
  4860. return 0;
  4861. }
  4862. long CyaSSL_CTX_sess_hits(CYASSL_CTX* ctx)
  4863. {
  4864. (void)ctx;
  4865. return 0;
  4866. }
  4867. long CyaSSL_CTX_sess_cb_hits(CYASSL_CTX* ctx)
  4868. {
  4869. (void)ctx;
  4870. return 0;
  4871. }
  4872. long CyaSSL_CTX_sess_cache_full(CYASSL_CTX* ctx)
  4873. {
  4874. (void)ctx;
  4875. return 0;
  4876. }
  4877. long CyaSSL_CTX_sess_misses(CYASSL_CTX* ctx)
  4878. {
  4879. (void)ctx;
  4880. return 0;
  4881. }
  4882. long CyaSSL_CTX_sess_timeouts(CYASSL_CTX* ctx)
  4883. {
  4884. (void)ctx;
  4885. return 0;
  4886. }
  4887. long CyaSSL_CTX_sess_number(CYASSL_CTX* ctx)
  4888. {
  4889. (void)ctx;
  4890. return 0;
  4891. }
  4892. void CyaSSL_DES_set_key_unchecked(CYASSL_const_DES_cblock* myDes,
  4893. CYASSL_DES_key_schedule* key)
  4894. {
  4895. (void)myDes;
  4896. (void)key;
  4897. }
  4898. void CyaSSL_DES_set_odd_parity(CYASSL_DES_cblock* myDes)
  4899. {
  4900. (void)myDes;
  4901. }
  4902. void CyaSSL_DES_ecb_encrypt(CYASSL_DES_cblock* desa,
  4903. CYASSL_DES_cblock* desb, CYASSL_DES_key_schedule* key, int len)
  4904. {
  4905. (void)desa;
  4906. (void)desb;
  4907. (void)key;
  4908. (void)len;
  4909. }
  4910. int CyaSSL_BIO_printf(CYASSL_BIO* bio, const char* format, ...)
  4911. {
  4912. (void)bio;
  4913. (void)format;
  4914. return 0;
  4915. }
  4916. int CyaSSL_ASN1_UTCTIME_print(CYASSL_BIO* bio, const CYASSL_ASN1_UTCTIME* a)
  4917. {
  4918. (void)bio;
  4919. (void)a;
  4920. return 0;
  4921. }
  4922. int CyaSSL_sk_num(CYASSL_X509_REVOKED* rev)
  4923. {
  4924. (void)rev;
  4925. return 0;
  4926. }
  4927. void* CyaSSL_sk_value(CYASSL_X509_REVOKED* rev, int i)
  4928. {
  4929. (void)rev;
  4930. (void)i;
  4931. return 0;
  4932. }
  4933. /* stunnel 4.28 needs */
  4934. void* CyaSSL_CTX_get_ex_data(const CYASSL_CTX* ctx, int d)
  4935. {
  4936. (void)ctx;
  4937. (void)d;
  4938. return 0;
  4939. }
  4940. int CyaSSL_CTX_set_ex_data(CYASSL_CTX* ctx, int d, void* p)
  4941. {
  4942. (void)ctx;
  4943. (void)d;
  4944. (void)p;
  4945. return SSL_SUCCESS;
  4946. }
  4947. void CyaSSL_CTX_sess_set_get_cb(CYASSL_CTX* ctx,
  4948. CYASSL_SESSION*(*f)(CYASSL*, unsigned char*, int, int*))
  4949. {
  4950. (void)ctx;
  4951. (void)f;
  4952. }
  4953. void CyaSSL_CTX_sess_set_new_cb(CYASSL_CTX* ctx,
  4954. int (*f)(CYASSL*, CYASSL_SESSION*))
  4955. {
  4956. (void)ctx;
  4957. (void)f;
  4958. }
  4959. void CyaSSL_CTX_sess_set_remove_cb(CYASSL_CTX* ctx, void (*f)(CYASSL_CTX*,
  4960. CYASSL_SESSION*))
  4961. {
  4962. (void)ctx;
  4963. (void)f;
  4964. }
  4965. int CyaSSL_i2d_SSL_SESSION(CYASSL_SESSION* sess, unsigned char** p)
  4966. {
  4967. (void)sess;
  4968. (void)p;
  4969. return sizeof(CYASSL_SESSION);
  4970. }
  4971. CYASSL_SESSION* CyaSSL_d2i_SSL_SESSION(CYASSL_SESSION** sess,
  4972. const unsigned char** p, long i)
  4973. {
  4974. (void)p;
  4975. (void)i;
  4976. if (sess)
  4977. return *sess;
  4978. return NULL;
  4979. }
  4980. long CyaSSL_SESSION_get_timeout(const CYASSL_SESSION* sess)
  4981. {
  4982. CYASSL_ENTER("CyaSSL_SESSION_get_timeout");
  4983. return sess->timeout;
  4984. }
  4985. long CyaSSL_SESSION_get_time(const CYASSL_SESSION* sess)
  4986. {
  4987. CYASSL_ENTER("CyaSSL_SESSION_get_time");
  4988. return sess->bornOn;
  4989. }
  4990. int CyaSSL_CTX_get_ex_new_index(long idx, void* arg, void* a, void* b,
  4991. void* c)
  4992. {
  4993. (void)idx;
  4994. (void)arg;
  4995. (void)a;
  4996. (void)b;
  4997. (void)c;
  4998. return 0;
  4999. }
  5000. /* write X509 serial number in unsigned binary to buffer
  5001. buffer needs to be at least EXTERNAL_SERIAL_SIZE (32) for all cases
  5002. return 0 on success */
  5003. int CyaSSL_X509_get_serial_number(CYASSL_X509* x509, byte* in, int* inOutSz)
  5004. {
  5005. CYASSL_ENTER("CyaSSL_X509_get_serial_number");
  5006. if (x509 == NULL || in == NULL || *inOutSz < x509->serialSz)
  5007. return BAD_FUNC_ARG;
  5008. XMEMCPY(in, x509->serial, x509->serialSz);
  5009. *inOutSz = x509->serialSz;
  5010. return 0;
  5011. }
  5012. const byte* CyaSSL_X509_get_der(CYASSL_X509* x509, int* outSz)
  5013. {
  5014. CYASSL_ENTER("CyaSSL_X509_get_der");
  5015. if (x509 == NULL || outSz == NULL)
  5016. return NULL;
  5017. *outSz = (int)x509->derCert.length;
  5018. return x509->derCert.buffer;
  5019. }
  5020. char* CyaSSL_X509_get_subjectCN(CYASSL_X509* x509)
  5021. {
  5022. if (x509 == NULL)
  5023. return NULL;
  5024. return x509->subjectCN;
  5025. }
  5026. #ifdef FORTRESS
  5027. int CyaSSL_cmp_peer_cert_to_file(CYASSL* ssl, const char *fname)
  5028. {
  5029. int ret = -1;
  5030. CYASSL_ENTER("CyaSSL_cmp_peer_cert_to_file");
  5031. if (ssl != NULL && fname != NULL)
  5032. {
  5033. XFILE file = XBADFILE;
  5034. long sz = 0;
  5035. byte staticBuffer[FILE_BUFFER_SIZE];
  5036. byte* myBuffer = staticBuffer;
  5037. CYASSL_CTX* ctx = ssl->ctx;
  5038. EncryptedInfo info;
  5039. buffer fileDer;
  5040. int eccKey = 0;
  5041. CYASSL_X509* peer_cert = &ssl->peerCert;
  5042. info.set = 0;
  5043. info.ctx = ctx;
  5044. info.consumed = 0;
  5045. fileDer.buffer = 0;
  5046. file = XFOPEN(fname, "rb");
  5047. if (file == XBADFILE) return SSL_BAD_FILE;
  5048. XFSEEK(file, 0, XSEEK_END);
  5049. sz = XFTELL(file);
  5050. XREWIND(file);
  5051. if (sz > (long)sizeof(staticBuffer)) {
  5052. CYASSL_MSG("Getting dynamic buffer");
  5053. myBuffer = (byte*) XMALLOC(sz, ctx->heap, DYNAMIC_TYPE_FILE);
  5054. }
  5055. if ((myBuffer != NULL) &&
  5056. (XFREAD(myBuffer, sz, 1, file) > 0) &&
  5057. (PemToDer(myBuffer, sz, CERT_TYPE,
  5058. &fileDer, ctx->heap, &info, &eccKey) == 0) &&
  5059. (fileDer.length != 0) &&
  5060. (fileDer.length == peer_cert->derCert.length) &&
  5061. (XMEMCMP(peer_cert->derCert.buffer, fileDer.buffer,
  5062. fileDer.length) == 0))
  5063. {
  5064. ret = 0;
  5065. }
  5066. XFCLOSE(file);
  5067. if (fileDer.buffer)
  5068. XFREE(fileDer.buffer, ctx->heap, DYNAMIC_TYPE_CERT);
  5069. if (myBuffer && (myBuffer != staticBuffer))
  5070. XFREE(myBuffer, ctx->heap, DYNAMIC_TYPE_FILE);
  5071. }
  5072. return ret;
  5073. }
  5074. #else
  5075. int CyaSSL_cmp_peer_cert_to_file(CYASSL* ssl, const char *fname)
  5076. {
  5077. (void)ssl;
  5078. (void)fname;
  5079. return -1;
  5080. }
  5081. #endif
  5082. static RNG globalRNG;
  5083. static int initGlobalRNG = 0;
  5084. int CyaSSL_RAND_seed(const void* seed, int len)
  5085. {
  5086. CYASSL_MSG("CyaSSL_RAND_seed");
  5087. (void)seed;
  5088. (void)len;
  5089. if (initGlobalRNG == 0) {
  5090. if (InitRng(&globalRNG) < 0) {
  5091. CYASSL_MSG("CyaSSL Init Global RNG failed");
  5092. }
  5093. initGlobalRNG = 1;
  5094. }
  5095. return 0;
  5096. }
  5097. int CyaSSL_RAND_bytes(unsigned char* buf, int num)
  5098. {
  5099. RNG tmpRNG;
  5100. RNG* rng = &tmpRNG;
  5101. CYASSL_ENTER("RAND_bytes");
  5102. if (InitRng(&tmpRNG) != 0) {
  5103. CYASSL_MSG("Bad RNG Init, trying global");
  5104. if (initGlobalRNG == 0) {
  5105. CYASSL_MSG("Global RNG no Init");
  5106. return 0;
  5107. }
  5108. rng = &globalRNG;
  5109. }
  5110. RNG_GenerateBlock(rng, buf, num);
  5111. return 1;
  5112. }
  5113. CYASSL_BN_CTX* CyaSSL_BN_CTX_new(void)
  5114. {
  5115. static int ctx; /* ctaocrypt doesn't now need ctx */
  5116. CYASSL_MSG("CyaSSL_BN_CTX_new");
  5117. return (CYASSL_BN_CTX*)&ctx;
  5118. }
  5119. void CyaSSL_BN_CTX_init(CYASSL_BN_CTX* ctx)
  5120. {
  5121. (void)ctx;
  5122. CYASSL_MSG("CyaSSL_BN_CTX_init");
  5123. }
  5124. void CyaSSL_BN_CTX_free(CYASSL_BN_CTX* ctx)
  5125. {
  5126. (void)ctx;
  5127. CYASSL_MSG("CyaSSL_BN_CTX_free");
  5128. /* do free since static ctx that does nothing */
  5129. }
  5130. static void InitCyaSSL_BigNum(CYASSL_BIGNUM* bn)
  5131. {
  5132. CYASSL_MSG("InitCyaSSL_BigNum");
  5133. if (bn) {
  5134. bn->neg = 0;
  5135. bn->internal = NULL;
  5136. }
  5137. }
  5138. CYASSL_BIGNUM* CyaSSL_BN_new(void)
  5139. {
  5140. CYASSL_BIGNUM* external;
  5141. mp_int* mpi;
  5142. CYASSL_MSG("CyaSSL_BN_new");
  5143. mpi = (mp_int*) XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  5144. if (mpi == NULL) {
  5145. CYASSL_MSG("CyaSSL_BN_new malloc mpi failure");
  5146. return NULL;
  5147. }
  5148. external = (CYASSL_BIGNUM*) XMALLOC(sizeof(CYASSL_BIGNUM), NULL,
  5149. DYNAMIC_TYPE_BIGINT);
  5150. if (external == NULL) {
  5151. CYASSL_MSG("CyaSSL_BN_new malloc CYASSL_BIGNUM failure");
  5152. XFREE(mpi, NULL, DYNAMIC_TYPE_BIGINT);
  5153. return NULL;
  5154. }
  5155. InitCyaSSL_BigNum(external);
  5156. external->internal = mpi;
  5157. if (mp_init(mpi) != MP_OKAY) {
  5158. CyaSSL_BN_free(external);
  5159. return NULL;
  5160. }
  5161. return external;
  5162. }
  5163. void CyaSSL_BN_free(CYASSL_BIGNUM* bn)
  5164. {
  5165. CYASSL_MSG("CyaSSL_BN_free");
  5166. if (bn) {
  5167. if (bn->internal) {
  5168. mp_clear((mp_int*)bn->internal);
  5169. XFREE(bn->internal, NULL, DYNAMIC_TYPE_BIGINT);
  5170. bn->internal = NULL;
  5171. }
  5172. XFREE(bn, NULL, DYNAMIC_TYPE_BIGINT);
  5173. }
  5174. }
  5175. void CyaSSL_BN_clear_free(CYASSL_BIGNUM* bn)
  5176. {
  5177. CYASSL_MSG("CyaSSL_BN_clear_free");
  5178. CyaSSL_BN_free(bn);
  5179. }
  5180. int CyaSSL_BN_sub(CYASSL_BIGNUM* r, const CYASSL_BIGNUM* a,
  5181. const CYASSL_BIGNUM* b)
  5182. {
  5183. CYASSL_MSG("CyaSSL_BN_sub");
  5184. if (r == NULL || a == NULL || b == NULL)
  5185. return 0;
  5186. if (mp_sub((mp_int*)a->internal,(mp_int*)b->internal,
  5187. (mp_int*)r->internal) == MP_OKAY)
  5188. return 1;
  5189. CYASSL_MSG("CyaSSL_BN_sub mp_sub failed");
  5190. return 0;
  5191. }
  5192. int CyaSSL_BN_mod(CYASSL_BIGNUM* r, const CYASSL_BIGNUM* a,
  5193. const CYASSL_BIGNUM* b, const CYASSL_BN_CTX* c)
  5194. {
  5195. (void)c;
  5196. CYASSL_MSG("CyaSSL_BN_mod");
  5197. if (r == NULL || a == NULL || b == NULL)
  5198. return 0;
  5199. if (mp_mod((mp_int*)a->internal,(mp_int*)b->internal,
  5200. (mp_int*)r->internal) == MP_OKAY)
  5201. return 1;
  5202. CYASSL_MSG("CyaSSL_BN_mod mp_mod failed");
  5203. return 0;
  5204. }
  5205. const CYASSL_BIGNUM* CyaSSL_BN_value_one(void)
  5206. {
  5207. static CYASSL_BIGNUM* bn_one = NULL;
  5208. CYASSL_MSG("CyaSSL_BN_value_one");
  5209. if (bn_one == NULL) {
  5210. bn_one = CyaSSL_BN_new();
  5211. if (bn_one)
  5212. mp_set_int((mp_int*)bn_one->internal, 1);
  5213. }
  5214. return bn_one;
  5215. }
  5216. int CyaSSL_BN_num_bytes(const CYASSL_BIGNUM* bn)
  5217. {
  5218. CYASSL_MSG("CyaSSL_BN_num_bytes");
  5219. if (bn == NULL || bn->internal == NULL)
  5220. return 0;
  5221. return mp_unsigned_bin_size((mp_int*)bn->internal);
  5222. }
  5223. int CyaSSL_BN_num_bits(const CYASSL_BIGNUM* bn)
  5224. {
  5225. CYASSL_MSG("CyaSSL_BN_num_bits");
  5226. if (bn == NULL || bn->internal == NULL)
  5227. return 0;
  5228. return mp_count_bits((mp_int*)bn->internal);
  5229. }
  5230. int CyaSSL_BN_is_zero(const CYASSL_BIGNUM* bn)
  5231. {
  5232. CYASSL_MSG("CyaSSL_BN_is_zero");
  5233. if (bn == NULL || bn->internal == NULL)
  5234. return 0;
  5235. return mp_iszero((mp_int*)bn->internal);
  5236. }
  5237. int CyaSSL_BN_is_one(const CYASSL_BIGNUM* bn)
  5238. {
  5239. CYASSL_MSG("CyaSSL_BN_is_one");
  5240. if (bn == NULL || bn->internal == NULL)
  5241. return 0;
  5242. if (mp_cmp_d((mp_int*)bn->internal, 1) == 0)
  5243. return 1;
  5244. return 0;
  5245. }
  5246. int CyaSSL_BN_is_odd(const CYASSL_BIGNUM* bn)
  5247. {
  5248. CYASSL_MSG("CyaSSL_BN_is_odd");
  5249. if (bn == NULL || bn->internal == NULL)
  5250. return 0;
  5251. return mp_isodd((mp_int*)bn->internal);
  5252. }
  5253. int CyaSSL_BN_cmp(const CYASSL_BIGNUM* a, const CYASSL_BIGNUM* b)
  5254. {
  5255. CYASSL_MSG("CyaSSL_BN_cmp");
  5256. if (a == NULL || a->internal == NULL || b == NULL || b->internal ==NULL)
  5257. return 0;
  5258. return mp_cmp((mp_int*)a->internal, (mp_int*)b->internal);
  5259. }
  5260. int CyaSSL_BN_bn2bin(const CYASSL_BIGNUM* bn, unsigned char* r)
  5261. {
  5262. CYASSL_MSG("CyaSSL_BN_bn2bin");
  5263. if (bn == NULL || bn->internal == NULL) {
  5264. CYASSL_MSG("NULL bn error");
  5265. return -1;
  5266. }
  5267. if (r == NULL)
  5268. return mp_unsigned_bin_size((mp_int*)bn->internal);
  5269. if (mp_to_unsigned_bin((mp_int*)bn->internal, r) != MP_OKAY) {
  5270. CYASSL_MSG("mp_to_unsigned_bin error");
  5271. return -1;
  5272. }
  5273. return mp_unsigned_bin_size((mp_int*)bn->internal);
  5274. }
  5275. CYASSL_BIGNUM* CyaSSL_BN_bin2bn(const unsigned char* str, int len,
  5276. CYASSL_BIGNUM* ret)
  5277. {
  5278. CYASSL_MSG("CyaSSL_BN_bin2bn");
  5279. if (ret && ret->internal) {
  5280. if (mp_read_unsigned_bin((mp_int*)ret->internal, str, len) != 0) {
  5281. CYASSL_MSG("mp_read_unsigned_bin failure");
  5282. return NULL;
  5283. }
  5284. }
  5285. else {
  5286. CYASSL_MSG("CyaSSL_BN_bin2bn wants return bignum");
  5287. }
  5288. return ret;
  5289. }
  5290. int CyaSSL_mask_bits(CYASSL_BIGNUM* bn, int n)
  5291. {
  5292. (void)bn;
  5293. (void)n;
  5294. CYASSL_MSG("CyaSSL_BN_mask_bits");
  5295. return -1;
  5296. }
  5297. int CyaSSL_BN_rand(CYASSL_BIGNUM* bn, int bits, int top, int bottom)
  5298. {
  5299. byte buff[1024];
  5300. RNG tmpRNG;
  5301. RNG* rng = &tmpRNG;
  5302. int ret;
  5303. int len = bits/8;
  5304. (void)top;
  5305. (void)bottom;
  5306. CYASSL_MSG("CyaSSL_BN_rand");
  5307. if (bn == NULL || bn->internal == NULL) {
  5308. CYASSL_MSG("Bad function arguments");
  5309. return 0;
  5310. }
  5311. if (bits % 8)
  5312. len++;
  5313. if ( (ret = InitRng(&tmpRNG)) != 0) {
  5314. CYASSL_MSG("Bad RNG Init, trying global");
  5315. if (initGlobalRNG == 0) {
  5316. CYASSL_MSG("Global RNG no Init");
  5317. return 0;
  5318. }
  5319. rng = &globalRNG;
  5320. }
  5321. RNG_GenerateBlock(rng, buff, len);
  5322. buff[0] |= 0x80 | 0x40;
  5323. buff[len-1] |= 0x01;
  5324. if (mp_read_unsigned_bin((mp_int*)bn->internal,buff,len) != MP_OKAY) {
  5325. CYASSL_MSG("mp read bin failed");
  5326. return 0;
  5327. }
  5328. return 1;
  5329. }
  5330. int CyaSSL_BN_is_bit_set(const CYASSL_BIGNUM* bn, int n)
  5331. {
  5332. (void)bn;
  5333. (void)n;
  5334. CYASSL_MSG("CyaSSL_BN_is_bit_set");
  5335. return 0;
  5336. }
  5337. int CyaSSL_BN_hex2bn(CYASSL_BIGNUM** bn, const char* str)
  5338. {
  5339. byte decoded[1024];
  5340. word32 decSz = sizeof(decoded);
  5341. CYASSL_MSG("CyaSSL_BN_hex2bn");
  5342. if (str == NULL) {
  5343. CYASSL_MSG("Bad function argument");
  5344. return 0;
  5345. }
  5346. if (Base16_Decode((byte*)str, (int)XSTRLEN(str), decoded, &decSz) < 0) {
  5347. CYASSL_MSG("Bad Base16_Decode error");
  5348. return 0;
  5349. }
  5350. if (bn == NULL)
  5351. return decSz;
  5352. if (*bn == NULL) {
  5353. *bn = CyaSSL_BN_new();
  5354. if (*bn == NULL) {
  5355. CYASSL_MSG("BN new failed");
  5356. return 0;
  5357. }
  5358. }
  5359. if (CyaSSL_BN_bin2bn(decoded, decSz, *bn) == NULL) {
  5360. CYASSL_MSG("Bad bin2bn error");
  5361. return 0;
  5362. }
  5363. return 1; /* success */
  5364. }
  5365. CYASSL_BIGNUM* CyaSSL_BN_dup(const CYASSL_BIGNUM* bn)
  5366. {
  5367. CYASSL_BIGNUM* ret;
  5368. CYASSL_MSG("CyaSSL_BN_dup");
  5369. if (bn == NULL || bn->internal == NULL) {
  5370. CYASSL_MSG("bn NULL error");
  5371. return NULL;
  5372. }
  5373. ret = CyaSSL_BN_new();
  5374. if (ret == NULL) {
  5375. CYASSL_MSG("bn new error");
  5376. return NULL;
  5377. }
  5378. if (mp_copy((mp_int*)bn->internal, (mp_int*)ret->internal) != MP_OKAY) {
  5379. CYASSL_MSG("mp_copy error");
  5380. CyaSSL_BN_free(ret);
  5381. return NULL;
  5382. }
  5383. return ret;
  5384. }
  5385. CYASSL_BIGNUM* CyaSSL_BN_copy(CYASSL_BIGNUM* r, const CYASSL_BIGNUM* bn)
  5386. {
  5387. (void)r;
  5388. (void)bn;
  5389. CYASSL_MSG("CyaSSL_BN_copy");
  5390. return NULL;
  5391. }
  5392. int CyaSSL_BN_set_word(CYASSL_BIGNUM* bn, unsigned long w)
  5393. {
  5394. (void)bn;
  5395. (void)w;
  5396. CYASSL_MSG("CyaSSL_BN_set_word");
  5397. return -1;
  5398. }
  5399. int CyaSSL_BN_dec2bn(CYASSL_BIGNUM** bn, const char* str)
  5400. {
  5401. (void)bn;
  5402. (void)str;
  5403. CYASSL_MSG("CyaSSL_BN_dec2bn");
  5404. return -1;
  5405. }
  5406. char* CyaSSL_BN_bn2dec(const CYASSL_BIGNUM* bn)
  5407. {
  5408. (void)bn;
  5409. CYASSL_MSG("CyaSSL_BN_bn2dec");
  5410. return NULL;
  5411. }
  5412. static void InitCyaSSL_DH(CYASSL_DH* dh)
  5413. {
  5414. if (dh) {
  5415. dh->p = NULL;
  5416. dh->g = NULL;
  5417. dh->pub_key = NULL;
  5418. dh->priv_key = NULL;
  5419. dh->internal = NULL;
  5420. dh->inSet = 0;
  5421. dh->exSet = 0;
  5422. }
  5423. }
  5424. CYASSL_DH* CyaSSL_DH_new(void)
  5425. {
  5426. CYASSL_DH* external;
  5427. DhKey* key;
  5428. CYASSL_MSG("CyaSSL_DH_new");
  5429. key = (DhKey*) XMALLOC(sizeof(DhKey), NULL, DYNAMIC_TYPE_DH);
  5430. if (key == NULL) {
  5431. CYASSL_MSG("CyaSSL_DH_new malloc DhKey failure");
  5432. return NULL;
  5433. }
  5434. external = (CYASSL_DH*) XMALLOC(sizeof(CYASSL_DH), NULL,
  5435. DYNAMIC_TYPE_DH);
  5436. if (external == NULL) {
  5437. CYASSL_MSG("CyaSSL_DH_new malloc CYASSL_DH failure");
  5438. XFREE(key, NULL, DYNAMIC_TYPE_DH);
  5439. return NULL;
  5440. }
  5441. InitCyaSSL_DH(external);
  5442. InitDhKey(key);
  5443. external->internal = key;
  5444. return external;
  5445. }
  5446. void CyaSSL_DH_free(CYASSL_DH* dh)
  5447. {
  5448. CYASSL_MSG("CyaSSL_DH_free");
  5449. if (dh) {
  5450. if (dh->internal) {
  5451. FreeDhKey((DhKey*)dh->internal);
  5452. XFREE(dh->internal, NULL, DYNAMIC_TYPE_DH);
  5453. dh->internal = NULL;
  5454. }
  5455. CyaSSL_BN_free(dh->priv_key);
  5456. CyaSSL_BN_free(dh->pub_key);
  5457. CyaSSL_BN_free(dh->g);
  5458. CyaSSL_BN_free(dh->p);
  5459. InitCyaSSL_DH(dh); /* set back to NULLs for safety */
  5460. XFREE(dh, NULL, DYNAMIC_TYPE_DH);
  5461. }
  5462. }
  5463. static int SetDhInternal(CYASSL_DH* dh)
  5464. {
  5465. unsigned char p[1024];
  5466. unsigned char g[1024];
  5467. int pSz = sizeof(p);
  5468. int gSz = sizeof(g);
  5469. CYASSL_ENTER("SetDhInternal");
  5470. if (dh == NULL || dh->p == NULL || dh->g == NULL) {
  5471. CYASSL_MSG("Bad function arguments");
  5472. return -1;
  5473. }
  5474. if (CyaSSL_BN_bn2bin(dh->p, NULL) > pSz) {
  5475. CYASSL_MSG("Bad p internal size");
  5476. return -1;
  5477. }
  5478. if (CyaSSL_BN_bn2bin(dh->g, NULL) > gSz) {
  5479. CYASSL_MSG("Bad g internal size");
  5480. return -1;
  5481. }
  5482. pSz = CyaSSL_BN_bn2bin(dh->p, p);
  5483. gSz = CyaSSL_BN_bn2bin(dh->g, g);
  5484. if (pSz <= 0 || gSz <= 0) {
  5485. CYASSL_MSG("Bad BN2bin set");
  5486. return -1;
  5487. }
  5488. if (DhSetKey((DhKey*)dh->internal, p, pSz, g, gSz) < 0) {
  5489. CYASSL_MSG("Bad DH SetKey");
  5490. return -1;
  5491. }
  5492. dh->inSet = 1;
  5493. return 0;
  5494. }
  5495. int CyaSSL_DH_size(CYASSL_DH* dh)
  5496. {
  5497. CYASSL_MSG("CyaSSL_DH_size");
  5498. if (dh == NULL)
  5499. return 0;
  5500. return CyaSSL_BN_num_bytes(dh->p);
  5501. }
  5502. /* return 1 on success else 0 */
  5503. int CyaSSL_DH_generate_key(CYASSL_DH* dh)
  5504. {
  5505. unsigned char pub [768];
  5506. unsigned char priv[768];
  5507. word32 pubSz = sizeof(pub);
  5508. word32 privSz = sizeof(priv);
  5509. RNG tmpRNG;
  5510. RNG* rng = &tmpRNG;
  5511. int ret;
  5512. CYASSL_MSG("CyaSSL_DH_generate_key");
  5513. if (dh == NULL || dh->p == NULL || dh->g == NULL) {
  5514. CYASSL_MSG("Bad function arguments");
  5515. return 0;
  5516. }
  5517. if (dh->inSet == 0) {
  5518. if (SetDhInternal(dh) < 0) {
  5519. CYASSL_MSG("Bad DH set internal");
  5520. return 0;
  5521. }
  5522. }
  5523. if ( (ret = InitRng(&tmpRNG)) != 0) {
  5524. CYASSL_MSG("Bad RNG Init, trying global");
  5525. if (initGlobalRNG == 0) {
  5526. CYASSL_MSG("Global RNG no Init");
  5527. return 0;
  5528. }
  5529. rng = &globalRNG;
  5530. }
  5531. if (DhGenerateKeyPair((DhKey*)dh->internal, rng, priv, &privSz,
  5532. pub, &pubSz) < 0) {
  5533. CYASSL_MSG("Bad DhGenerateKeyPair");
  5534. return 0;
  5535. }
  5536. if (dh->pub_key)
  5537. CyaSSL_BN_free(dh->pub_key);
  5538. dh->pub_key = CyaSSL_BN_new();
  5539. if (dh->pub_key == NULL) {
  5540. CYASSL_MSG("Bad DH new pub");
  5541. return 0;
  5542. }
  5543. if (dh->priv_key)
  5544. CyaSSL_BN_free(dh->priv_key);
  5545. dh->priv_key = CyaSSL_BN_new();
  5546. if (dh->priv_key == NULL) {
  5547. CYASSL_MSG("Bad DH new priv");
  5548. return 0;
  5549. }
  5550. if (CyaSSL_BN_bin2bn(pub, pubSz, dh->pub_key) == NULL) {
  5551. CYASSL_MSG("Bad DH bn2bin error pub");
  5552. return 0;
  5553. }
  5554. if (CyaSSL_BN_bin2bn(priv, privSz, dh->priv_key) == NULL) {
  5555. CYASSL_MSG("Bad DH bn2bin error priv");
  5556. return 0;
  5557. }
  5558. CYASSL_MSG("CyaSSL_generate_key success");
  5559. return 1;
  5560. }
  5561. /* return 1 on success, 0 otherwise */
  5562. int CyaSSL_DH_compute_key(unsigned char* key, CYASSL_BIGNUM* otherPub,
  5563. CYASSL_DH* dh)
  5564. {
  5565. unsigned char pub [1024];
  5566. unsigned char priv[1024];
  5567. word32 pubSz = sizeof(pub);
  5568. word32 privSz = sizeof(priv);
  5569. word32 keySz;
  5570. CYASSL_MSG("CyaSSL_DH_compute_key");
  5571. if (dh == NULL || dh->priv_key == NULL || otherPub == NULL) {
  5572. CYASSL_MSG("Bad function arguments");
  5573. return 0;
  5574. }
  5575. keySz = (word32)DH_size(dh);
  5576. if (keySz == 0) {
  5577. CYASSL_MSG("Bad DH_size");
  5578. return 0;
  5579. }
  5580. if (CyaSSL_BN_bn2bin(dh->priv_key, NULL) > (int)privSz) {
  5581. CYASSL_MSG("Bad priv internal size");
  5582. return 0;
  5583. }
  5584. if (CyaSSL_BN_bn2bin(otherPub, NULL) > (int)pubSz) {
  5585. CYASSL_MSG("Bad otherPub size");
  5586. return 0;
  5587. }
  5588. privSz = CyaSSL_BN_bn2bin(dh->priv_key, priv);
  5589. pubSz = CyaSSL_BN_bn2bin(otherPub, pub);
  5590. if (privSz <= 0 || pubSz <= 0) {
  5591. CYASSL_MSG("Bad BN2bin set");
  5592. return 0;
  5593. }
  5594. if (DhAgree((DhKey*)dh->internal, key, &keySz, priv, privSz, pub,
  5595. pubSz) < 0) {
  5596. CYASSL_MSG("DhAgree failed");
  5597. return 0;
  5598. }
  5599. CYASSL_MSG("CyaSSL_compute_key success");
  5600. return (int)keySz;
  5601. }
  5602. static void InitCyaSSL_DSA(CYASSL_DSA* dsa)
  5603. {
  5604. if (dsa) {
  5605. dsa->p = NULL;
  5606. dsa->q = NULL;
  5607. dsa->g = NULL;
  5608. dsa->pub_key = NULL;
  5609. dsa->priv_key = NULL;
  5610. dsa->internal = NULL;
  5611. dsa->inSet = 0;
  5612. dsa->exSet = 0;
  5613. }
  5614. }
  5615. CYASSL_DSA* CyaSSL_DSA_new(void)
  5616. {
  5617. CYASSL_DSA* external;
  5618. DsaKey* key;
  5619. CYASSL_MSG("CyaSSL_DSA_new");
  5620. key = (DsaKey*) XMALLOC(sizeof(DsaKey), NULL, DYNAMIC_TYPE_DSA);
  5621. if (key == NULL) {
  5622. CYASSL_MSG("CyaSSL_DSA_new malloc DsaKey failure");
  5623. return NULL;
  5624. }
  5625. external = (CYASSL_DSA*) XMALLOC(sizeof(CYASSL_DSA), NULL,
  5626. DYNAMIC_TYPE_DSA);
  5627. if (external == NULL) {
  5628. CYASSL_MSG("CyaSSL_DSA_new malloc CYASSL_DSA failure");
  5629. XFREE(key, NULL, DYNAMIC_TYPE_DSA);
  5630. return NULL;
  5631. }
  5632. InitCyaSSL_DSA(external);
  5633. InitDsaKey(key);
  5634. external->internal = key;
  5635. return external;
  5636. }
  5637. void CyaSSL_DSA_free(CYASSL_DSA* dsa)
  5638. {
  5639. CYASSL_MSG("CyaSSL_DSA_free");
  5640. if (dsa) {
  5641. if (dsa->internal) {
  5642. FreeDsaKey((DsaKey*)dsa->internal);
  5643. XFREE(dsa->internal, NULL, DYNAMIC_TYPE_DSA);
  5644. dsa->internal = NULL;
  5645. }
  5646. CyaSSL_BN_free(dsa->priv_key);
  5647. CyaSSL_BN_free(dsa->pub_key);
  5648. CyaSSL_BN_free(dsa->g);
  5649. CyaSSL_BN_free(dsa->q);
  5650. CyaSSL_BN_free(dsa->p);
  5651. InitCyaSSL_DSA(dsa); /* set back to NULLs for safety */
  5652. XFREE(dsa, NULL, DYNAMIC_TYPE_DSA);
  5653. }
  5654. }
  5655. int CyaSSL_DSA_generate_key(CYASSL_DSA* dsa)
  5656. {
  5657. (void)dsa;
  5658. CYASSL_MSG("CyaSSL_DSA_generate_key");
  5659. return 0; /* key gen not needed by server */
  5660. }
  5661. int CyaSSL_DSA_generate_parameters_ex(CYASSL_DSA* dsa, int bits,
  5662. unsigned char* seed, int seedLen, int* counterRet,
  5663. unsigned long* hRet, void* cb)
  5664. {
  5665. (void)dsa;
  5666. (void)bits;
  5667. (void)seed;
  5668. (void)seedLen;
  5669. (void)counterRet;
  5670. (void)hRet;
  5671. (void)cb;
  5672. CYASSL_MSG("CyaSSL_DSA_generate_parameters_ex");
  5673. return 0; /* key gen not needed by server */
  5674. }
  5675. static void InitCyaSSL_Rsa(CYASSL_RSA* rsa)
  5676. {
  5677. if (rsa) {
  5678. rsa->n = NULL;
  5679. rsa->e = NULL;
  5680. rsa->d = NULL;
  5681. rsa->p = NULL;
  5682. rsa->q = NULL;
  5683. rsa->dmp1 = NULL;
  5684. rsa->dmq1 = NULL;
  5685. rsa->iqmp = NULL;
  5686. rsa->internal = NULL;
  5687. rsa->inSet = 0;
  5688. rsa->exSet = 0;
  5689. }
  5690. }
  5691. CYASSL_RSA* CyaSSL_RSA_new(void)
  5692. {
  5693. CYASSL_RSA* external;
  5694. RsaKey* key;
  5695. CYASSL_MSG("CyaSSL_RSA_new");
  5696. key = (RsaKey*) XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_RSA);
  5697. if (key == NULL) {
  5698. CYASSL_MSG("CyaSSL_RSA_new malloc RsaKey failure");
  5699. return NULL;
  5700. }
  5701. external = (CYASSL_RSA*) XMALLOC(sizeof(CYASSL_RSA), NULL,
  5702. DYNAMIC_TYPE_RSA);
  5703. if (external == NULL) {
  5704. CYASSL_MSG("CyaSSL_RSA_new malloc CYASSL_RSA failure");
  5705. XFREE(key, NULL, DYNAMIC_TYPE_RSA);
  5706. return NULL;
  5707. }
  5708. InitCyaSSL_Rsa(external);
  5709. InitRsaKey(key, NULL);
  5710. external->internal = key;
  5711. return external;
  5712. }
  5713. void CyaSSL_RSA_free(CYASSL_RSA* rsa)
  5714. {
  5715. CYASSL_MSG("CyaSSL_RSA_free");
  5716. if (rsa) {
  5717. if (rsa->internal) {
  5718. FreeRsaKey((RsaKey*)rsa->internal);
  5719. XFREE(rsa->internal, NULL, DYNAMIC_TYPE_RSA);
  5720. rsa->internal = NULL;
  5721. }
  5722. CyaSSL_BN_free(rsa->iqmp);
  5723. CyaSSL_BN_free(rsa->dmq1);
  5724. CyaSSL_BN_free(rsa->dmp1);
  5725. CyaSSL_BN_free(rsa->q);
  5726. CyaSSL_BN_free(rsa->p);
  5727. CyaSSL_BN_free(rsa->d);
  5728. CyaSSL_BN_free(rsa->e);
  5729. CyaSSL_BN_free(rsa->n);
  5730. InitCyaSSL_Rsa(rsa); /* set back to NULLs for safety */
  5731. XFREE(rsa, NULL, DYNAMIC_TYPE_RSA);
  5732. }
  5733. }
  5734. static int SetIndividualExternal(CYASSL_BIGNUM** bn, mp_int* mpi)
  5735. {
  5736. CYASSL_MSG("Entering SetIndividualExternal");
  5737. if (mpi == NULL) {
  5738. CYASSL_MSG("mpi NULL error");
  5739. return -1;
  5740. }
  5741. if (*bn == NULL) {
  5742. *bn = CyaSSL_BN_new();
  5743. if (*bn == NULL) {
  5744. CYASSL_MSG("SetIndividualExternal alloc failed");
  5745. return -1;
  5746. }
  5747. }
  5748. if (mp_copy(mpi, (mp_int*)((*bn)->internal)) != MP_OKAY) {
  5749. CYASSL_MSG("mp_copy error");
  5750. return -1;
  5751. }
  5752. return 0;
  5753. }
  5754. static int SetDsaExternal(CYASSL_DSA* dsa)
  5755. {
  5756. DsaKey* key;
  5757. CYASSL_MSG("Entering SetDsaExternal");
  5758. if (dsa == NULL || dsa->internal == NULL) {
  5759. CYASSL_MSG("dsa key NULL error");
  5760. return -1;
  5761. }
  5762. key = (DsaKey*)dsa->internal;
  5763. if (SetIndividualExternal(&dsa->p, &key->p) < 0) {
  5764. CYASSL_MSG("dsa p key error");
  5765. return -1;
  5766. }
  5767. if (SetIndividualExternal(&dsa->q, &key->q) < 0) {
  5768. CYASSL_MSG("dsa q key error");
  5769. return -1;
  5770. }
  5771. if (SetIndividualExternal(&dsa->g, &key->g) < 0) {
  5772. CYASSL_MSG("dsa g key error");
  5773. return -1;
  5774. }
  5775. if (SetIndividualExternal(&dsa->pub_key, &key->y) < 0) {
  5776. CYASSL_MSG("dsa y key error");
  5777. return -1;
  5778. }
  5779. if (SetIndividualExternal(&dsa->priv_key, &key->x) < 0) {
  5780. CYASSL_MSG("dsa x key error");
  5781. return -1;
  5782. }
  5783. dsa->exSet = 1;
  5784. return 0;
  5785. }
  5786. static int SetRsaExternal(CYASSL_RSA* rsa)
  5787. {
  5788. RsaKey* key;
  5789. CYASSL_MSG("Entering SetRsaExternal");
  5790. if (rsa == NULL || rsa->internal == NULL) {
  5791. CYASSL_MSG("rsa key NULL error");
  5792. return -1;
  5793. }
  5794. key = (RsaKey*)rsa->internal;
  5795. if (SetIndividualExternal(&rsa->n, &key->n) < 0) {
  5796. CYASSL_MSG("rsa n key error");
  5797. return -1;
  5798. }
  5799. if (SetIndividualExternal(&rsa->e, &key->e) < 0) {
  5800. CYASSL_MSG("rsa e key error");
  5801. return -1;
  5802. }
  5803. if (SetIndividualExternal(&rsa->d, &key->d) < 0) {
  5804. CYASSL_MSG("rsa d key error");
  5805. return -1;
  5806. }
  5807. if (SetIndividualExternal(&rsa->p, &key->p) < 0) {
  5808. CYASSL_MSG("rsa p key error");
  5809. return -1;
  5810. }
  5811. if (SetIndividualExternal(&rsa->q, &key->q) < 0) {
  5812. CYASSL_MSG("rsa q key error");
  5813. return -1;
  5814. }
  5815. if (SetIndividualExternal(&rsa->dmp1, &key->dP) < 0) {
  5816. CYASSL_MSG("rsa dP key error");
  5817. return -1;
  5818. }
  5819. if (SetIndividualExternal(&rsa->dmq1, &key->dQ) < 0) {
  5820. CYASSL_MSG("rsa dQ key error");
  5821. return -1;
  5822. }
  5823. if (SetIndividualExternal(&rsa->iqmp, &key->u) < 0) {
  5824. CYASSL_MSG("rsa u key error");
  5825. return -1;
  5826. }
  5827. rsa->exSet = 1;
  5828. return 0;
  5829. }
  5830. int CyaSSL_RSA_generate_key_ex(CYASSL_RSA* rsa, int bits, CYASSL_BIGNUM* bn,
  5831. void* cb)
  5832. {
  5833. RNG rng;
  5834. CYASSL_MSG("CyaSSL_RSA_generate_key_ex");
  5835. (void)rsa;
  5836. (void)bits;
  5837. (void)cb;
  5838. (void)bn;
  5839. if (InitRng(&rng) < 0) {
  5840. CYASSL_MSG("RNG init failed");
  5841. return -1;
  5842. }
  5843. #ifdef CYASSL_KEY_GEN
  5844. if (MakeRsaKey((RsaKey*)rsa->internal, bits, 65537, &rng) < 0) {
  5845. CYASSL_MSG("MakeRsaKey failed");
  5846. return -1;
  5847. }
  5848. if (SetRsaExternal(rsa) < 0) {
  5849. CYASSL_MSG("SetRsaExternal failed");
  5850. return -1;
  5851. }
  5852. rsa->inSet = 1;
  5853. return 1; /* success */
  5854. #else
  5855. CYASSL_MSG("No Key Gen built in");
  5856. return -1;
  5857. #endif
  5858. }
  5859. int CyaSSL_RSA_blinding_on(CYASSL_RSA* rsa, CYASSL_BN_CTX* bn)
  5860. {
  5861. (void)rsa;
  5862. (void)bn;
  5863. CYASSL_MSG("CyaSSL_RSA_blinding_on");
  5864. return 1; /* on by default */
  5865. }
  5866. int CyaSSL_RSA_public_encrypt(int len, unsigned char* fr,
  5867. unsigned char* to, CYASSL_RSA* rsa, int padding)
  5868. {
  5869. (void)len;
  5870. (void)fr;
  5871. (void)to;
  5872. (void)rsa;
  5873. (void)padding;
  5874. CYASSL_MSG("CyaSSL_RSA_public_encrypt");
  5875. return -1;
  5876. }
  5877. int CyaSSL_RSA_private_decrypt(int len, unsigned char* fr,
  5878. unsigned char* to, CYASSL_RSA* rsa, int padding)
  5879. {
  5880. (void)len;
  5881. (void)fr;
  5882. (void)to;
  5883. (void)rsa;
  5884. (void)padding;
  5885. CYASSL_MSG("CyaSSL_RSA_private_decrypt");
  5886. return -1;
  5887. }
  5888. int CyaSSL_RSA_size(const CYASSL_RSA* rsa)
  5889. {
  5890. CYASSL_MSG("CyaSSL_RSA_size");
  5891. if (rsa == NULL)
  5892. return 0;
  5893. return CyaSSL_BN_num_bytes(rsa->n);
  5894. }
  5895. /* return 0 on success, < 0 otherwise */
  5896. int CyaSSL_DSA_do_sign(const unsigned char* d, unsigned char* sigRet,
  5897. CYASSL_DSA* dsa)
  5898. {
  5899. RNG tmpRNG;
  5900. RNG* rng = &tmpRNG;
  5901. CYASSL_MSG("CyaSSL_DSA_do_sign");
  5902. if (d == NULL || sigRet == NULL || dsa == NULL) {
  5903. CYASSL_MSG("Bad function arguments");
  5904. return -1;
  5905. }
  5906. if (dsa->inSet == 0) {
  5907. CYASSL_MSG("No DSA internal set");
  5908. return -1;
  5909. }
  5910. if (InitRng(&tmpRNG) != 0) {
  5911. CYASSL_MSG("Bad RNG Init, trying global");
  5912. if (initGlobalRNG == 0) {
  5913. CYASSL_MSG("Global RNG no Init");
  5914. return -1;
  5915. }
  5916. rng = &globalRNG;
  5917. }
  5918. if (DsaSign(d, sigRet, (DsaKey*)dsa->internal, rng) < 0) {
  5919. CYASSL_MSG("DsaSign failed");
  5920. return -1;
  5921. }
  5922. return 0;
  5923. }
  5924. /* return 1 on success, 0 otherwise */
  5925. int CyaSSL_RSA_sign(int type, const unsigned char* m,
  5926. unsigned int mLen, unsigned char* sigRet,
  5927. unsigned int* sigLen, CYASSL_RSA* rsa)
  5928. {
  5929. byte encodedSig[MAX_ENCODED_SIG_SZ];
  5930. word32 outLen;
  5931. word32 signSz;
  5932. RNG tmpRNG;
  5933. RNG* rng = &tmpRNG;
  5934. CYASSL_MSG("CyaSSL_RSA_sign");
  5935. if (m == NULL || sigRet == NULL || sigLen == NULL || rsa == NULL) {
  5936. CYASSL_MSG("Bad function arguments");
  5937. return 0;
  5938. }
  5939. if (rsa->inSet == 0) {
  5940. CYASSL_MSG("No RSA internal set");
  5941. return 0;
  5942. }
  5943. outLen = (word32)CyaSSL_BN_num_bytes(rsa->n);
  5944. if (outLen == 0) {
  5945. CYASSL_MSG("Bad RSA size");
  5946. return 0;
  5947. }
  5948. if (InitRng(&tmpRNG) != 0) {
  5949. CYASSL_MSG("Bad RNG Init, trying global");
  5950. if (initGlobalRNG == 0) {
  5951. CYASSL_MSG("Global RNG no Init");
  5952. return 0;
  5953. }
  5954. rng = &globalRNG;
  5955. }
  5956. switch (type) {
  5957. case NID_md5:
  5958. type = MD5h;
  5959. break;
  5960. case NID_sha1:
  5961. type = SHAh;
  5962. break;
  5963. default:
  5964. CYASSL_MSG("Bad md type");
  5965. return 0;
  5966. }
  5967. signSz = EncodeSignature(encodedSig, m, mLen, type);
  5968. if (signSz == 0) {
  5969. CYASSL_MSG("Bad Encode Signature");
  5970. return 0;
  5971. }
  5972. *sigLen = RsaSSL_Sign(encodedSig, signSz, sigRet, outLen,
  5973. (RsaKey*)rsa->internal, rng);
  5974. if (*sigLen <= 0) {
  5975. CYASSL_MSG("Bad Rsa Sign");
  5976. return 0;
  5977. }
  5978. CYASSL_MSG("CyaSSL_RSA_sign success");
  5979. return 1; /* success */
  5980. }
  5981. int CyaSSL_RSA_public_decrypt(int flen, unsigned char* from,
  5982. unsigned char* to, CYASSL_RSA* rsa, int padding)
  5983. {
  5984. (void)flen;
  5985. (void)from;
  5986. (void)to;
  5987. (void)rsa;
  5988. (void)padding;
  5989. CYASSL_MSG("CyaSSL_RSA_public_decrypt");
  5990. return -1;
  5991. }
  5992. /* generate p-1 and q-1 */
  5993. int CyaSSL_RSA_GenAdd(CYASSL_RSA* rsa)
  5994. {
  5995. int err;
  5996. mp_int tmp;
  5997. CYASSL_MSG("CyaSSL_RsaGenAdd");
  5998. if (rsa == NULL || rsa->p == NULL || rsa->q == NULL || rsa->d == NULL ||
  5999. rsa->dmp1 == NULL || rsa->dmq1 == NULL) {
  6000. CYASSL_MSG("rsa no init error");
  6001. return -1;
  6002. }
  6003. if (mp_init(&tmp) != MP_OKAY) {
  6004. CYASSL_MSG("mp_init error");
  6005. return -1;
  6006. }
  6007. err = mp_sub_d((mp_int*)rsa->p->internal, 1, &tmp);
  6008. if (err != MP_OKAY)
  6009. CYASSL_MSG("mp_sub_d error");
  6010. else
  6011. err = mp_mod((mp_int*)rsa->d->internal, &tmp,
  6012. (mp_int*)rsa->dmp1->internal);
  6013. if (err != MP_OKAY)
  6014. CYASSL_MSG("mp_mod error");
  6015. else
  6016. err = mp_sub_d((mp_int*)rsa->q->internal, 1, &tmp);
  6017. if (err != MP_OKAY)
  6018. CYASSL_MSG("mp_sub_d error");
  6019. else
  6020. err = mp_mod((mp_int*)rsa->d->internal, &tmp,
  6021. (mp_int*)rsa->dmq1->internal);
  6022. mp_clear(&tmp);
  6023. if (err == MP_OKAY)
  6024. return 0;
  6025. else
  6026. return -1;
  6027. }
  6028. void CyaSSL_HMAC_Init(CYASSL_HMAC_CTX* ctx, const void* key, int keylen,
  6029. const EVP_MD* type)
  6030. {
  6031. CYASSL_MSG("CyaSSL_HMAC_Init");
  6032. if (ctx == NULL) {
  6033. CYASSL_MSG("no ctx on init");
  6034. return;
  6035. }
  6036. if (type) {
  6037. CYASSL_MSG("init has type");
  6038. if (XSTRNCMP(type, "MD5", 3) == 0) {
  6039. CYASSL_MSG("md5 hmac");
  6040. ctx->type = MD5;
  6041. }
  6042. else if (XSTRNCMP(type, "SHA256", 6) == 0) {
  6043. CYASSL_MSG("sha256 hmac");
  6044. ctx->type = SHA256;
  6045. }
  6046. /* has to be last since would pick or 256, 384, or 512 too */
  6047. else if (XSTRNCMP(type, "SHA", 3) == 0) {
  6048. CYASSL_MSG("sha hmac");
  6049. ctx->type = SHA;
  6050. }
  6051. else {
  6052. CYASSL_MSG("bad init type");
  6053. }
  6054. }
  6055. if (key && keylen) {
  6056. CYASSL_MSG("keying hmac");
  6057. HmacSetKey(&ctx->hmac, ctx->type, (const byte*)key, (word32)keylen);
  6058. }
  6059. }
  6060. void CyaSSL_HMAC_Update(CYASSL_HMAC_CTX* ctx, const unsigned char* data,
  6061. int len)
  6062. {
  6063. CYASSL_MSG("CyaSSL_HMAC_Update");
  6064. if (ctx && data) {
  6065. CYASSL_MSG("updating hmac");
  6066. HmacUpdate(&ctx->hmac, data, (word32)len);
  6067. }
  6068. }
  6069. void CyaSSL_HMAC_Final(CYASSL_HMAC_CTX* ctx, unsigned char* hash,
  6070. unsigned int* len)
  6071. {
  6072. CYASSL_MSG("CyaSSL_HMAC_Final");
  6073. if (ctx && hash) {
  6074. CYASSL_MSG("final hmac");
  6075. HmacFinal(&ctx->hmac, hash);
  6076. if (len) {
  6077. CYASSL_MSG("setting output len");
  6078. switch (ctx->type) {
  6079. case MD5:
  6080. *len = MD5_DIGEST_SIZE;
  6081. break;
  6082. case SHA:
  6083. *len = SHA_DIGEST_SIZE;
  6084. break;
  6085. case SHA256:
  6086. *len = SHA256_DIGEST_SIZE;
  6087. break;
  6088. default:
  6089. CYASSL_MSG("bad hmac type");
  6090. }
  6091. }
  6092. }
  6093. }
  6094. void CyaSSL_HMAC_cleanup(CYASSL_HMAC_CTX* ctx)
  6095. {
  6096. (void)ctx;
  6097. CYASSL_MSG("CyaSSL_HMAC_cleanup");
  6098. }
  6099. const CYASSL_EVP_MD* CyaSSL_EVP_get_digestbynid(int id)
  6100. {
  6101. CYASSL_MSG("CyaSSL_get_digestbynid");
  6102. switch(id) {
  6103. case NID_md5:
  6104. return CyaSSL_EVP_md5();
  6105. break;
  6106. case NID_sha1:
  6107. return CyaSSL_EVP_sha1();
  6108. break;
  6109. default:
  6110. CYASSL_MSG("Bad digest id value");
  6111. }
  6112. return NULL;
  6113. }
  6114. CYASSL_RSA* CyaSSL_EVP_PKEY_get1_RSA(CYASSL_EVP_PKEY* key)
  6115. {
  6116. (void)key;
  6117. CYASSL_MSG("CyaSSL_EVP_PKEY_get1_RSA");
  6118. return NULL;
  6119. }
  6120. CYASSL_DSA* CyaSSL_EVP_PKEY_get1_DSA(CYASSL_EVP_PKEY* key)
  6121. {
  6122. (void)key;
  6123. CYASSL_MSG("CyaSSL_EVP_PKEY_get1_DSA");
  6124. return NULL;
  6125. }
  6126. void* CyaSSL_EVP_X_STATE(const CYASSL_EVP_CIPHER_CTX* ctx)
  6127. {
  6128. CYASSL_MSG("CyaSSL_EVP_X_STATE");
  6129. if (ctx) {
  6130. switch (ctx->cipherType) {
  6131. case ARC4_TYPE:
  6132. CYASSL_MSG("returning arc4 state");
  6133. return (void*)&ctx->cipher.arc4.x;
  6134. break;
  6135. default:
  6136. CYASSL_MSG("bad x state type");
  6137. return 0;
  6138. }
  6139. }
  6140. return NULL;
  6141. }
  6142. int CyaSSL_EVP_X_STATE_LEN(const CYASSL_EVP_CIPHER_CTX* ctx)
  6143. {
  6144. CYASSL_MSG("CyaSSL_EVP_X_STATE_LEN");
  6145. if (ctx) {
  6146. switch (ctx->cipherType) {
  6147. case ARC4_TYPE:
  6148. CYASSL_MSG("returning arc4 state size");
  6149. return sizeof(Arc4);
  6150. break;
  6151. default:
  6152. CYASSL_MSG("bad x state type");
  6153. return 0;
  6154. }
  6155. }
  6156. return 0;
  6157. }
  6158. void CyaSSL_3des_iv(CYASSL_EVP_CIPHER_CTX* ctx, int doset,
  6159. unsigned char* iv, int len)
  6160. {
  6161. (void)len;
  6162. CYASSL_MSG("CyaSSL_3des_iv");
  6163. if (ctx == NULL || iv == NULL) {
  6164. CYASSL_MSG("Bad function argument");
  6165. return;
  6166. }
  6167. if (doset)
  6168. Des3_SetIV(&ctx->cipher.des3, iv);
  6169. else
  6170. memcpy(iv, &ctx->cipher.des3.reg, DES_BLOCK_SIZE);
  6171. }
  6172. void CyaSSL_aes_ctr_iv(CYASSL_EVP_CIPHER_CTX* ctx, int doset,
  6173. unsigned char* iv, int len)
  6174. {
  6175. (void)len;
  6176. CYASSL_MSG("CyaSSL_aes_ctr_iv");
  6177. if (ctx == NULL || iv == NULL) {
  6178. CYASSL_MSG("Bad function argument");
  6179. return;
  6180. }
  6181. if (doset)
  6182. AesSetIV(&ctx->cipher.aes, iv);
  6183. else
  6184. memcpy(iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
  6185. }
  6186. const CYASSL_EVP_MD* CyaSSL_EVP_ripemd160(void)
  6187. {
  6188. CYASSL_MSG("CyaSSL_ripemd160");
  6189. return NULL;
  6190. }
  6191. int CyaSSL_EVP_MD_size(const CYASSL_EVP_MD* type)
  6192. {
  6193. CYASSL_MSG("CyaSSL_EVP_MD_size");
  6194. if (type == NULL) {
  6195. CYASSL_MSG("No md type arg");
  6196. return BAD_FUNC_ARG;
  6197. }
  6198. if (XSTRNCMP(type, "MD5", 3) == 0) {
  6199. return MD5_DIGEST_SIZE;
  6200. }
  6201. else if (XSTRNCMP(type, "SHA256", 6) == 0) {
  6202. return SHA256_DIGEST_SIZE;
  6203. }
  6204. #ifdef CYASSL_SHA384
  6205. else if (XSTRNCMP(type, "SHA384", 6) == 0) {
  6206. return SHA384_DIGEST_SIZE;
  6207. }
  6208. #endif
  6209. #ifdef CYASSL_SHA512
  6210. else if (XSTRNCMP(type, "SHA512", 6) == 0) {
  6211. return SHA512_DIGEST_SIZE;
  6212. }
  6213. #endif
  6214. /* has to be last since would pick or 256, 384, or 512 too */
  6215. else if (XSTRNCMP(type, "SHA", 3) == 0) {
  6216. return SHA_DIGEST_SIZE;
  6217. }
  6218. return BAD_FUNC_ARG;
  6219. }
  6220. int CyaSSL_EVP_CIPHER_CTX_iv_length(const CYASSL_EVP_CIPHER_CTX* ctx)
  6221. {
  6222. CYASSL_MSG("CyaSSL_EVP_CIPHER_CTX_iv_length");
  6223. switch (ctx->cipherType) {
  6224. case AES_128_CBC_TYPE :
  6225. case AES_192_CBC_TYPE :
  6226. case AES_256_CBC_TYPE :
  6227. CYASSL_MSG("AES CBC");
  6228. return AES_BLOCK_SIZE;
  6229. break;
  6230. #ifdef CYASSL_AES_COUNTER
  6231. case AES_128_CTR_TYPE :
  6232. case AES_192_CTR_TYPE :
  6233. case AES_256_CTR_TYPE :
  6234. CYASSL_MSG("AES CTR");
  6235. return AES_BLOCK_SIZE;
  6236. break;
  6237. #endif
  6238. case DES_CBC_TYPE :
  6239. CYASSL_MSG("DES CBC");
  6240. return DES_BLOCK_SIZE;
  6241. break;
  6242. case DES_EDE3_CBC_TYPE :
  6243. CYASSL_MSG("DES EDE3 CBC");
  6244. return DES_BLOCK_SIZE;
  6245. break;
  6246. case ARC4_TYPE :
  6247. CYASSL_MSG("ARC4");
  6248. return 0;
  6249. break;
  6250. case NULL_CIPHER_TYPE :
  6251. CYASSL_MSG("NULL");
  6252. return 0;
  6253. break;
  6254. default: {
  6255. CYASSL_MSG("bad type");
  6256. }
  6257. }
  6258. return 0;
  6259. }
  6260. void CyaSSL_OPENSSL_free(void* p)
  6261. {
  6262. CYASSL_MSG("CyaSSL_OPENSSL_free");
  6263. XFREE(p, NULL, 0);
  6264. }
  6265. int CyaSSL_PEM_write_bio_RSAPrivateKey(CYASSL_BIO* bio, RSA* rsa,
  6266. const EVP_CIPHER* cipher,
  6267. unsigned char* passwd, int len,
  6268. pem_password_cb cb, void* arg)
  6269. {
  6270. (void)bio;
  6271. (void)rsa;
  6272. (void)cipher;
  6273. (void)passwd;
  6274. (void)len;
  6275. (void)cb;
  6276. (void)arg;
  6277. CYASSL_MSG("CyaSSL_PEM_write_bio_RSAPrivateKey");
  6278. return -1;
  6279. }
  6280. int CyaSSL_PEM_write_bio_DSAPrivateKey(CYASSL_BIO* bio, DSA* rsa,
  6281. const EVP_CIPHER* cipher,
  6282. unsigned char* passwd, int len,
  6283. pem_password_cb cb, void* arg)
  6284. {
  6285. (void)bio;
  6286. (void)rsa;
  6287. (void)cipher;
  6288. (void)passwd;
  6289. (void)len;
  6290. (void)cb;
  6291. (void)arg;
  6292. CYASSL_MSG("CyaSSL_PEM_write_bio_DSAPrivateKey");
  6293. return -1;
  6294. }
  6295. CYASSL_EVP_PKEY* CyaSSL_PEM_read_bio_PrivateKey(CYASSL_BIO* bio,
  6296. CYASSL_EVP_PKEY** key, pem_password_cb cb, void* arg)
  6297. {
  6298. (void)bio;
  6299. (void)key;
  6300. (void)cb;
  6301. (void)arg;
  6302. CYASSL_MSG("CyaSSL_PEM_read_bio_PrivateKey");
  6303. return NULL;
  6304. }
  6305. /* Return bytes written to buff or < 0 for error */
  6306. int CyaSSL_KeyPemToDer(const unsigned char* pem, int pemSz, unsigned char* buff,
  6307. int buffSz, const char* pass)
  6308. {
  6309. EncryptedInfo info;
  6310. int eccKey = 0;
  6311. int ret;
  6312. buffer der;
  6313. (void)pass;
  6314. CYASSL_ENTER("CyaSSL_KeyPemToDer");
  6315. if (pem == NULL || buff == NULL || buffSz <= 0) {
  6316. CYASSL_MSG("Bad pem der args");
  6317. return BAD_FUNC_ARG;
  6318. }
  6319. info.set = 0;
  6320. info.ctx = NULL;
  6321. info.consumed = 0;
  6322. der.buffer = NULL;
  6323. ret = PemToDer(pem, pemSz, PRIVATEKEY_TYPE, &der, NULL, &info, &eccKey);
  6324. if (ret < 0) {
  6325. CYASSL_MSG("Bad Pem To Der");
  6326. }
  6327. else {
  6328. if (der.length <= (word32)buffSz) {
  6329. XMEMCPY(buff, der.buffer, der.length);
  6330. ret = der.length;
  6331. }
  6332. else {
  6333. CYASSL_MSG("Bad der length");
  6334. ret = BAD_FUNC_ARG;
  6335. }
  6336. }
  6337. XFREE(der.buffer, NULL, DYANMIC_KEY_TYPE);
  6338. return ret;
  6339. }
  6340. /* Load RSA from Der, 0 on success < 0 on error */
  6341. int CyaSSL_RSA_LoadDer(CYASSL_RSA* rsa, const unsigned char* der, int derSz)
  6342. {
  6343. word32 idx = 0;
  6344. int ret;
  6345. CYASSL_ENTER("CyaSSL_RSA_LoadDer");
  6346. if (rsa == NULL || rsa->internal == NULL || der == NULL || derSz <= 0) {
  6347. CYASSL_MSG("Bad function arguments");
  6348. return BAD_FUNC_ARG;
  6349. }
  6350. ret = RsaPrivateKeyDecode(der, &idx, (RsaKey*)rsa->internal, derSz);
  6351. if (ret < 0) {
  6352. CYASSL_MSG("RsaPrivateKeyDecode failed");
  6353. return ret;
  6354. }
  6355. if (SetRsaExternal(rsa) < 0) {
  6356. CYASSL_MSG("SetRsaExternal failed");
  6357. return -1;
  6358. }
  6359. rsa->inSet = 1;
  6360. return 0;
  6361. }
  6362. /* Load DSA from Der, 0 on success < 0 on error */
  6363. int CyaSSL_DSA_LoadDer(CYASSL_DSA* dsa, const unsigned char* der, int derSz)
  6364. {
  6365. word32 idx = 0;
  6366. int ret;
  6367. CYASSL_ENTER("CyaSSL_DSA_LoadDer");
  6368. if (dsa == NULL || dsa->internal == NULL || der == NULL || derSz <= 0) {
  6369. CYASSL_MSG("Bad function arguments");
  6370. return BAD_FUNC_ARG;
  6371. }
  6372. ret = DsaPrivateKeyDecode(der, &idx, (DsaKey*)dsa->internal, derSz);
  6373. if (ret < 0) {
  6374. CYASSL_MSG("DsaPrivateKeyDecode failed");
  6375. return ret;
  6376. }
  6377. if (SetDsaExternal(dsa) < 0) {
  6378. CYASSL_MSG("SetDsaExternal failed");
  6379. return -1;
  6380. }
  6381. dsa->inSet = 1;
  6382. return 0;
  6383. }
  6384. #endif /* OPENSSL_EXTRA */
  6385. #ifdef SESSION_CERTS
  6386. /* Get peer's certificate chain */
  6387. CYASSL_X509_CHAIN* CyaSSL_get_peer_chain(CYASSL* ssl)
  6388. {
  6389. CYASSL_ENTER("CyaSSL_get_peer_chain");
  6390. if (ssl)
  6391. return &ssl->session.chain;
  6392. return 0;
  6393. }
  6394. /* Get peer's certificate chain total count */
  6395. int CyaSSL_get_chain_count(CYASSL_X509_CHAIN* chain)
  6396. {
  6397. CYASSL_ENTER("CyaSSL_get_chain_count");
  6398. if (chain)
  6399. return chain->count;
  6400. return 0;
  6401. }
  6402. /* Get peer's ASN.1 DER ceritifcate at index (idx) length in bytes */
  6403. int CyaSSL_get_chain_length(CYASSL_X509_CHAIN* chain, int idx)
  6404. {
  6405. CYASSL_ENTER("CyaSSL_get_chain_length");
  6406. if (chain)
  6407. return chain->certs[idx].length;
  6408. return 0;
  6409. }
  6410. /* Get peer's ASN.1 DER ceritifcate at index (idx) */
  6411. byte* CyaSSL_get_chain_cert(CYASSL_X509_CHAIN* chain, int idx)
  6412. {
  6413. CYASSL_ENTER("CyaSSL_get_chain_cert");
  6414. if (chain)
  6415. return chain->certs[idx].buffer;
  6416. return 0;
  6417. }
  6418. /* Get peer's PEM ceritifcate at index (idx), output to buffer if inLen big
  6419. enough else return error (-1), output length is in *outLen */
  6420. int CyaSSL_get_chain_cert_pem(CYASSL_X509_CHAIN* chain, int idx,
  6421. unsigned char* buf, int inLen, int* outLen)
  6422. {
  6423. const char header[] = "-----BEGIN CERTIFICATE-----\n";
  6424. const char footer[] = "-----END CERTIFICATE-----\n";
  6425. int headerLen = sizeof(header) - 1;
  6426. int footerLen = sizeof(footer) - 1;
  6427. int i;
  6428. int err;
  6429. CYASSL_ENTER("CyaSSL_get_chain_cert_pem");
  6430. if (!chain || !outLen || !buf)
  6431. return BAD_FUNC_ARG;
  6432. /* don't even try if inLen too short */
  6433. if (inLen < headerLen + footerLen + chain->certs[idx].length)
  6434. return BAD_FUNC_ARG;
  6435. /* header */
  6436. XMEMCPY(buf, header, headerLen);
  6437. i = headerLen;
  6438. /* body */
  6439. *outLen = inLen; /* input to Base64_Encode */
  6440. if ( (err = Base64_Encode(chain->certs[idx].buffer,
  6441. chain->certs[idx].length, buf + i, (word32*)outLen)) < 0)
  6442. return err;
  6443. i += *outLen;
  6444. /* footer */
  6445. if ( (i + footerLen) > inLen)
  6446. return BAD_FUNC_ARG;
  6447. XMEMCPY(buf + i, footer, footerLen);
  6448. *outLen += headerLen + footerLen;
  6449. return 0;
  6450. }
  6451. /* get session ID */
  6452. const byte* CyaSSL_get_sessionID(const CYASSL_SESSION* session)
  6453. {
  6454. CYASSL_ENTER("CyaSSL_get_sessionID");
  6455. if (session)
  6456. return session->sessionID;
  6457. return NULL;
  6458. }
  6459. #endif /* SESSION_CERTS */
  6460. long CyaSSL_CTX_OCSP_set_options(CYASSL_CTX* ctx, long options)
  6461. {
  6462. CYASSL_ENTER("CyaSSL_CTX_OCSP_set_options");
  6463. #ifdef HAVE_OCSP
  6464. if (ctx != NULL) {
  6465. ctx->ocsp.enabled = (options & CYASSL_OCSP_ENABLE) != 0;
  6466. ctx->ocsp.useOverrideUrl = (options & CYASSL_OCSP_URL_OVERRIDE) != 0;
  6467. ctx->ocsp.useNonce = (options & CYASSL_OCSP_NO_NONCE) == 0;
  6468. return 1;
  6469. }
  6470. return 0;
  6471. #else
  6472. (void)ctx;
  6473. (void)options;
  6474. return NOT_COMPILED_IN;
  6475. #endif
  6476. }
  6477. int CyaSSL_CTX_OCSP_set_override_url(CYASSL_CTX* ctx, const char* url)
  6478. {
  6479. CYASSL_ENTER("CyaSSL_CTX_OCSP_set_override_url");
  6480. #ifdef HAVE_OCSP
  6481. return CyaSSL_OCSP_set_override_url(&ctx->ocsp, url);
  6482. #else
  6483. (void)ctx;
  6484. (void)url;
  6485. return NOT_COMPILED_IN;
  6486. #endif
  6487. }