2
0

echoserver.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /* echoserver.c
  2. *
  3. * Copyright (C) 2006-2022 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/ssl.h> /* name change portability layer */
  25. #include <cyassl/ctaocrypt/settings.h>
  26. #ifdef HAVE_ECC
  27. #include <cyassl/ctaocrypt/ecc.h> /* ecc_fp_free */
  28. #endif
  29. #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include "cmsis_os.h"
  33. #include "rl_fs.h"
  34. #include "rl_net.h"
  35. #include "wolfssl_MDK_ARM.h"
  36. #endif
  37. #include <cyassl/ssl.h>
  38. #include <cyassl/test.h>
  39. #ifndef NO_MAIN_DRIVER
  40. #define ECHO_OUT
  41. #endif
  42. #include "examples/echoserver/echoserver.h"
  43. #ifndef NO_WOLFSSL_SERVER
  44. #ifdef NO_FILESYSTEM
  45. #ifdef NO_RSA
  46. #error currently the example only tries to load in a RSA buffer
  47. #endif
  48. #undef USE_CERT_BUFFERS_2048
  49. #define USE_CERT_BUFFERS_2048
  50. #include <wolfssl/certs_test.h>
  51. #endif
  52. #ifdef WOLFSSL_ASYNC_CRYPT
  53. static int devId = INVALID_DEVID;
  54. #endif
  55. #define SVR_COMMAND_SIZE 256
  56. static void SignalReady(void* args, word16 port)
  57. {
  58. #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__)
  59. /* signal ready to tcp_accept */
  60. func_args* server_args = (func_args*)args;
  61. tcp_ready* ready = server_args->signal;
  62. PTHREAD_CHECK_RET(pthread_mutex_lock(&ready->mutex));
  63. ready->ready = 1;
  64. ready->port = port;
  65. PTHREAD_CHECK_RET(pthread_cond_signal(&ready->cond));
  66. PTHREAD_CHECK_RET(pthread_mutex_unlock(&ready->mutex));
  67. #endif
  68. (void)args;
  69. (void)port;
  70. }
  71. THREAD_RETURN CYASSL_THREAD echoserver_test(void* args)
  72. {
  73. SOCKET_T sockfd = 0;
  74. CYASSL_METHOD* method = 0;
  75. CYASSL_CTX* ctx = 0;
  76. int ret = 0;
  77. int doDTLS = 0;
  78. int doPSK;
  79. int outCreated = 0;
  80. int shutDown = 0;
  81. int useAnyAddr = 0;
  82. word16 port;
  83. int argc = ((func_args*)args)->argc;
  84. char** argv = ((func_args*)args)->argv;
  85. char buffer[CYASSL_MAX_ERROR_SZ];
  86. #ifdef HAVE_TEST_SESSION_TICKET
  87. MyTicketCtx myTicketCtx;
  88. #endif
  89. #ifdef ECHO_OUT
  90. FILE* fout = stdout;
  91. if (argc >= 2) {
  92. fout = fopen(argv[1], "w");
  93. outCreated = 1;
  94. }
  95. if (!fout) err_sys("can't open output file");
  96. #endif
  97. (void)outCreated;
  98. (void)argc;
  99. (void)argv;
  100. ((func_args*)args)->return_code = -1; /* error state */
  101. #ifdef CYASSL_DTLS
  102. doDTLS = 1;
  103. #endif
  104. #if (defined(NO_RSA) && !defined(HAVE_ECC) && !defined(HAVE_ED25519) && \
  105. !defined(HAVE_ED448)) || defined(CYASSL_LEANPSK)
  106. doPSK = 1;
  107. #else
  108. doPSK = 0;
  109. #endif
  110. #if defined(NO_MAIN_DRIVER) && !defined(CYASSL_SNIFFER) && \
  111. !defined(WOLFSSL_MDK_SHELL) && !defined(CYASSL_TIRTOS) && \
  112. !defined(USE_WINDOWS_API)
  113. /* Let tcp_listen assign port */
  114. port = 0;
  115. #else
  116. /* Use default port */
  117. port = wolfSSLPort;
  118. #endif
  119. #if defined(USE_ANY_ADDR)
  120. useAnyAddr = 1;
  121. #endif
  122. #ifdef CYASSL_TIRTOS
  123. fdOpenSession(Task_self());
  124. #endif
  125. tcp_listen(&sockfd, &port, useAnyAddr, doDTLS, 0);
  126. #if defined(CYASSL_DTLS)
  127. #ifdef WOLFSSL_DTLS13
  128. method = wolfDTLSv1_3_server_method();
  129. #elif !defined(WOLFSSL_NO_TLS12)
  130. method = CyaDTLSv1_2_server_method();
  131. #endif
  132. #elif !defined(NO_TLS)
  133. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_SNIFFER)
  134. method = CyaTLSv1_2_server_method();
  135. #else
  136. method = CyaSSLv23_server_method();
  137. #endif
  138. #elif defined(WOLFSSL_ALLOW_SSLV3)
  139. method = CyaSSLv3_server_method();
  140. #else
  141. #error "no valid server method built in"
  142. #endif
  143. ctx = CyaSSL_CTX_new(method);
  144. /* CyaSSL_CTX_set_session_cache_mode(ctx, WOLFSSL_SESS_CACHE_OFF); */
  145. #ifdef WOLFSSL_ENCRYPTED_KEYS
  146. CyaSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  147. #endif
  148. #ifdef HAVE_TEST_SESSION_TICKET
  149. if (TicketInit() != 0)
  150. err_sys("unable to setup Session Ticket Key context");
  151. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  152. XMEMSET(&myTicketCtx, 0, sizeof(myTicketCtx));
  153. wolfSSL_CTX_set_TicketEncCtx(ctx, &myTicketCtx);
  154. #endif
  155. #ifndef NO_FILESYSTEM
  156. if (doPSK == 0) {
  157. #if defined(HAVE_ECC) && !defined(CYASSL_SNIFFER)
  158. /* ecc */
  159. if (CyaSSL_CTX_use_certificate_file(ctx, eccCertFile, WOLFSSL_FILETYPE_PEM)
  160. != WOLFSSL_SUCCESS)
  161. err_sys("can't load server cert file, "
  162. "Please run from wolfSSL home dir");
  163. if (CyaSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, WOLFSSL_FILETYPE_PEM)
  164. != WOLFSSL_SUCCESS)
  165. err_sys("can't load server key file, "
  166. "Please run from wolfSSL home dir");
  167. #elif defined(HAVE_ED25519) && !defined(CYASSL_SNIFFER)
  168. /* ed25519 */
  169. if (CyaSSL_CTX_use_certificate_chain_file(ctx, edCertFile)
  170. != WOLFSSL_SUCCESS)
  171. err_sys("can't load server cert file, "
  172. "Please run from wolfSSL home dir");
  173. if (CyaSSL_CTX_use_PrivateKey_file(ctx, edKeyFile, WOLFSSL_FILETYPE_PEM)
  174. != WOLFSSL_SUCCESS)
  175. err_sys("can't load server key file, "
  176. "Please run from wolfSSL home dir");
  177. #elif defined(HAVE_ED448) && !defined(CYASSL_SNIFFER)
  178. /* ed448 */
  179. if (CyaSSL_CTX_use_certificate_chain_file(ctx, ed448CertFile)
  180. != WOLFSSL_SUCCESS)
  181. err_sys("can't load server cert file, "
  182. "Please run from wolfSSL home dir");
  183. if (CyaSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  184. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  185. err_sys("can't load server key file, "
  186. "Please run from wolfSSL home dir");
  187. #elif defined(NO_CERTS)
  188. /* do nothing, just don't load cert files */
  189. #else
  190. /* normal */
  191. if (CyaSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM)
  192. != WOLFSSL_SUCCESS)
  193. err_sys("can't load server cert file, "
  194. "Please run from wolfSSL home dir");
  195. if (CyaSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM)
  196. != WOLFSSL_SUCCESS)
  197. err_sys("can't load server key file, "
  198. "Please run from wolfSSL home dir");
  199. #endif
  200. } /* doPSK */
  201. #elif !defined(NO_CERTS)
  202. if (!doPSK) {
  203. if (CyaSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  204. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1)
  205. != WOLFSSL_SUCCESS)
  206. err_sys("can't load server cert buffer");
  207. if (CyaSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  208. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1)
  209. != WOLFSSL_SUCCESS)
  210. err_sys("can't load server key buffer");
  211. }
  212. #endif
  213. #if defined(CYASSL_SNIFFER)
  214. /* Only set if not running testsuite */
  215. if (XSTRSTR(argv[0], "testsuite") != 0) {
  216. /* don't use EDH, can't sniff tmp keys */
  217. CyaSSL_CTX_set_cipher_list(ctx, "AES256-SHA");
  218. }
  219. #endif
  220. if (doPSK) {
  221. #ifndef NO_PSK
  222. const char *defaultCipherList;
  223. CyaSSL_CTX_set_psk_server_callback(ctx, my_psk_server_cb);
  224. CyaSSL_CTX_use_psk_identity_hint(ctx, "cyassl server");
  225. #ifdef HAVE_NULL_CIPHER
  226. defaultCipherList = "PSK-NULL-SHA256";
  227. #elif defined(HAVE_AESGCM) && !defined(NO_DH)
  228. #ifdef WOLFSSL_TLS13
  229. defaultCipherList = "TLS13-AES128-GCM-SHA256"
  230. #ifndef WOLFSSL_NO_TLS12
  231. ":DHE-PSK-AES128-GCM-SHA256"
  232. #endif
  233. ;
  234. #else
  235. defaultCipherList = "DHE-PSK-AES128-GCM-SHA256";
  236. #endif
  237. #elif defined(HAVE_AESGCM) && defined(WOLFSSL_TLS13)
  238. defaultCipherList = "TLS13-AES128-GCM-SHA256"
  239. #ifndef WOLFSSL_NO_TLS12
  240. ":PSK-AES128-GCM-SHA256"
  241. #endif
  242. ;
  243. #else
  244. defaultCipherList = "PSK-AES128-CBC-SHA256";
  245. #endif
  246. if (CyaSSL_CTX_set_cipher_list(ctx, defaultCipherList) != WOLFSSL_SUCCESS)
  247. err_sys("server can't set cipher list 2");
  248. wolfSSL_CTX_set_psk_callback_ctx(ctx, (void*)defaultCipherList);
  249. #endif
  250. }
  251. #ifdef WOLFSSL_ASYNC_CRYPT
  252. ret = wolfAsync_DevOpen(&devId);
  253. if (ret < 0) {
  254. fprintf(stderr, "Async device open failed\nRunning without async\n");
  255. }
  256. wolfSSL_CTX_SetDevId(ctx, devId);
  257. #endif /* WOLFSSL_ASYNC_CRYPT */
  258. SignalReady(args, port);
  259. while (!shutDown) {
  260. CYASSL* ssl = NULL;
  261. CYASSL* write_ssl = NULL; /* may have separate w/ HAVE_WRITE_DUP */
  262. char command[SVR_COMMAND_SIZE+1];
  263. int echoSz = 0;
  264. int clientfd;
  265. int firstRead = 1;
  266. int gotFirstG = 0;
  267. int err = 0;
  268. SOCKADDR_IN_T client;
  269. socklen_t client_len = sizeof(client);
  270. #ifndef CYASSL_DTLS
  271. clientfd = accept(sockfd, (struct sockaddr*)&client,
  272. (ACCEPT_THIRD_T)&client_len);
  273. #else
  274. clientfd = sockfd;
  275. {
  276. /* For DTLS, peek at the next datagram so we can get the client's
  277. * address and set it into the ssl object later to generate the
  278. * cookie. */
  279. int n;
  280. byte b[1500];
  281. n = (int)recvfrom(clientfd, (char*)b, sizeof(b), MSG_PEEK,
  282. (struct sockaddr*)&client, &client_len);
  283. if (n <= 0)
  284. err_sys("recvfrom failed");
  285. }
  286. #endif
  287. if (WOLFSSL_SOCKET_IS_INVALID(clientfd)) err_sys("tcp accept failed");
  288. ssl = CyaSSL_new(ctx);
  289. if (ssl == NULL) err_sys("SSL_new failed");
  290. CyaSSL_set_fd(ssl, clientfd);
  291. #ifdef CYASSL_DTLS
  292. wolfSSL_dtls_set_peer(ssl, &client, client_len);
  293. #endif
  294. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && !defined(NO_ASN)
  295. CyaSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  296. #elif !defined(NO_DH)
  297. SetDH(ssl); /* will repick suites with DHE, higher than PSK */
  298. #endif
  299. do {
  300. err = 0; /* Reset error */
  301. ret = CyaSSL_accept(ssl);
  302. if (ret != WOLFSSL_SUCCESS) {
  303. err = CyaSSL_get_error(ssl, 0);
  304. #ifdef WOLFSSL_ASYNC_CRYPT
  305. if (err == WC_PENDING_E) {
  306. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  307. if (ret < 0) break;
  308. }
  309. #endif
  310. }
  311. } while (err == WC_PENDING_E);
  312. if (ret != WOLFSSL_SUCCESS) {
  313. fprintf(stderr, "SSL_accept error = %d, %s\n", err,
  314. CyaSSL_ERR_error_string(err, buffer));
  315. fprintf(stderr, "SSL_accept failed\n");
  316. CyaSSL_free(ssl);
  317. CloseSocket(clientfd);
  318. continue;
  319. }
  320. #if defined(PEER_INFO)
  321. showPeer(ssl);
  322. #endif
  323. #ifdef HAVE_WRITE_DUP
  324. write_ssl = wolfSSL_write_dup(ssl);
  325. if (write_ssl == NULL) {
  326. fprintf(stderr, "wolfSSL_write_dup failed\n");
  327. CyaSSL_free(ssl);
  328. CloseSocket(clientfd);
  329. continue;
  330. }
  331. #else
  332. write_ssl = ssl;
  333. #endif
  334. while (1) {
  335. do {
  336. err = 0; /* reset error */
  337. ret = CyaSSL_read(ssl, command, sizeof(command)-1);
  338. if (ret <= 0) {
  339. err = CyaSSL_get_error(ssl, 0);
  340. #ifdef WOLFSSL_ASYNC_CRYPT
  341. if (err == WC_PENDING_E) {
  342. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  343. if (ret < 0) break;
  344. }
  345. #endif
  346. }
  347. } while (err == WC_PENDING_E);
  348. if (ret <= 0) {
  349. if (err != WOLFSSL_ERROR_WANT_READ && err != WOLFSSL_ERROR_ZERO_RETURN){
  350. fprintf(stderr, "SSL_read echo error %d, %s!\n", err,
  351. CyaSSL_ERR_error_string(err, buffer));
  352. }
  353. break;
  354. }
  355. echoSz = ret;
  356. if (firstRead == 1) {
  357. firstRead = 0; /* browser may send 1 byte 'G' to start */
  358. if (echoSz == 1 && command[0] == 'G') {
  359. gotFirstG = 1;
  360. continue;
  361. }
  362. }
  363. else if (gotFirstG == 1 && strncmp(command, "ET /", 4) == 0) {
  364. strncpy(command, "GET", 4);
  365. /* fall through to normal GET */
  366. }
  367. if ( strncmp(command, "quit", 4) == 0) {
  368. printf("client sent quit command: shutting down!\n");
  369. shutDown = 1;
  370. break;
  371. }
  372. if ( strncmp(command, "break", 5) == 0) {
  373. printf("client sent break command: closing session!\n");
  374. break;
  375. }
  376. #ifdef PRINT_SESSION_STATS
  377. if ( strncmp(command, "printstats", 10) == 0) {
  378. CyaSSL_PrintSessionStats();
  379. break;
  380. }
  381. #endif
  382. if (strncmp(command, "GET", 3) == 0) {
  383. const char resp[] =
  384. "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n"
  385. "<html><body BGCOLOR=\"#ffffff\"><pre>\r\n"
  386. "greetings from wolfSSL\r\n</pre></body></html>\r\n\r\n";
  387. echoSz = (int)strlen(resp) + 1;
  388. if (echoSz > (int)sizeof(command)) {
  389. /* Internal error. */
  390. err_sys("HTTP response greater than buffer.");
  391. }
  392. strncpy(command, resp, sizeof(command));
  393. do {
  394. err = 0; /* reset error */
  395. ret = CyaSSL_write(write_ssl, command, echoSz);
  396. if (ret <= 0) {
  397. err = CyaSSL_get_error(write_ssl, 0);
  398. #ifdef WOLFSSL_ASYNC_CRYPT
  399. if (err == WC_PENDING_E) {
  400. ret = wolfSSL_AsyncPoll(write_ssl, WOLF_POLL_FLAG_CHECK_HW);
  401. if (ret < 0) break;
  402. }
  403. #endif
  404. }
  405. } while (err == WC_PENDING_E);
  406. if (ret != echoSz) {
  407. fprintf(stderr, "SSL_write get error = %d, %s\n", err,
  408. CyaSSL_ERR_error_string(err, buffer));
  409. err_sys("SSL_write get failed");
  410. }
  411. break;
  412. }
  413. command[echoSz] = 0;
  414. #ifdef ECHO_OUT
  415. LIBCALL_CHECK_RET(fputs(command, fout));
  416. #endif
  417. do {
  418. err = 0; /* reset error */
  419. ret = CyaSSL_write(write_ssl, command, echoSz);
  420. if (ret <= 0) {
  421. err = CyaSSL_get_error(write_ssl, 0);
  422. #ifdef WOLFSSL_ASYNC_CRYPT
  423. if (err == WC_PENDING_E) {
  424. ret = wolfSSL_AsyncPoll(write_ssl, WOLF_POLL_FLAG_CHECK_HW);
  425. if (ret < 0) break;
  426. }
  427. #endif
  428. }
  429. } while (err == WC_PENDING_E);
  430. if (ret != echoSz) {
  431. fprintf(stderr, "SSL_write echo error = %d, %s\n", err,
  432. CyaSSL_ERR_error_string(err, buffer));
  433. err_sys("SSL_write echo failed");
  434. }
  435. }
  436. #ifndef CYASSL_DTLS
  437. CyaSSL_shutdown(ssl);
  438. #endif
  439. #ifdef HAVE_WRITE_DUP
  440. CyaSSL_free(write_ssl);
  441. #endif
  442. CyaSSL_free(ssl);
  443. CloseSocket(clientfd);
  444. #ifdef CYASSL_DTLS
  445. tcp_listen(&sockfd, &port, useAnyAddr, doDTLS, 0);
  446. SignalReady(args, port);
  447. #endif
  448. }
  449. CloseSocket(sockfd);
  450. CyaSSL_CTX_free(ctx);
  451. #ifdef ECHO_OUT
  452. if (outCreated)
  453. fclose(fout);
  454. #endif
  455. ((func_args*)args)->return_code = 0;
  456. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  457. && defined(HAVE_THREAD_LS)
  458. ecc_fp_free(); /* free per thread cache */
  459. #endif
  460. #ifdef CYASSL_TIRTOS
  461. fdCloseSession(Task_self());
  462. #endif
  463. #ifdef HAVE_TEST_SESSION_TICKET
  464. TicketCleanup();
  465. #endif
  466. #ifdef WOLFSSL_ASYNC_CRYPT
  467. wolfAsync_DevClose(&devId);
  468. #endif
  469. #ifndef CYASSL_TIRTOS
  470. return 0;
  471. #endif
  472. }
  473. #endif /* !NO_WOLFSSL_SERVER */
  474. /* so overall tests can pull in test function */
  475. #ifndef NO_MAIN_DRIVER
  476. int main(int argc, char** argv)
  477. {
  478. func_args args;
  479. #ifdef HAVE_WNR
  480. if (wc_InitNetRandom(wnrConfig, NULL, 5000) != 0)
  481. err_sys("Whitewood netRandom global config failed");
  482. #endif
  483. StartTCP();
  484. args.argc = argc;
  485. args.argv = argv;
  486. args.return_code = 0;
  487. CyaSSL_Init();
  488. #if defined(DEBUG_CYASSL) && !defined(CYASSL_MDK_SHELL)
  489. CyaSSL_Debugging_ON();
  490. #endif
  491. ChangeToWolfRoot();
  492. #ifndef NO_WOLFSSL_SERVER
  493. echoserver_test(&args);
  494. #endif
  495. CyaSSL_Cleanup();
  496. #ifdef HAVE_WNR
  497. if (wc_FreeNetRandom() < 0)
  498. err_sys("Failed to free netRandom context");
  499. #endif /* HAVE_WNR */
  500. return args.return_code;
  501. }
  502. #endif /* NO_MAIN_DRIVER */