2
0

client.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* client.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 "client.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. /* utility functions shared between client and server */
  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/networking.h>
  39. #ifndef NO_MAIN_DRIVER
  40. int main(int argc, char** argv)
  41. #else
  42. int azsphere_client_app(int argc, char** argv)
  43. #endif
  44. {
  45. bool isNetworkingReady = false;
  46. SOCKET_T sockfd = 0;
  47. char buff[256];
  48. size_t len;
  49. int ret;
  50. /* declare wolfSSL objects */
  51. WOLFSSL_CTX* ctx = NULL;
  52. WOLFSSL* ssl = NULL;
  53. util_PrintIfAddr();
  54. /* Check if the Azure Sphere Dev Board has network connectivity. */
  55. if ((Networking_IsNetworkingReady(&isNetworkingReady) < 0) || !isNetworkingReady) {
  56. fprintf(stderr, "ERROR: network is not up.\n");
  57. return -1;
  58. }
  59. ret = wolfIO_TcpConnect(&sockfd, SERVER_IP, DEFAULT_PORT, 0);
  60. if ((ret != 0) || ((int)sockfd < 0)) {
  61. fprintf(stderr, "ERROR: failed to create socket.");
  62. return -1;
  63. }
  64. /* Initialize wolfSSL */
  65. wolfSSL_Init();
  66. /* Create and initialize WOLFSSL_CTX */
  67. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  68. if (ctx == NULL) {
  69. fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
  70. util_Cleanup(sockfd,ctx,ssl);
  71. return -1;
  72. }
  73. /* Load client certificates into WOLFSSL_CTX */
  74. ret = wolfSSL_CTX_load_verify_buffer(ctx, CERT, SIZEOF_CERT, WOLFSSL_FILETYPE_ASN1);
  75. if (ret != SSL_SUCCESS) {
  76. fprintf(stderr, "ERROR: failed to load client certificate, "
  77. "please check the buffer.\n");
  78. util_Cleanup(sockfd,ctx,ssl);
  79. return -1;
  80. }
  81. /* Create a WOLFSSL object */
  82. if ((ssl = wolfSSL_new(ctx)) == NULL) {
  83. fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
  84. util_Cleanup(sockfd,ctx,ssl);
  85. return -1;
  86. }
  87. /* Attach wolfSSL to the socket */
  88. wolfSSL_set_fd(ssl, sockfd);
  89. /* Connect to wolfSSL on the server side */
  90. if (wolfSSL_connect(ssl) != SSL_SUCCESS) {
  91. fprintf(stderr, "ERROR: failed to connect to wolfSSL\n");
  92. util_Cleanup(sockfd,ctx,ssl);
  93. return -1;
  94. }
  95. /* Get length of message for server. */
  96. printf("\nMessage for server: %s\n",msg);
  97. len = strnlen(msg, sizeof(msg));
  98. /* Send the message to the server */
  99. if (wolfSSL_write(ssl, msg, (int)len) != len) {
  100. fprintf(stderr, "ERROR: failed to write\n");
  101. util_Cleanup(sockfd,ctx,ssl);
  102. return -1;
  103. }
  104. /* Read the server data into our buff array */
  105. memset(buff, 0, sizeof(buff));
  106. if (wolfSSL_read(ssl, buff, sizeof(buff) - 1) == -1) {
  107. fprintf(stderr, "ERROR: failed to read\n");
  108. util_Cleanup(sockfd,ctx,ssl);
  109. return -1;
  110. }
  111. /* Print to stdout any data the server sends */
  112. printf("Server Reply: %s\n", buff);
  113. /* Cleanup and return */
  114. util_Cleanup(sockfd,ctx,ssl);
  115. return 0; /* Return reporting a success */
  116. }