echoclient.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* echoclient.c
  2. *
  3. * Copyright (C) 2006-2020 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL 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. * wolfSSL 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-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <cyassl/ctaocrypt/settings.h>
  25. /* let's use cyassl layer AND cyassl openssl layer */
  26. #include <cyassl/ssl.h>
  27. #include <cyassl/openssl/ssl.h>
  28. #ifdef CYASSL_DTLS
  29. #include <cyassl/error-ssl.h>
  30. #endif
  31. #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include "cmsis_os.h"
  35. #include "rl_fs.h"
  36. #include "rl_net.h"
  37. #include "wolfssl_MDK_ARM.h"
  38. #endif
  39. #include <cyassl/test.h>
  40. #include <examples/echoclient/echoclient.h>
  41. #ifndef NO_WOLFSSL_CLIENT
  42. #ifdef WOLFSSL_ASYNC_CRYPT
  43. static int devId = INVALID_DEVID;
  44. #endif
  45. void echoclient_test(void* args)
  46. {
  47. SOCKET_T sockfd = 0;
  48. FILE* fin = stdin ;
  49. FILE* fout = stdout;
  50. int inCreated = 0;
  51. int outCreated = 0;
  52. char msg[1024];
  53. char reply[1024+1];
  54. SSL_METHOD* method = 0;
  55. SSL_CTX* ctx = 0;
  56. SSL* ssl = 0;
  57. int ret = 0, err = 0;
  58. int doDTLS = 0;
  59. int doPSK = 0;
  60. int sendSz;
  61. #ifndef WOLFSSL_MDK_SHELL
  62. int argc = 0;
  63. char** argv = 0;
  64. #endif
  65. word16 port = yasslPort;
  66. char buffer[CYASSL_MAX_ERROR_SZ];
  67. ((func_args*)args)->return_code = -1; /* error state */
  68. #ifndef WOLFSSL_MDK_SHELL
  69. argc = ((func_args*)args)->argc;
  70. argv = ((func_args*)args)->argv;
  71. if (argc >= 2) {
  72. fin = fopen(argv[1], "r");
  73. inCreated = 1;
  74. }
  75. if (argc >= 3) {
  76. fout = fopen(argv[2], "w");
  77. outCreated = 1;
  78. }
  79. #endif
  80. if (!fin) err_sys("can't open input file");
  81. if (!fout) err_sys("can't open output file");
  82. #ifdef CYASSL_DTLS
  83. doDTLS = 1;
  84. #endif
  85. #ifdef CYASSL_LEANPSK
  86. doPSK = 1;
  87. #endif
  88. #if defined(NO_RSA) && !defined(HAVE_ECC) && !defined(HAVE_ED25519)
  89. doPSK = 1;
  90. #endif
  91. #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) && !defined(WOLFSSL_MDK_SHELL)
  92. port = ((func_args*)args)->signal->port;
  93. #endif
  94. #if defined(CYASSL_DTLS)
  95. method = DTLSv1_2_client_method();
  96. #elif !defined(NO_TLS)
  97. method = CyaSSLv23_client_method();
  98. #elif defined(WOLFSSL_ALLOW_SSLV3)
  99. method = SSLv3_client_method();
  100. #else
  101. #error "no valid client method type"
  102. #endif
  103. ctx = SSL_CTX_new(method);
  104. #ifndef NO_FILESYSTEM
  105. #ifndef NO_RSA
  106. if (SSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  107. err_sys("can't load ca file, Please run from wolfSSL home dir");
  108. #endif
  109. #ifdef HAVE_ECC
  110. if (SSL_CTX_load_verify_locations(ctx, caEccCertFile, 0) != WOLFSSL_SUCCESS)
  111. err_sys("can't load ca file, Please run from wolfSSL home dir");
  112. #elif defined(HAVE_ED25519)
  113. if (SSL_CTX_load_verify_locations(ctx, caEdCertFile, 0) != WOLFSSL_SUCCESS)
  114. err_sys("can't load ca file, Please run from wolfSSL home dir");
  115. #endif
  116. #elif !defined(NO_CERTS)
  117. if (!doPSK)
  118. load_buffer(ctx, caCertFile, WOLFSSL_CA);
  119. #endif
  120. #if defined(CYASSL_SNIFFER)
  121. /* don't use EDH, can't sniff tmp keys */
  122. SSL_CTX_set_cipher_list(ctx, "AES256-SHA");
  123. #endif
  124. if (doPSK) {
  125. #ifndef NO_PSK
  126. const char *defaultCipherList;
  127. CyaSSL_CTX_set_psk_client_callback(ctx, my_psk_client_cb);
  128. #ifdef HAVE_NULL_CIPHER
  129. defaultCipherList = "PSK-NULL-SHA256";
  130. #elif defined(HAVE_AESGCM) && !defined(NO_DH)
  131. defaultCipherList = "DHE-PSK-AES128-GCM-SHA256";
  132. #else
  133. defaultCipherList = "PSK-AES128-CBC-SHA256";
  134. #endif
  135. if (CyaSSL_CTX_set_cipher_list(ctx,defaultCipherList) !=WOLFSSL_SUCCESS)
  136. err_sys("client can't set cipher list 2");
  137. #endif
  138. }
  139. #ifdef WOLFSSL_ENCRYPTED_KEYS
  140. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  141. #endif
  142. #if defined(WOLFSSL_MDK_ARM)
  143. CyaSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, 0);
  144. #endif
  145. #ifdef WOLFSSL_ASYNC_CRYPT
  146. ret = wolfAsync_DevOpen(&devId);
  147. if (ret < 0) {
  148. printf("Async device open failed\nRunning without async\n");
  149. }
  150. wolfSSL_CTX_UseAsync(ctx, devId);
  151. #endif /* WOLFSSL_ASYNC_CRYPT */
  152. ssl = SSL_new(ctx);
  153. tcp_connect(&sockfd, yasslIP, port, doDTLS, 0, ssl);
  154. SSL_set_fd(ssl, sockfd);
  155. #if defined(USE_WINDOWS_API) && defined(CYASSL_DTLS) && defined(NO_MAIN_DRIVER)
  156. /* let echoserver bind first, TODO: add Windows signal like pthreads does */
  157. Sleep(100);
  158. #endif
  159. do {
  160. err = 0; /* Reset error */
  161. ret = SSL_connect(ssl);
  162. if (ret != WOLFSSL_SUCCESS) {
  163. err = SSL_get_error(ssl, 0);
  164. #ifdef WOLFSSL_ASYNC_CRYPT
  165. if (err == WC_PENDING_E) {
  166. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  167. if (ret < 0) break;
  168. }
  169. #endif
  170. }
  171. } while (err == WC_PENDING_E);
  172. if (ret != WOLFSSL_SUCCESS) {
  173. printf("SSL_connect error %d, %s\n", err,
  174. ERR_error_string(err, buffer));
  175. err_sys("SSL_connect failed");
  176. }
  177. while (fgets(msg, sizeof(msg), fin) != 0) {
  178. sendSz = (int)XSTRLEN(msg);
  179. do {
  180. err = 0; /* reset error */
  181. ret = SSL_write(ssl, msg, sendSz);
  182. if (ret <= 0) {
  183. err = SSL_get_error(ssl, 0);
  184. #ifdef WOLFSSL_ASYNC_CRYPT
  185. if (err == WC_PENDING_E) {
  186. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  187. if (ret < 0) break;
  188. }
  189. #endif
  190. }
  191. } while (err == WC_PENDING_E);
  192. if (ret != sendSz) {
  193. printf("SSL_write msg error %d, %s\n", err,
  194. ERR_error_string(err, buffer));
  195. err_sys("SSL_write failed");
  196. }
  197. if (strncmp(msg, "quit", 4) == 0) {
  198. fputs("sending server shutdown command: quit!\n", fout);
  199. break;
  200. }
  201. if (strncmp(msg, "break", 5) == 0) {
  202. fputs("sending server session close: break!\n", fout);
  203. break;
  204. }
  205. #ifndef WOLFSSL_MDK_SHELL
  206. while (sendSz)
  207. #endif
  208. {
  209. do {
  210. err = 0; /* reset error */
  211. ret = SSL_read(ssl, reply, sizeof(reply)-1);
  212. if (ret <= 0) {
  213. err = SSL_get_error(ssl, 0);
  214. #ifdef WOLFSSL_ASYNC_CRYPT
  215. if (err == WC_PENDING_E) {
  216. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  217. if (ret < 0) break;
  218. }
  219. #endif
  220. }
  221. } while (err == WC_PENDING_E);
  222. if (ret > 0) {
  223. reply[ret] = 0;
  224. fputs(reply, fout);
  225. fflush(fout) ;
  226. sendSz -= ret;
  227. }
  228. #ifdef CYASSL_DTLS
  229. else if (wolfSSL_dtls(ssl) && err == DECRYPT_ERROR) {
  230. /* This condition is OK. The packet should be dropped
  231. * silently when there is a decrypt or MAC error on
  232. * a DTLS record. */
  233. sendSz = 0;
  234. }
  235. #endif
  236. else {
  237. printf("SSL_read msg error %d, %s\n", err,
  238. ERR_error_string(err, buffer));
  239. err_sys("SSL_read failed");
  240. #ifndef WOLFSSL_MDK_SHELL
  241. break;
  242. #endif
  243. }
  244. }
  245. }
  246. #ifdef CYASSL_DTLS
  247. strncpy(msg, "break", 6);
  248. sendSz = (int)strlen(msg);
  249. /* try to tell server done */
  250. do {
  251. err = 0; /* reset error */
  252. ret = SSL_write(ssl, msg, sendSz);
  253. if (ret <= 0) {
  254. err = SSL_get_error(ssl, 0);
  255. #ifdef WOLFSSL_ASYNC_CRYPT
  256. if (err == WC_PENDING_E) {
  257. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  258. if (ret < 0) break;
  259. }
  260. #endif
  261. }
  262. } while (err == WC_PENDING_E);
  263. #else
  264. SSL_shutdown(ssl);
  265. #endif
  266. SSL_free(ssl);
  267. SSL_CTX_free(ctx);
  268. #ifdef WOLFSSL_ASYNC_CRYPT
  269. wolfAsync_DevClose(&devId);
  270. #endif
  271. fflush(fout);
  272. if (inCreated) fclose(fin);
  273. if (outCreated) fclose(fout);
  274. CloseSocket(sockfd);
  275. ((func_args*)args)->return_code = 0;
  276. }
  277. #endif /* !NO_WOLFSSL_CLIENT */
  278. /* so overall tests can pull in test function */
  279. #ifndef NO_MAIN_DRIVER
  280. int main(int argc, char** argv)
  281. {
  282. func_args args;
  283. #ifdef HAVE_WNR
  284. if (wc_InitNetRandom(wnrConfig, NULL, 5000) != 0)
  285. err_sys("Whitewood netRandom global config failed");
  286. #endif
  287. StartTCP();
  288. args.argc = argc;
  289. args.argv = argv;
  290. args.return_code = 0;
  291. CyaSSL_Init();
  292. #if defined(DEBUG_CYASSL) && !defined(WOLFSSL_MDK_SHELL)
  293. CyaSSL_Debugging_ON();
  294. #endif
  295. #ifndef CYASSL_TIRTOS
  296. ChangeToWolfRoot();
  297. #endif
  298. #ifndef NO_WOLFSSL_CLIENT
  299. echoclient_test(&args);
  300. #endif
  301. CyaSSL_Cleanup();
  302. #ifdef HAVE_WNR
  303. if (wc_FreeNetRandom() < 0)
  304. err_sys("Failed to free netRandom context");
  305. #endif /* HAVE_WNR */
  306. return args.return_code;
  307. }
  308. #endif /* NO_MAIN_DRIVER */