1
0

server.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* server.c
  2. *
  3. * Copyright (C) 2006-2024 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. #include "server.h"
  22. /* the usual suspects */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. /* socket includes */
  27. #include <sys/socket.h>
  28. #include <arpa/inet.h>
  29. #include <netinet/in.h>
  30. #include <unistd.h>
  31. /* <shared/util.h> includes */
  32. #include <shared/util.h>
  33. /* wolfSSL */
  34. #include <wolfssl/ssl.h>
  35. #include <wolfssl/certs_test.h>
  36. /* Azure Sphere */
  37. #include <applibs/log.h>
  38. #include <applibs/networking.h>
  39. #define BIND_PORT 11111
  40. #define CERT_BUF server_cert_der_2048
  41. #define SIZEOF_CERT_BUF sizeof_server_cert_der_2048
  42. #define KEY_BUF server_key_der_2048
  43. #define SIZEOF_KEY_BUF sizeof_server_key_der_2048
  44. int main(void)
  45. {
  46. bool isNetworkingReady = false;
  47. int sockfd;
  48. int connd;
  49. struct sockaddr_in servAddr;
  50. struct sockaddr_in clientAddr;
  51. socklen_t size = sizeof(clientAddr);
  52. char buff[256];
  53. size_t len;
  54. int shutdown = 0;
  55. int ret;
  56. const char* reply = "I hear ya fa shizzle!\n";
  57. /* declare wolfSSL objects */
  58. WOLFSSL_CTX* ctx = NULL;
  59. WOLFSSL* ssl = NULL;
  60. util_PrintIfAddr();
  61. /* Check if the Azure Sphere Dev Board has network connectivity. */
  62. if ((Networking_IsNetworkingReady(&isNetworkingReady) < 0) || !isNetworkingReady) {
  63. fprintf(stderr,"Error: Network is not up.\n");
  64. return -1;
  65. }
  66. /* Initialize wolfSSL */
  67. wolfSSL_Init();
  68. /* Create a socket that uses an internet IPv4 address,
  69. * Sets the socket to be stream based (TCP),
  70. * 0 means choose the default protocol. */
  71. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  72. fprintf(stderr, "ERROR: failed to create the socket\n");
  73. util_Cleanup(sockfd, ctx, ssl);
  74. return -1;
  75. }
  76. /* Create and initialize WOLFSSL_CTX */
  77. if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())) == NULL) {
  78. fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
  79. util_Cleanup(sockfd, ctx, ssl);
  80. return -1;
  81. }
  82. /* Load server certificates into WOLFSSL_CTX */
  83. if (wolfSSL_CTX_use_certificate_buffer(ctx, CERT_BUF, SIZEOF_CERT_BUF, SSL_FILETYPE_ASN1)
  84. != SSL_SUCCESS) {
  85. fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
  86. CERT_BUF);
  87. util_Cleanup(sockfd, ctx, ssl);
  88. return -1;
  89. }
  90. /* Load server key into WOLFSSL_CTX */
  91. if (wolfSSL_CTX_use_PrivateKey_buffer(ctx, KEY_BUF, SIZEOF_KEY_BUF, SSL_FILETYPE_ASN1)
  92. != SSL_SUCCESS) {
  93. fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
  94. KEY_BUF);
  95. util_Cleanup(sockfd, ctx, ssl);
  96. return -1;
  97. }
  98. /* Initialize the server address struct with zeros */
  99. memset(&servAddr, 0, sizeof(servAddr));
  100. /* Fill in the server address */
  101. servAddr.sin_family = AF_INET; /* using IPv4 */
  102. servAddr.sin_port = htons(BIND_PORT); /* on BIND_PORT */
  103. servAddr.sin_addr.s_addr = INADDR_ANY; /* from anywhere */
  104. /* Bind the server socket to our port */
  105. if (bind(sockfd, (struct sockaddr*)&servAddr, sizeof(servAddr)) == -1) {
  106. fprintf(stderr, "ERROR: failed to bind\n");
  107. util_Cleanup(sockfd, ctx, ssl);
  108. return -1;
  109. }
  110. /* Listen for a new connection, allow 5 pending connections */
  111. if (listen(sockfd, 5) == -1) {
  112. fprintf(stderr, "ERROR: failed to listen\n");
  113. util_Cleanup(sockfd, ctx, ssl);
  114. return -1;
  115. }
  116. /* Continue to accept clients until shutdown is issued */
  117. while (!shutdown) {
  118. printf("Waiting for a connection...\n");
  119. /* Accept client connections */
  120. if ((connd = accept(sockfd, (struct sockaddr*)&clientAddr, &size))
  121. == -1) {
  122. fprintf(stderr, "ERROR: failed to accept the connection\n\n");
  123. util_Cleanup(sockfd, ctx, ssl);
  124. return -1;
  125. }
  126. /* Create a WOLFSSL object */
  127. if ((ssl = wolfSSL_new(ctx)) == NULL) {
  128. fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
  129. util_Cleanup(sockfd, ctx, ssl);
  130. return -1;
  131. }
  132. /* Attach wolfSSL to the socket */
  133. wolfSSL_set_fd(ssl, connd);
  134. /* Establish TLS connection */
  135. ret = wolfSSL_accept(ssl);
  136. if (ret != SSL_SUCCESS) {
  137. fprintf(stderr, "wolfSSL_accept error = %d\n",
  138. wolfSSL_get_error(ssl, ret));
  139. util_Cleanup(sockfd, ctx, ssl);
  140. return -1;
  141. }
  142. printf("Client connected successfully\n");
  143. /* Read the client data into our buff array */
  144. memset(buff, 0, sizeof(buff));
  145. if (wolfSSL_read(ssl, buff, sizeof(buff)-1) == -1) {
  146. fprintf(stderr, "ERROR: failed to read\n");
  147. util_Cleanup(sockfd, ctx, ssl);
  148. return -1;
  149. }
  150. /* Print to stdout any data the client sends */
  151. printf("Client: %s\n", buff);
  152. /* Check for server shutdown command */
  153. if (strncmp(buff, "shutdown", 8) == 0) {
  154. printf("Shutdown command issued!\n");
  155. shutdown = 1;
  156. }
  157. /* Write our reply into buff */
  158. memset(buff, 0, sizeof(buff));
  159. memcpy(buff, reply, strlen(reply));
  160. len = strnlen(buff, sizeof(buff));
  161. /* Reply back to the client */
  162. if (wolfSSL_write(ssl, buff, (int)len) != len) {
  163. fprintf(stderr, "ERROR: failed to write\n");
  164. util_Cleanup(sockfd, ctx, ssl);
  165. return -1;
  166. }
  167. /* Cleanup after this connection */
  168. wolfSSL_free(ssl); /* Free the wolfSSL object */
  169. close(connd); /* Close the connection to the client */
  170. }
  171. printf("Shutdown complete\n");
  172. /* Cleanup and return */
  173. util_Cleanup(sockfd, ctx, ssl);
  174. return 0; /* Return reporting a success */
  175. }