1
0

async_server.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* async_server.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL. (formerly known as CyaSSL)
  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-1301, USA
  20. */
  21. /* TLS server demonstrating asynchronous cryptography features and optionally
  22. * using the crypto or PK callbacks */
  23. /* std */
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. /* socket */
  28. #include <sys/socket.h>
  29. #include <arpa/inet.h>
  30. #include <netinet/in.h>
  31. #include <unistd.h>
  32. #define HAVE_SIGNAL
  33. #ifdef HAVE_SIGNAL
  34. #include <signal.h> /* for catching ctrl+c */
  35. #endif
  36. /* wolfSSL */
  37. #ifndef WOLFSSL_USER_SETTINGS
  38. #include <wolfssl/options.h>
  39. #endif
  40. #include <wolfssl/ssl.h>
  41. #include <wolfssl/wolfio.h>
  42. #include <wolfssl/wolfcrypt/error-crypt.h>
  43. #include "examples/async/async_tls.h"
  44. /* Test certificates and keys for RSA and ECC */
  45. #ifndef NO_RSA
  46. #define CERT_FILE "./certs/server-cert.pem"
  47. #define KEY_FILE "./certs/server-key.pem"
  48. #define CA_FILE "./certs/client-cert.pem"
  49. #elif defined(HAVE_ECC)
  50. #define CERT_FILE "./certs/server-ecc.pem"
  51. #define KEY_FILE "./certs/ecc-key.pem"
  52. #define CA_FILE "./certs/client-ecc-cert.pem"
  53. #else
  54. #error No authentication algorithm (ECC/RSA)
  55. #endif
  56. static int mSockfd = SOCKET_INVALID;
  57. static int mConnd = SOCKET_INVALID;
  58. static int mShutdown = 0;
  59. #ifdef HAVE_SIGNAL
  60. static void sig_handler(const int sig)
  61. {
  62. #ifdef DEBUG_WOLFSSL
  63. fprintf(stderr, "SIGINT handled = %d.\n", sig);
  64. #else
  65. (void)sig;
  66. #endif
  67. mShutdown = 1;
  68. if (mConnd != SOCKET_INVALID) {
  69. close(mConnd); /* Close the connection to the client */
  70. mConnd = SOCKET_INVALID;
  71. }
  72. if (mSockfd != SOCKET_INVALID) {
  73. close(mSockfd); /* Close the socket listening for clients */
  74. mSockfd = SOCKET_INVALID;
  75. }
  76. }
  77. #endif
  78. int server_async_test(int argc, char** argv)
  79. {
  80. int ret = 0;
  81. struct sockaddr_in servAddr;
  82. struct sockaddr_in clientAddr;
  83. socklen_t size = sizeof(clientAddr);
  84. char buff[TEST_BUF_SZ];
  85. size_t len;
  86. const char* reply = "I hear ya fa shizzle!\n";
  87. int on;
  88. int devId = 1; /* anything besides -2 (INVALID_DEVID) */
  89. #ifdef WOLF_CRYPTO_CB
  90. AsyncTlsCryptoCbCtx myCtx;
  91. #endif
  92. int err;
  93. char errBuff[WOLFSSL_MAX_ERROR_SZ];
  94. /* declare wolfSSL objects */
  95. WOLFSSL_CTX* ctx = NULL;
  96. WOLFSSL* ssl = NULL;
  97. #ifdef HAVE_SIGNAL
  98. if ((signal(SIGINT, sig_handler)) == SIG_ERR) {
  99. fprintf(stderr, "ERROR: failed to listen to SIGINT (errno: %d)\n",errno);
  100. goto exit;
  101. }
  102. #endif
  103. /* Initialize the server address struct with zeros */
  104. memset(&servAddr, 0, sizeof(servAddr));
  105. /* Fill in the server address */
  106. servAddr.sin_family = AF_INET; /* using IPv4 */
  107. servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */
  108. servAddr.sin_addr.s_addr = INADDR_ANY; /* from anywhere */
  109. /* Create a socket that uses an internet IPv4 address,
  110. * Sets the socket to be stream based (TCP),
  111. * 0 means choose the default protocol. */
  112. if ((mSockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  113. fprintf(stderr, "ERROR: failed to create the socket\n");
  114. goto exit;
  115. }
  116. /* make sure server is setup for reuse addr/port */
  117. on = 1;
  118. if (setsockopt(mSockfd, SOL_SOCKET, SO_REUSEADDR,
  119. (char*)&on, (socklen_t)sizeof(on)) != 0) {
  120. fprintf(stderr, "ERROR: failed to set SO_REUSEADDR (errno: %d)\n",errno);
  121. goto exit;
  122. }
  123. #ifdef SO_REUSEPORT
  124. if (setsockopt(mSockfd, SOL_SOCKET, SO_REUSEPORT,
  125. (char*)&on, (socklen_t)sizeof(on)) != 0) {
  126. fprintf(stderr, "ERROR: failed to set SO_REUSEPORT (errno: %d)\n",errno);
  127. goto exit;
  128. }
  129. #endif
  130. /* Bind the server socket to our port */
  131. if (bind(mSockfd, (struct sockaddr*)&servAddr, sizeof(servAddr)) == -1) {
  132. fprintf(stderr, "ERROR: failed to bind\n");
  133. goto exit;
  134. }
  135. /* Listen for a new connection, allow 5 pending connections */
  136. if (listen(mSockfd, 5) == -1) {
  137. fprintf(stderr, "ERROR: failed to listen\n");
  138. goto exit;
  139. }
  140. /*---------------------------------*/
  141. /* Start of wolfSSL initialization and configuration */
  142. /*---------------------------------*/
  143. #ifdef DEBUG_WOLFSSL
  144. wolfSSL_Debugging_ON();
  145. #endif
  146. /* Initialize wolfSSL */
  147. if ((ret = wolfSSL_Init()) != WOLFSSL_SUCCESS) {
  148. fprintf(stderr, "ERROR: Failed to initialize the library\n");
  149. goto exit;
  150. }
  151. /* Create and initialize WOLFSSL_CTX */
  152. if ((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())) == NULL) {
  153. fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
  154. ret = -1;
  155. goto exit;
  156. }
  157. #ifdef WOLF_CRYPTO_CB
  158. XMEMSET(&myCtx, 0, sizeof(myCtx));
  159. /* register a devID for crypto callbacks */
  160. ret = wc_CryptoCb_RegisterDevice(devId, AsyncTlsCryptoCb, &myCtx);
  161. if (ret != 0) {
  162. fprintf(stderr, "wc_CryptoCb_RegisterDevice: error %d", ret);
  163. goto exit;
  164. }
  165. #endif
  166. /* register a devID for crypto callbacks */
  167. wolfSSL_CTX_SetDevId(ctx, devId);
  168. /* Require mutual authentication */
  169. wolfSSL_CTX_set_verify(ctx,
  170. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
  171. /* Load server certificates into WOLFSSL_CTX */
  172. if ((ret = wolfSSL_CTX_use_certificate_file(ctx, CERT_FILE,
  173. WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) {
  174. fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
  175. CERT_FILE);
  176. goto exit;
  177. }
  178. /* Load server key into WOLFSSL_CTX */
  179. if ((ret = wolfSSL_CTX_use_PrivateKey_file(ctx, KEY_FILE,
  180. WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) {
  181. fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
  182. KEY_FILE);
  183. goto exit;
  184. }
  185. /* Load client certificate as "trusted" into WOLFSSL_CTX */
  186. if ((ret = wolfSSL_CTX_load_verify_locations(ctx, CA_FILE, NULL))
  187. != WOLFSSL_SUCCESS) {
  188. fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
  189. CA_FILE);
  190. goto exit;
  191. }
  192. /* Continue to accept clients until mShutdown is issued */
  193. while (!mShutdown) {
  194. printf("Waiting for a connection...\n");
  195. /* Accept client connections */
  196. if ((mConnd = accept(mSockfd, (struct sockaddr*)&clientAddr, &size))
  197. == -1) {
  198. fprintf(stderr, "ERROR: failed to accept the connection\n\n");
  199. ret = -1; goto exit;
  200. }
  201. /* Create a WOLFSSL object */
  202. if ((ssl = wolfSSL_new(ctx)) == NULL) {
  203. fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
  204. ret = -1; goto exit;
  205. }
  206. /* Attach wolfSSL to the socket */
  207. wolfSSL_set_fd(ssl, mConnd);
  208. /* Establish TLS connection */
  209. #ifdef WOLFSSL_ASYNC_CRYPT
  210. err = 0; /* Reset error */
  211. #endif
  212. do {
  213. #ifdef WOLFSSL_ASYNC_CRYPT
  214. if (err == WC_PENDING_E) {
  215. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  216. if (ret < 0)
  217. break;
  218. }
  219. #endif
  220. ret = wolfSSL_accept(ssl);
  221. err = wolfSSL_get_error(ssl, 0);
  222. } while (err == WC_PENDING_E);
  223. if (ret != WOLFSSL_SUCCESS) {
  224. fprintf(stderr, "wolfSSL_accept error %d: %s\n",
  225. err, wolfSSL_ERR_error_string(err, errBuff));
  226. goto exit;
  227. }
  228. printf("Client connected successfully\n");
  229. /* Read the client data into our buff array */
  230. memset(buff, 0, sizeof(buff));
  231. #ifdef WOLFSSL_ASYNC_CRYPT
  232. err = 0; /* Reset error */
  233. #endif
  234. do {
  235. #ifdef WOLFSSL_ASYNC_CRYPT
  236. if (err == WC_PENDING_E) {
  237. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  238. if (ret < 0)
  239. break;
  240. }
  241. #endif
  242. ret = wolfSSL_read(ssl, buff, sizeof(buff)-1);
  243. err = wolfSSL_get_error(ssl, 0);
  244. } while (err == WC_PENDING_E);
  245. if (ret < 0) {
  246. fprintf(stderr, "wolfSSL_read error %d: %s\n",
  247. err, wolfSSL_ERR_error_string(err, errBuff));
  248. goto exit;
  249. }
  250. /* Print to stdout any data the client sends */
  251. printf("Client: %s\n", buff);
  252. /* Check for server shutdown command */
  253. if (strncmp(buff, "shutdown", 8) == 0) {
  254. printf("Shutdown command issued!\n");
  255. mShutdown = 1;
  256. }
  257. /* Write our reply into buff */
  258. memset(buff, 0, sizeof(buff));
  259. memcpy(buff, reply, strlen(reply));
  260. len = strnlen(buff, sizeof(buff));
  261. /* Reply back to the client */
  262. #ifdef WOLFSSL_ASYNC_CRYPT
  263. err = 0; /* Reset error */
  264. #endif
  265. do {
  266. #ifdef WOLFSSL_ASYNC_CRYPT
  267. if (err == WC_PENDING_E) {
  268. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  269. if (ret < 0)
  270. break;
  271. }
  272. #endif
  273. ret = wolfSSL_write(ssl, buff, (int)len);
  274. err = wolfSSL_get_error(ssl, 0);
  275. } while (err == WC_PENDING_E);
  276. if (ret != (int)len) {
  277. fprintf(stderr, "wolfSSL_write error %d: %s\n",
  278. err, wolfSSL_ERR_error_string(err, errBuff));
  279. goto exit;
  280. }
  281. /* Cleanup after this connection */
  282. wolfSSL_shutdown(ssl);
  283. if (ssl) {
  284. wolfSSL_free(ssl); /* Free the wolfSSL object */
  285. ssl = NULL;
  286. }
  287. if (mConnd != SOCKET_INVALID) {
  288. close(mConnd); /* Close the connection to the client */
  289. mConnd = SOCKET_INVALID;
  290. }
  291. }
  292. printf("Shutdown complete\n");
  293. exit:
  294. /* Cleanup and return */
  295. if (ssl)
  296. wolfSSL_free(ssl); /* Free the wolfSSL object */
  297. if (mConnd != SOCKET_INVALID) {
  298. close(mConnd); /* Close the connection to the client */
  299. mConnd = SOCKET_INVALID;
  300. }
  301. if (mSockfd != SOCKET_INVALID) {
  302. close(mSockfd); /* Close the socket listening for clients */
  303. mSockfd = SOCKET_INVALID;
  304. }
  305. if (ctx)
  306. wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
  307. wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
  308. (void)argc;
  309. (void)argv;
  310. return ret;
  311. }
  312. #ifndef NO_MAIN_DRIVER
  313. int main(int argc, char** argv)
  314. {
  315. return server_async_test(argc, argv);
  316. }
  317. #endif /* !NO_MAIN_DRIVER */