2
0

client-tls.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* client-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. #define DEFAULT_PORT 11111
  25. #define CERT_FILE "../certs/ca-cert.pem"
  26. int main(int argc, char** argv)
  27. {
  28. int sockfd;
  29. struct sockaddr_in servAddr;
  30. char buff[256];
  31. size_t len;
  32. int ret;
  33. /* declare wolfSSL objects */
  34. WOLFSSL_CTX* ctx;
  35. WOLFSSL* ssl;
  36. /* Check for proper calling convention */
  37. if (argc != 2) {
  38. printf("usage: %s <IPv4 address>\n", argv[0]);
  39. return 0;
  40. }
  41. /* Create a socket that uses an internet IPv4 address,
  42. * Sets the socket to be stream based (TCP),
  43. * 0 means choose the default protocol. */
  44. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  45. fprintf(stderr, "ERROR: failed to create the socket\n");
  46. ret = -1;
  47. goto end;
  48. }
  49. /* Initialize the server address struct with zeros */
  50. memset(&servAddr, 0, sizeof(servAddr));
  51. /* Fill in the server address */
  52. servAddr.sin_family = AF_INET; /* using IPv4 */
  53. servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */
  54. /* Get the server IPv4 address from the command line call */
  55. if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr, sizeof(servAddr.sin_addr)) != 1) {
  56. fprintf(stderr, "ERROR: invalid address\n");
  57. ret = -1;
  58. goto end;
  59. }
  60. /* Connect to the server */
  61. if ((ret = connect(sockfd, (struct sockaddr*) &servAddr, sizeof(servAddr)))
  62. == -1) {
  63. fprintf(stderr, "ERROR: failed to connect\n");
  64. goto end;
  65. }
  66. /*---------------------------------*/
  67. /* Start of security */
  68. /*---------------------------------*/
  69. /* Initialize wolfSSL */
  70. if ((ret = wolfSSL_Init()) != WOLFSSL_SUCCESS) {
  71. fprintf(stderr, "ERROR: Failed to initialize the library\n");
  72. goto socket_cleanup;
  73. }
  74. /* Create and initialize WOLFSSL_CTX */
  75. if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
  76. fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
  77. ret = -1;
  78. goto socket_cleanup;
  79. }
  80. /* Load client certificates into WOLFSSL_CTX */
  81. if ((ret = wolfSSL_CTX_load_verify_locations(ctx, CERT_FILE, NULL))
  82. != SSL_SUCCESS) {
  83. fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
  84. CERT_FILE);
  85. goto ctx_cleanup;
  86. }
  87. /* Create a WOLFSSL object */
  88. if ((ssl = wolfSSL_new(ctx)) == NULL) {
  89. fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
  90. ret = -1;
  91. goto ctx_cleanup;
  92. }
  93. /* Attach wolfSSL to the socket */
  94. if ((ret = wolfSSL_set_fd(ssl, sockfd)) != WOLFSSL_SUCCESS) {
  95. fprintf(stderr, "ERROR: Failed to set the file descriptor\n");
  96. goto cleanup;
  97. }
  98. /* Connect to wolfSSL on the server side */
  99. if ((ret = wolfSSL_connect(ssl)) != SSL_SUCCESS) {
  100. fprintf(stderr, "ERROR: failed to connect to wolfSSL\n");
  101. goto cleanup;
  102. }
  103. /* Get a message for the server from stdin */
  104. printf("Message for server: ");
  105. memset(buff, 0, sizeof(buff));
  106. if (fgets(buff, sizeof(buff), stdin) == NULL) {
  107. fprintf(stderr, "ERROR: failed to get message for server\n");
  108. ret = -1;
  109. goto cleanup;
  110. }
  111. len = strnlen(buff, sizeof(buff));
  112. /* Send the message to the server */
  113. if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
  114. fprintf(stderr, "ERROR: failed to write entire message\n");
  115. fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len);
  116. goto cleanup;
  117. }
  118. /* Read the server data into our buff array */
  119. memset(buff, 0, sizeof(buff));
  120. if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) {
  121. fprintf(stderr, "ERROR: failed to read\n");
  122. goto cleanup;
  123. }
  124. /* Print to stdout any data the server sends */
  125. printf("Server: %s\n", buff);
  126. /* Cleanup and return */
  127. cleanup:
  128. wolfSSL_free(ssl); /* Free the wolfSSL object */
  129. ctx_cleanup:
  130. wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
  131. wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
  132. socket_cleanup:
  133. close(sockfd); /* Close the connection to the server */
  134. end:
  135. return ret; /* Return reporting a success */
  136. }