echoclient.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* echoclient.c
  2. *
  3. * Copyright (C) 2006-2014 wolfSSL Inc.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * CyaSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <cyassl/ctaocrypt/settings.h>
  25. #include <cyassl/openssl/ssl.h>
  26. #if defined(CYASSL_MDK_ARM)
  27. #include <stdio.h>
  28. #include <string.h>
  29. #if defined(CYASSL_MDK5)
  30. #include "cmsis_os.h"
  31. #include "rl_fs.h"
  32. #include "rl_net.h"
  33. #else
  34. #include "rtl.h"
  35. #endif
  36. #include "cyassl_MDK_ARM.h"
  37. #endif
  38. #include <cyassl/test.h>
  39. #include "examples/echoclient/echoclient.h"
  40. void echoclient_test(void* args)
  41. {
  42. SOCKET_T sockfd = 0;
  43. FILE* fin = stdin ;
  44. FILE* fout = stdout;
  45. int inCreated = 0;
  46. int outCreated = 0;
  47. char msg[1024];
  48. char reply[1024+1];
  49. SSL_METHOD* method = 0;
  50. SSL_CTX* ctx = 0;
  51. SSL* ssl = 0;
  52. int doDTLS = 0;
  53. int doPSK = 0;
  54. int sendSz;
  55. int argc = 0;
  56. char** argv = 0;
  57. word16 port = yasslPort;
  58. ((func_args*)args)->return_code = -1; /* error state */
  59. #ifndef CYASSL_MDK_SHELL
  60. argc = ((func_args*)args)->argc;
  61. argv = ((func_args*)args)->argv;
  62. #endif
  63. if (argc >= 2) {
  64. fin = fopen(argv[1], "r");
  65. inCreated = 1;
  66. }
  67. if (argc >= 3) {
  68. fout = fopen(argv[2], "w");
  69. outCreated = 1;
  70. }
  71. if (!fin) err_sys("can't open input file");
  72. if (!fout) err_sys("can't open output file");
  73. #ifdef CYASSL_DTLS
  74. doDTLS = 1;
  75. #endif
  76. #ifdef CYASSL_LEANPSK
  77. doPSK = 1;
  78. #endif
  79. #if defined(NO_RSA) && !defined(HAVE_ECC)
  80. doPSK = 1;
  81. #endif
  82. #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) && !defined(CYASSL_MDK_SHELL)
  83. port = ((func_args*)args)->signal->port;
  84. #endif
  85. #if defined(CYASSL_DTLS)
  86. method = DTLSv1_client_method();
  87. #elif !defined(NO_TLS)
  88. method = CyaSSLv23_client_method();
  89. #else
  90. method = SSLv3_client_method();
  91. #endif
  92. ctx = SSL_CTX_new(method);
  93. #ifndef NO_FILESYSTEM
  94. #ifndef NO_RSA
  95. if (SSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_SUCCESS)
  96. err_sys("can't load ca file, Please run from CyaSSL home dir");
  97. #endif
  98. #ifdef HAVE_ECC
  99. if (SSL_CTX_load_verify_locations(ctx, eccCert, 0) != SSL_SUCCESS)
  100. err_sys("can't load ca file, Please run from CyaSSL home dir");
  101. #endif
  102. #elif !defined(NO_CERTS)
  103. if (!doPSK)
  104. load_buffer(ctx, caCert, CYASSL_CA);
  105. #endif
  106. #if defined(CYASSL_SNIFFER) && !defined(HAVE_NTRU) && !defined(HAVE_ECC)
  107. /* don't use EDH, can't sniff tmp keys */
  108. SSL_CTX_set_cipher_list(ctx, "AES256-SHA");
  109. #endif
  110. if (doPSK) {
  111. #ifndef NO_PSK
  112. const char *defaultCipherList;
  113. CyaSSL_CTX_set_psk_client_callback(ctx, my_psk_client_cb);
  114. #ifdef HAVE_NULL_CIPHER
  115. defaultCipherList = "PSK-NULL-SHA256";
  116. #else
  117. defaultCipherList = "PSK-AES128-CBC-SHA256";
  118. #endif
  119. if (CyaSSL_CTX_set_cipher_list(ctx,defaultCipherList) !=SSL_SUCCESS)
  120. err_sys("client can't set cipher list 2");
  121. #endif
  122. }
  123. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  124. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  125. #endif
  126. #if defined(CYASSL_MDK_ARM)
  127. CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
  128. #endif
  129. ssl = SSL_new(ctx);
  130. if (doDTLS) {
  131. SOCKADDR_IN_T addr;
  132. build_addr(&addr, yasslIP, port, 1);
  133. CyaSSL_dtls_set_peer(ssl, &addr, sizeof(addr));
  134. tcp_socket(&sockfd, 1);
  135. }
  136. else {
  137. tcp_connect(&sockfd, yasslIP, port, 0);
  138. }
  139. SSL_set_fd(ssl, sockfd);
  140. #if defined(USE_WINDOWS_API) && defined(CYASSL_DTLS) && defined(NO_MAIN_DRIVER)
  141. /* let echoserver bind first, TODO: add Windows signal like pthreads does */
  142. Sleep(100);
  143. #endif
  144. if (SSL_connect(ssl) != SSL_SUCCESS) err_sys("SSL_connect failed");
  145. while (fgets(msg, sizeof(msg), fin) != 0) {
  146. sendSz = (int)strlen(msg);
  147. if (SSL_write(ssl, msg, sendSz) != sendSz)
  148. err_sys("SSL_write failed");
  149. if (strncmp(msg, "quit", 4) == 0) {
  150. fputs("sending server shutdown command: quit!\n", fout);
  151. break;
  152. }
  153. if (strncmp(msg, "break", 5) == 0) {
  154. fputs("sending server session close: break!\n", fout);
  155. break;
  156. }
  157. #ifndef CYASSL_MDK_SHELL
  158. while (sendSz) {
  159. int got;
  160. if ( (got = SSL_read(ssl, reply, sizeof(reply)-1)) > 0) {
  161. reply[got] = 0;
  162. fputs(reply, fout);
  163. fflush(fout) ;
  164. sendSz -= got;
  165. }
  166. else
  167. break;
  168. }
  169. #else
  170. {
  171. int got;
  172. if ( (got = SSL_read(ssl, reply, sizeof(reply)-1)) > 0) {
  173. reply[got] = 0;
  174. fputs(reply, fout);
  175. fflush(fout) ;
  176. sendSz -= got;
  177. }
  178. }
  179. #endif
  180. }
  181. #ifdef CYASSL_DTLS
  182. strncpy(msg, "break", 6);
  183. sendSz = (int)strlen(msg);
  184. /* try to tell server done */
  185. SSL_write(ssl, msg, sendSz);
  186. #else
  187. SSL_shutdown(ssl);
  188. #endif
  189. SSL_free(ssl);
  190. SSL_CTX_free(ctx);
  191. fflush(fout);
  192. if (inCreated) fclose(fin);
  193. if (outCreated) fclose(fout);
  194. CloseSocket(sockfd);
  195. ((func_args*)args)->return_code = 0;
  196. }
  197. /* so overall tests can pull in test function */
  198. #ifndef NO_MAIN_DRIVER
  199. int main(int argc, char** argv)
  200. {
  201. func_args args;
  202. #ifdef HAVE_CAVIUM
  203. int ret = OpenNitroxDevice(CAVIUM_DIRECT, CAVIUM_DEV_ID);
  204. if (ret != 0)
  205. err_sys("Cavium OpenNitroxDevice failed");
  206. #endif /* HAVE_CAVIUM */
  207. StartTCP();
  208. args.argc = argc;
  209. args.argv = argv;
  210. CyaSSL_Init();
  211. #if defined(DEBUG_CYASSL) && !defined(CYASSL_MDK_SHELL)
  212. CyaSSL_Debugging_ON();
  213. #endif
  214. #ifndef CYASSL_TIRTOS
  215. if (CurrentDir("echoclient"))
  216. ChangeDirBack(2);
  217. else if (CurrentDir("Debug") || CurrentDir("Release"))
  218. ChangeDirBack(3);
  219. #endif
  220. echoclient_test(&args);
  221. CyaSSL_Cleanup();
  222. #ifdef HAVE_CAVIUM
  223. CspShutdown(CAVIUM_DEV_ID);
  224. #endif
  225. return args.return_code;
  226. }
  227. #endif /* NO_MAIN_DRIVER */