echoclient.c 10 KB

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