2
0

server.c 6.7 KB

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