2
0

server-tls.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* server-tls.c
  2. *
  3. * Copyright (C) 2006-2023 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. /* wolfSSL */
  22. #include <wolfssl/wolfcrypt/settings.h>
  23. #include <wolfssl/ssl.h>
  24. #include <addrinfo.h>
  25. #define DEFAULT_PORT 11111
  26. #define CERT_FILE "../certs/server-cert.pem"
  27. #define KEY_FILE "../certs/server-key.pem"
  28. int main()
  29. {
  30. int sockfd;
  31. int connd;
  32. struct sockaddr_in servAddr;
  33. struct sockaddr_in clientAddr;
  34. socklen_t size = sizeof(clientAddr);
  35. char buff[256];
  36. size_t len;
  37. int shutdown = 0;
  38. int ret;
  39. const char* reply = "I hear ya fa shizzle!\n";
  40. /* declare wolfSSL objects */
  41. WOLFSSL_CTX* ctx;
  42. WOLFSSL* ssl;
  43. /* Initialize wolfSSL */
  44. wolfSSL_Init();
  45. /* Create a socket that uses an internet IPv4 address,
  46. * Sets the socket to be stream based (TCP),
  47. * 0 means choose the default protocol. */
  48. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  49. fprintf(stderr, "ERROR: failed to create the socket\n");
  50. return -1;
  51. }
  52. /* Create and initialize WOLFSSL_CTX */
  53. if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())) == NULL) {
  54. fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
  55. return -1;
  56. }
  57. /* Load server certificates into WOLFSSL_CTX */
  58. if (wolfSSL_CTX_use_certificate_file(ctx, CERT_FILE, SSL_FILETYPE_PEM)
  59. != SSL_SUCCESS) {
  60. fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
  61. CERT_FILE);
  62. return -1;
  63. }
  64. /* Load server key into WOLFSSL_CTX */
  65. if (wolfSSL_CTX_use_PrivateKey_file(ctx, KEY_FILE, SSL_FILETYPE_PEM)
  66. != SSL_SUCCESS) {
  67. fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
  68. KEY_FILE);
  69. return -1;
  70. }
  71. /* Initialize the server address struct with zeros */
  72. memset(&servAddr, 0, sizeof(servAddr));
  73. /* Fill in the server address */
  74. servAddr.sin_family = AF_INET; /* using IPv4 */
  75. servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */
  76. servAddr.sin_addr.s_addr = INADDR_ANY; /* from anywhere */
  77. /* Bind the server socket to our port */
  78. if (bind(sockfd, (struct sockaddr*)&servAddr, sizeof(servAddr)) == -1) {
  79. fprintf(stderr, "ERROR: failed to bind\n");
  80. return -1;
  81. }
  82. /* Listen for a new connection, allow 5 pending connections */
  83. if (listen(sockfd, 5) == -1) {
  84. fprintf(stderr, "ERROR: failed to listen\n");
  85. return -1;
  86. }
  87. /* Continue to accept clients until shutdown is issued */
  88. while (!shutdown) {
  89. printf("Waiting for a connection...\n");
  90. /* Accept client connections */
  91. if ((connd = accept(sockfd, (struct sockaddr*)&clientAddr, &size))
  92. == -1) {
  93. fprintf(stderr, "ERROR: failed to accept the connection\n\n");
  94. return -1;
  95. }
  96. /* Create a WOLFSSL object */
  97. if ((ssl = wolfSSL_new(ctx)) == NULL) {
  98. fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
  99. return -1;
  100. }
  101. /* Attach wolfSSL to the socket */
  102. wolfSSL_set_fd(ssl, connd);
  103. /* Establish TLS connection */
  104. ret = wolfSSL_accept(ssl);
  105. if (ret != SSL_SUCCESS) {
  106. fprintf(stderr, "wolfSSL_accept error = %d\n",
  107. wolfSSL_get_error(ssl, ret));
  108. return -1;
  109. }
  110. printf("Client connected successfully\n");
  111. /* Read the client data into our buff array */
  112. memset(buff, 0, sizeof(buff));
  113. if (wolfSSL_read(ssl, buff, sizeof(buff)-1) == -1) {
  114. fprintf(stderr, "ERROR: failed to read\n");
  115. return -1;
  116. }
  117. /* Print to stdout any data the client sends */
  118. printf("Client: %s\n", buff);
  119. /* Check for server shutdown command */
  120. if (strncmp(buff, "shutdown", 8) == 0) {
  121. printf("Shutdown command issued!\n");
  122. shutdown = 1;
  123. }
  124. /* Write our reply into buff */
  125. memset(buff, 0, sizeof(buff));
  126. memcpy(buff, reply, strlen(reply));
  127. len = strnlen(buff, sizeof(buff));
  128. /* Reply back to the client */
  129. if (wolfSSL_write(ssl, buff, len) != len) {
  130. fprintf(stderr, "ERROR: failed to write\n");
  131. return -1;
  132. }
  133. /* Cleanup after this connection */
  134. wolfSSL_free(ssl); /* Free the wolfSSL object */
  135. close(connd); /* Close the connection to the client */
  136. }
  137. printf("Shutdown complete\n");
  138. /* Cleanup and return */
  139. wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
  140. wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
  141. close(sockfd); /* Close the socket listening for clients */
  142. return 0; /* Return reporting a success */
  143. }