echoclient.c 11 KB

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