1
0

client.c 3.9 KB

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