diff --git a/tls/client-tcp.c b/tls/client-tcp.c index 50c74712..9881f2da 100644 --- a/tls/client-tcp.c +++ b/tls/client-tcp.c @@ -19,7 +19,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -/* the unual suspects */ +/* the usual suspects */ #include #include #include @@ -47,13 +47,11 @@ int main(int argc, char** argv) } - /* - * Creates a socket that uses an internet IPv4 address, + /* 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)) < 0) { - fprintf(stderr, "ERROR: failed to create socket\n"); + * 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; } @@ -104,6 +102,6 @@ int main(int argc, char** argv) /* Cleanup and return */ - close(sockfd); /* Close the connection to the server */ - return 0; /* Return reporting a success */ + close(sockfd); /* Close the connection to the server */ + return 0; /* Return reporting a success */ } diff --git a/tls/server-tcp.c b/tls/server-tcp.c index fcb1648b..2635eff4 100644 --- a/tls/server-tcp.c +++ b/tls/server-tcp.c @@ -43,19 +43,17 @@ int main() int ret = 0; - /* - * Creates a socket that uses an internet IPv4 address, + + /* Create a socket that uses an internet IPv4 address, * Sets the socket to be stream based (TCP), - * 0 means choose the default protocol. - */ + * 0 means choose the default protocol. */ if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { - /* exit the program with -1 after printing the message to stderr */ fprintf(stderr, "ERROR: failed to create the socket\n"); return -1; } - /* 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 */ @@ -91,7 +89,7 @@ int main() /* Read in from the client while there is something to read */ do { - /* Clear the buffer memory for anything possibly left over */ + /* Clear the buffer memory for anything possibly left over */ memset(buff, 0, sizeof(buff)); @@ -108,8 +106,8 @@ int main() /* Reply back to the client */ if ((ret = write(connd, buff, sizeof(buff)-1)) < 0) { - /* Write an error without exiting the program */ fprintf(stderr, "ERROR: failed to write\n"); + return -1; } } } while (ret > 0); @@ -121,6 +119,7 @@ int main() } else { fprintf(stderr, "ERROR: failed to read\n"); + return -1; } diff --git a/tls/server-tls.c b/tls/server-tls.c index 1a5c7b93..5e1b4f7b 100644 --- a/tls/server-tls.c +++ b/tls/server-tls.c @@ -1,4 +1,4 @@ -/* server-tls.c +/* server-tcp.c * * Copyright (C) 2006-2015 wolfSSL Inc. * @@ -17,180 +17,165 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - *============================================================================= - * - * This is a super basic example of what a TCP Server secured with TLS 1.2 - * might look like. This server can also resume the session if a client - * inadvertantly disconnects. */ -#include -#include -#include -#include -#include -#include +/* the usual suspects */ #include -#include +#include +#include -/* include the wolfSSL library for our TLS 1.2 security */ +/* socket includes */ +#include +#include +#include + +/* wolfSSL */ #include + #define DEFAULT_PORT 11111 -int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t sockfd, struct sockaddr_in - clientAddr); - -int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t sockfd, struct sockaddr_in - clientAddr) -{ - /* Create our reply message */ - const char reply[] = "I hear ya fa shizzle!\n"; - socklen_t size = sizeof(clientAddr); - - /* Wait until a client connects */ - socklen_t connd = accept(sockfd, (struct sockaddr *)&clientAddr, &size); - - /* If fails to connect,int loop back up and wait for a new connection */ - if (connd == -1) { - printf("failed to accept the connection..\n"); - } - /* If it connects, read in and reply to the client */ - else { - printf("Client connected successfully\n"); - WOLFSSL* ssl; - - if ( (ssl = wolfSSL_new(ctx)) == NULL) { - fprintf(stderr, "wolfSSL_new error.\n"); - exit(EXIT_FAILURE); - } - - /* direct our ssl to our clients connection */ - wolfSSL_set_fd(ssl, connd); - - printf("Using Non-Blocking I/O: %d\n", wolfSSL_get_using_nonblock( - ssl)); - - for ( ; ; ) { - char buff[256]; - int ret = 0; - - /* Clear the buffer memory for anything possibly left over */ - memset(&buff, 0, sizeof(buff)); - - /* Read the client data into our buff array */ - if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) > 0) { - /* Print any data the client sends to the console */ - printf("Client: %s\n", buff); - - /* Reply back to the client */ - if ((ret = wolfSSL_write(ssl, reply, sizeof(reply)-1)) - < 0) - { - printf("wolfSSL_write error = %d\n", wolfSSL_get_error(ssl, ret)); - } - } - /* if the client disconnects break the loop */ - else { - if (ret < 0) - printf("wolfSSL_read error = %d\n", wolfSSL_get_error(ssl - ,ret)); - else if (ret == 0) - printf("The client has closed the connection.\n"); - - break; - } - } - wolfSSL_free(ssl); /* Free the WOLFSSL object */ - } - close(connd); /* close the connected socket */ - - return 0; -} - - int main() { - /* Create a ctx pointer for our ssl */ + int sockfd; + int connd; + char buff[256]; + struct sockaddr_in serverAddr; + struct sockaddr_in clientAddr; + socklen_t size = sizeof(clientAddr); + int ret = 0; + + /* declare wolfSSL objects */ WOLFSSL_CTX* ctx; + WOLFSSL* ssl; - /* - * Creates a socket that uses an internet IP address, - * Sets the type to be Stream based (TCP), - * 0 means choose the default protocol. - */ - socklen_t sockfd = socket(AF_INET, SOCK_STREAM, 0); - int loopExit = 0; /* 0 = False, 1 = True */ - int ret = 0; /* Return value */ - /* Server and client socket address structures */ - struct sockaddr_in serverAddr = {0}, clientAddr = {0}; - /* Initialize wolfSSL */ + /* initialize wolfSSL */ wolfSSL_Init(); - /* If positive value, the socket is valid */ - if (sockfd == -1) { - printf("ERROR: failed to create the socket\n"); - return EXIT_FAILURE; /* Kill the server with exit status 1 */ + + /* 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 structure */ + + /* Create and initilize WOLFSSL_CTX */ if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())) == NULL) { - fprintf(stderr, "wolfSSL_CTX_new error.\n"); - return EXIT_FAILURE; + fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n"); + return -1; } /* Load server certificate into WOLFSSL_CTX */ if (wolfSSL_CTX_use_certificate_file(ctx, "../certs/server-cert.pem", - SSL_FILETYPE_PEM) != SSL_SUCCESS) { - fprintf(stderr, "Error loading certs/server-cert.pem, please check" - "the file.\n"); - return EXIT_FAILURE; + SSL_FILETYPE_PEM) != SSL_SUCCESS) { + fprintf(stderr, "ERROR: failed to loadcerts/server-cert.pem, please " + "check the file.\n"); + return -1; } /* Load server key into WOLFSSL_CTX */ if (wolfSSL_CTX_use_PrivateKey_file(ctx, "../certs/server-key.pem", - SSL_FILETYPE_PEM) != SSL_SUCCESS) { - fprintf(stderr, "Error loading certs/server-key.pem, please check" - "the file.\n"); - return EXIT_FAILURE; + SSL_FILETYPE_PEM) != SSL_SUCCESS) { + fprintf(stderr, "ERROR: failed to load certs/server-key.pem, please " + "check the file.\n"); + return -1; } - /* load DH params */ - ret = wolfSSL_CTX_SetTmpDH_file(ctx, "../certs/dh2048.pem" , SSL_FILETYPE_PEM); - if (ret != SSL_SUCCESS) { - fprintf(stderr, "Error setting DH parameters.\n"); - return EXIT_FAILURE; + /* Set DH params for WOLFSSL_CTX */ + if ((ret = wolfSSL_CTX_SetTmpDH_file(ctx, "../certs/dh2048.pem", + SSL_FILETYPE_PEM)) != SSL_SUCCESS) { + fprintf(stderr, "ERROR: failed to set DH parameters.\n"); + return -1; } + + /* Initialize the server address struct to zero */ - memset((char *)&serverAddr, 0, sizeof(serverAddr)); + memset(&serverAddr, 0, sizeof(serverAddr)); /* Fill the server's address family */ - serverAddr.sin_family = AF_INET; - serverAddr.sin_addr.s_addr = INADDR_ANY; - serverAddr.sin_port = htons(DEFAULT_PORT); + serverAddr.sin_family = AF_INET; /* using IPv4 */ + serverAddr.sin_addr.s_addr = INADDR_ANY; /* from anywhere */ + serverAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */ + /* Attach the server socket to our port */ - if (bind(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) - < 0) { - printf("ERROR: failed to bind\n"); - return EXIT_FAILURE; + if (bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) { + fprintf(stderr, "ERROR: failed to bind\n"); + return -1; } + + /* Listen for a new connection, allow 5 pending connections */ + if (listen(sockfd, 5) == -1) { + fprintf(stderr, "ERROR: failed to listen\n"); + return -1; + } + + printf("Waiting for a connection...\n"); - /* Continuously accept connects while not currently in an active connection - or told to quit */ - while (loopExit == 0) { - /* listen for a new connection, allow 5 pending connections */ - ret = listen(sockfd, 5); - if (ret == 0) { - /* Accept client connections and read from them */ - loopExit = AcceptAndRead(ctx, sockfd, clientAddr); - } + /* Accept client connections */ + if ((connd = accept(sockfd, (struct sockaddr*)&clientAddr, &size)) == -1) { + fprintf(stderr, "ERROR: failed to accept the connection\n\n"); + return -1; } - wolfSSL_CTX_free(ctx); /* Free WOLFSSL_CTX */ - wolfSSL_Cleanup(); /* Free wolfSSL */ - return EXIT_SUCCESS; + + /* Create a WOLFSSL object */ + if ((ssl = wolfSSL_new(ctx)) == NULL) { + fprintf(stderr, "ERROR: failed to create WOLFSSL object\n"); + return -1; + } + + + /* Attach wolfSSL to the socket */ + wolfSSL_set_fd(ssl, connd); + + + printf("Client connected successfully\n"); + + /* Read in from the client while there is something to read */ + do { + /* Clear the buffer memory for anything possibly left over */ + memset(buff, 0, sizeof(buff)); + + + /* Read the client data into our buff array */ + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) > 0) { + /* Print any data the client sends to the console */ + printf("Client: %s\n", buff); + + + /* Write our reply into buff */ + memset(buff, 0, sizeof(buff)); + memcpy(buff, "I hear ya fa shizzle!\n", 23); + + + /* Reply back to the client */ + if ((ret = wolfSSL_write(ssl, buff, sizeof(buff)-1)) < 0) { + fprintf(stderr, "ERROR: failed to write\n"); + } + } + } while (ret > 0); + + + /* Check for a read error condition */ + if (ret == 0) { + printf("Client has closed the connection.\n"); + } + else { + fprintf(stderr, "ERROR: failed to read\n"); + } + + + /* 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 */ + }