From a0aa083f9e4895e66a344c4327d1871bb95dec80 Mon Sep 17 00:00:00 2001 From: Levi Rak Date: Tue, 30 May 2017 17:08:08 -0600 Subject: [PATCH] made client-tls from cleaned client-tcp --- tls/client-tls.c | 166 ++++++++++++++++++++++++++--------------------- tls/server-tls.c | 5 +- 2 files changed, 94 insertions(+), 77 deletions(-) diff --git a/tls/client-tls.c b/tls/client-tls.c index 5e08a5c5..e0ea4efa 100644 --- a/tls/client-tls.c +++ b/tls/client-tls.c @@ -1,4 +1,4 @@ -/* client-tls.c +/* client-tcp.c * * Copyright (C) 2006-2015 wolfSSL Inc. * @@ -18,111 +18,127 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#include -#include -#include -#include -#include -#include /* wolfSSL secure read/write methods */ -#define MAXDATASIZE 4096 /* maximum acceptable amount of data */ -#define SERV_PORT 11111 /* define default port number */ +/* the usual suspects */ +#include +#include +#include -const char* cert = "../certs/ca-cert.pem"; +/* socket includes */ +#include +#include +#include + +/* wolfSSL */ +#include + + +#define DEFAULT_PORT 11111 int main(int argc, char** argv) { - int sockfd; /* socket file descriptor */ - struct sockaddr_in servAddr; /* struct for server address */ - int ret = 0; /* variable for error checking */ - - /* data to send to the server, data recieved from the server */ - char sendBuff[MAXDATASIZE], rcvBuff[MAXDATASIZE] = {0}; + int sockfd; + struct sockaddr_in servAddr; + char buff[256]; + size_t len; + /* declare wolfSSL objects */ WOLFSSL_CTX* ctx; - WOLFSSL* ssl; /* create WOLFSSL object */ + WOLFSSL* ssl; + /* Check for proper calling convention */ if (argc != 2) { - /* if the number of arguments is not two, error */ - printf("usage: ./client-tcp \n"); - return EXIT_FAILURE; + printf("usage: ./client-tcp \n"); + return 0; } - /* internet address family, stream based tcp, default protocol */ - sockfd = socket(AF_INET, SOCK_STREAM, 0); - - if (sockfd < 0) { - printf("Failed to create socket. errno: %i\n", errno); - return EXIT_FAILURE; - } - - memset(&servAddr, 0, sizeof(servAddr)); /* clears memory block for use */ - servAddr.sin_family = AF_INET; /* sets addressfamily to internet*/ - servAddr.sin_port = htons(SERV_PORT); /* sets port to defined port */ - - /* looks for the server at the entered address (ip in the command line) */ - if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr) < 1) { - /* checks validity of address */ - ret = errno; - printf("Invalid Address. errno: %i\n", ret); - return EXIT_FAILURE; - } - - if (connect(sockfd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) { - ret = errno; - printf("Connect error. Error: %i\n", ret); - return EXIT_FAILURE; - } - - printf("Message for server:\t"); - fgets(sendBuff, MAXDATASIZE, stdin); + /* Initialize wolfSSL */ wolfSSL_Init(); + + /* Create a socket that uses an internet IPv4 address, + * Sets the socket to be stream based (TCP), + * 0 means choose the default protocol. */ + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { + fprintf(stderr, "ERROR: failed to create the socket\n"); + return -1; + } + + + /* Create and initialize WOLFSSL_CTX */ if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) { - printf("SSL_CTX_new error.\n"); - return EXIT_FAILURE; + fprintf(stderr, "ERROR: failed to make WOLFSSL_CTX\n"); + return -1; } - if (wolfSSL_CTX_load_verify_locations(ctx, cert, 0) != SSL_SUCCESS) { - printf("Error loading %s. Please check the file.\n", cert); - return EXIT_FAILURE; + /* Load certificates into WOLFSSL_CTX */ + if (wolfSSL_CTX_load_verify_locations(ctx, "../certs/ca-cert.pem", NULL) + != SSL_SUCCESS) { + fprintf(stderr, "ERROR: failed to load certs/ca-cert.pem, please " + "check the file.\n"); + return -1; } + + /* Initialize the server address struct with zeros */ + memset(&servAddr, 0, sizeof(servAddr)); + + /* Fill in the server address */ + servAddr.sin_family = AF_INET; /* use IPv4 */ + servAddr.sin_port = htons(DEFAULT_PORT); /* look at port DEFAULT_PORT */ + + /* Get the server IPv4 address from the command line call */ + if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr) != 1) { + fprintf(stderr, "ERROR: invalid Address\n"); + return -1; + } + + + /* Connect to the server */ + if (connect(sockfd, (struct sockaddr*) &servAddr, sizeof(servAddr)) < 0) { + fprintf(stderr, "ERROR: failed to connect\n"); + return -1; + } + + /* Make WOLFSSL object */ if ((ssl = wolfSSL_new(ctx)) == NULL) { printf("wolfSSL_new error.\n"); - return EXIT_FAILURE; + return -1; } + /* Hook wolfSSL up to the socket */ wolfSSL_set_fd(ssl, sockfd); - ret = wolfSSL_connect(ssl); - if (ret != SSL_SUCCESS) { - printf("Failed to connect to server\n"); - return EXIT_FAILURE; + /* Get a message for the server from stdin */ + printf("Message for server: "); + memset(buff, 0, sizeof(buff)); + fgets(buff, sizeof(buff), stdin); + len = strnlen(buff, sizeof(buff)); + + /* Send the message */ + if (wolfSSL_write(ssl, buff, len) != len) { + fprintf(stderr, "ERROR: failed to write\n"); + return -1; } - if (wolfSSL_write(ssl, sendBuff, strlen(sendBuff)) != strlen(sendBuff)) { - /* the message is not able to send, or error trying */ - ret = wolfSSL_get_error(ssl, 0); - printf("Write error: Error: %i\n", ret); - return EXIT_FAILURE; + + /* Get a response from the server */ + memset(buff, 0, sizeof(buff)); + if (wolfSSL_read(ssl, buff, sizeof(buff)-1) < 0) { + fprintf(stderr, "ERROR: failed to read\n"); + return -1; } - if (wolfSSL_read(ssl, rcvBuff, MAXDATASIZE) < 0) { - /* the server failed to send data, or error trying */ - ret = wolfSSL_get_error(ssl, 0); - printf("Read error. Error: %i\n", ret); - return EXIT_FAILURE; - } - printf("Recieved: \t%s\n", rcvBuff); + /* Print the message to stdout */ + printf("Recieved: %s\n", buff); - /* frees all data before client termination */ - wolfSSL_free(ssl); - wolfSSL_CTX_free(ctx); - wolfSSL_Cleanup(); - return ret; + /* Cleanup and return */ + wolfSSL_free(ssl); /* Free the wolfSSL object */ + wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */ + wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */ + return 0; /* Return reporting a success */ } diff --git a/tls/server-tls.c b/tls/server-tls.c index 5e1b4f7b..8e571cb9 100644 --- a/tls/server-tls.c +++ b/tls/server-tls.c @@ -63,7 +63,7 @@ int main() } - /* Create and initilize WOLFSSL_CTX */ + /* Create and initialize WOLFSSL_CTX */ if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())) == NULL) { fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n"); return -1; @@ -93,7 +93,7 @@ int main() } - /* Initialize the server address struct to zero */ + /* Initialize the server address struct with zeros */ memset(&serverAddr, 0, sizeof(serverAddr)); /* Fill the server's address family */ @@ -169,6 +169,7 @@ int main() } else { fprintf(stderr, "ERROR: failed to read\n"); + return -1; }