From 56f6b9c2fb23f033ed488a597571ffdc225bd2ab Mon Sep 17 00:00:00 2001 From: Levi Rak Date: Mon, 22 May 2017 11:18:54 -0600 Subject: [PATCH] Cleaned up whitespace/alignment --- tls/Makefile | 0 tls/README.md | 293 +++++++++++++++++++++++--------- tls/client-callback.c | 18 +- tls/client-tls-nonblocking.c | 42 ++--- tls/client-tls-perf.c | 0 tls/client-tls-resume.c | 46 ++--- tls/client-tls-writedup.c | 6 +- tls/server-callback.c | 34 ++-- tls/server-tcp.c | 26 +-- tls/server-tls-epoll-perf.c | 6 +- tls/server-tls-epoll-threaded.c | 6 +- tls/server-tls-nonblocking.c | 40 ++--- tls/server-tls-threaded.c | 22 +-- 13 files changed, 332 insertions(+), 207 deletions(-) mode change 100755 => 100644 tls/Makefile mode change 100755 => 100644 tls/README.md mode change 100755 => 100644 tls/client-tls-perf.c mode change 100755 => 100644 tls/server-tls-epoll-perf.c mode change 100755 => 100644 tls/server-tls-epoll-threaded.c diff --git a/tls/Makefile b/tls/Makefile old mode 100755 new mode 100644 diff --git a/tls/README.md b/tls/README.md old mode 100755 new mode 100644 index 07045982..ed915af3 --- a/tls/README.md +++ b/tls/README.md @@ -1,10 +1,15 @@ Tutorial ======== -This tutorial will teach you how to install and run a basic TCP Server and Client. As well as how to incorporate wolfSSL TLS and some additional features on top of these basic examples. It is expected that you have a basic understanding of a simple tcp server/client. If not, before continueing please take a moment to look over the `server-tcp.c` and `client-tcp.c` file which contains the basic tcp server that we will be expanding upon in this tutorial. +This tutorial will teach you how to install and run a basic TCP Server and +Client. As well as how to incorporate wolfSSL TLS and some additional features +on top of these basic examples. It is expected that you have a basic +understanding of a simple tcp server/client. If not, before continueing please +take a moment to look over the `server-tcp.c` and `client-tcp.c` file which +contains the basic tcp server that we will be expanding upon in this tutorial. - -First you will need `gcc` and `make` installed on your terminal. You can do this by opening a new terminal window and typing: +First you will need `gcc` and `make` installed on your terminal. You can do +this by opening a new terminal window and typing: sudo apt-get install gcc make @@ -22,21 +27,29 @@ First you will need `gcc` and `make` installed on your terminal. You can do this 4. [Starting the TLS Client & Server](https://github.com/wolfSSL/wolfssl-examples/blob/master/tls/tutorial-tcp-tls.md#starting-the-tls-client--server) ## Incorporating wolfSSL TLS -To incorporate wolfSSL TLS into your client or server you need to first configure and install the wolfSSL library to your linux machine. After you have done that, you can then run `make` to compile the TLS versions into an executeable. +To incorporate wolfSSL TLS into your client or server you need to first +configure and install the wolfSSL library to your linux machine. After you have +done that, you can then run `make` to compile the TLS versions into an +executeable. ### Installing wolfSSL + Download and extract the wolfSSL package from [here.](http://wolfssl.com/yaSSL/Products-wolfssl.html) + In terminal, navigate to the root of the extracted folder. + Type `./configure` press enter. Wait until it finishes configuring. -+ Type `make` press enter. -+ Type `sudo make install`, this will install the wolfSSL libraries to your machine. ++ Type `make` press enter. ++ Type `sudo make install`, this will install the wolfSSL libraries to your + machine. -wolfSSL libraries should now be installed to your machine and ready to use. You can now make and run the server and client examples. +wolfSSL libraries should now be installed to your machine and ready to use. You +can now make and run the server and client examples. ## Server TLS Tutorial ### Basic TLS Server -To begin, we will be re-writing the basic `server-tcp.c` with wolfSSL. The structure of the file will be almost identical. To begin, we will need to include the wolfssl libraries with the rest of our includes at the top of the file: +To begin, we will be re-writing the basic `server-tcp.c` with wolfSSL. The +structure of the file will be almost identical. To begin, we will need to +include the wolfssl libraries with the rest of our includes at the top of the +file: ```c #include @@ -51,15 +64,23 @@ To begin, we will be re-writing the basic `server-tcp.c` with wolfSSL. The struc /* include the wolfSSL library for our TLS 1.2 security */ #include ``` -Next we will add our `#define DEFAULT_PORT 11111` and the prototype for our `AcceptAndRead` function: +Next we will add our `#define DEFAULT_PORT 11111` and the prototype for our +`AcceptAndRead` function: ```c #define DEFAULT_PORT 11111 -int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t sockfd, struct sockaddr_in +int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t sockfd, struct sockaddr_in clientAddr); ``` -Now we will build our `main()` function for the program. What happens here is we create a WOLFSSL context pointer and a socket. We then initialize wolfSSL so that it can be used. After that we tell wolfSSL where our certificate and private key files are that we want our server to use. We then attach our socket to the `DEFAULT_PORT` that we defined above. The last thing to do in the main function is to listen for a new connection on the socket that we binded to our port above. When we get a new connection, we call the `AcceptAndRead` function. The main function should look like: +Now we will build our `main()` function for the program. What happens here is +we create a WOLFSSL context pointer and a socket. We then initialize wolfSSL so +that it can be used. After that we tell wolfSSL where our certificate and +private key files are that we want our server to use. We then attach our socket +to the `DEFAULT_PORT` that we defined above. The last thing to do in the main +function is to listen for a new connection on the socket that we binded to our +port above. When we get a new connection, we call the `AcceptAndRead` function. +The main function should look like: ```c int main() @@ -67,7 +88,7 @@ int main() /* Create a ctx pointer for our ssl */ WOLFSSL_CTX* ctx; - /* + /* * Creates a socket that uses an internet IP address, * Sets the type to be Stream based (TCP), * 0 means choose the default protocol. @@ -84,7 +105,7 @@ int main() /* 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 */ + return EXIT_FAILURE; /* Kill the server with exit status 1 */ } /* create and initialize WOLFSSL_CTX structure */ @@ -94,7 +115,7 @@ int main() } /* Load server certificate into WOLFSSL_CTX */ - if (wolfSSL_CTX_use_certificate_file(ctx, "../certs/server-cert.pem", + 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"); @@ -102,7 +123,7 @@ int main() } /* Load server key into WOLFSSL_CTX */ - if (wolfSSL_CTX_use_PrivateKey_file(ctx, "../certs/server-key.pem", + 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"); @@ -110,7 +131,7 @@ int main() } /* Initialize the server address struct to zero */ - memset((char *)&serverAddr, 0, sizeof(serverAddr)); + memset((char *)&serverAddr, 0, sizeof(serverAddr)); /* Fill the server's address family */ serverAddr.sin_family = AF_INET; @@ -142,12 +163,21 @@ int main() return EXIT_SUCCESS; } ``` -Now all that is left is the `AcceptAndRead` function. This function accepts the new connection and passes it off to its on file descriptor `connd`. We then create our ssl object and direct it to our clients connection. Once thats done we jump into a `for ( ; ; )` loop and do a `wolfSSL_read` which will decrypt and send any data the client sends to our `buff` array. Once that happens we print the data to the console and then send a reply back to the client letting the client know that we reicieved their message. We then break out of the loop, free our ssl and close the `connd` connection since it's no longer used. We then `return 0` which tells our loop in main that it was successful and to continue listening for new connections. +Now all that is left is the `AcceptAndRead` function. This function accepts the +new connection and passes it off to its on file descriptor `connd`. We then +create our ssl object and direct it to our clients connection. Once thats done +we jump into a `for ( ; ; )` loop and do a `wolfSSL_read` which will decrypt +and send any data the client sends to our `buff` array. Once that happens we +print the data to the console and then send a reply back to the client letting +the client know that we reicieved their message. We then break out of the loop, +free our ssl and close the `connd` connection since it's no longer used. We +then `return 0` which tells our loop in main that it was successful and to +continue listening for new connections. Here is the `AcceptAndRead` with more detailed comments on what's happening. ```c -int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t sockfd, struct sockaddr_in +int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t sockfd, struct sockaddr_in clientAddr) { /* Create our reply message */ @@ -188,9 +218,9 @@ int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t sockfd, struct sockaddr_in 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)) + if ((ret = wolfSSL_write(ssl, reply, sizeof(reply)-1)) < 0) { printf("wolfSSL_write error = %d\n", wolfSSL_get_error(ssl, ret)); @@ -214,14 +244,27 @@ int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t sockfd, struct sockaddr_in return 0; } ``` -And with that, you should now have a basic TLS server that accepts a connection, reads in data from the client, sends a reply back, and closes the clients connection. +And with that, you should now have a basic TLS server that accepts a +connection, reads in data from the client, sends a reply back, and closes the +clients connection. **The finished source code for this can be [found here.](https://github.com/wolfSSL/wolfssl-examples/blob/master/tls/server-tls.c)** ### Adding Server Multi-threading -To add multi-threading support to the basic `tls-server.c` that we created above, we will be using pthreads. Multi-threading will allow the server to handle multiple client connections at the same time. It will pass each new connection off into it's own thread. To do this we will create a new function called `ThreadHandler`. This function will be passed off to its own thread when a new client connection is accepted. We will also be making some minor changes to our `main()` and `AcceptAndRead` functions. +To add multi-threading support to the basic `tls-server.c` that we created +above, we will be using pthreads. Multi-threading will allow the server to +handle multiple client connections at the same time. It will pass each new +connection off into it's own thread. To do this we will create a new function +called `ThreadHandler`. This function will be passed off to its own thread when +a new client connection is accepted. We will also be making some minor changes +to our `main()` and `AcceptAndRead` functions. -We will start by adding a `#include ` followed by removing our `WOLFSSL_CTX* ctx` from our main() function and making it global. This will allow our threads to have access to it. Because we are no long passing it into the `AcceptAndRead` function, we need to modify the prototype function to no longer take a `wolfSSL_CTX` parameter. The top of your file should now look like: +We will start by adding a `#include ` followed by removing our +`WOLFSSL_CTX* ctx` from our main() function and making it global. This will +allow our threads to have access to it. Because we are no long passing it into +the `AcceptAndRead` function, we need to modify the prototype function to no +longer take a `wolfSSL_CTX` parameter. The top of your file should now look +like: ```c #include @@ -245,7 +288,9 @@ void *ThreadHandler(void* socketDesc); /* Create a ctx pointer for our ssl */ WOLFSSL_CTX* ctx; ``` -Moving down to our `main()` function, we need to now remove `WOLFSSL_CTX* ctx` from the top of the function since it is now a global variable. Lastly we need modify the `while` loop at the bottom of `main()` to look like: +Moving down to our `main()` function, we need to now remove `WOLFSSL_CTX* ctx` +from the top of the function since it is now a global variable. Lastly we need +modify the `while` loop at the bottom of `main()` to look like: ```c printf("Waiting for a connection...\n"); @@ -260,11 +305,17 @@ Moving down to our `main()` function, we need to now remove `WOLFSSL_CTX* ctx` f loopExit = AcceptAndRead(sockfd, clientAddr); } ``` -As you can tell, when we call - loopExit = AcceptAndRead(sockfd, clientAddr); +As you can tell, when we call +```c +loopExit = AcceptAndRead(sockfd, clientAddr); +``` we are no longer passing in our `WOLFSSL_CTX` pointer since it's now global. -Moving on, we can now modify our `AcceptAndRead` function. This function will now pass accepted connections off into their own thread using `pthreads`. This new thread will then loop, reading and writing to the connected client. Because of this most of this function will be moved into the 'ThreadHandler' function. The `AcceptAndRead` function will now look like: +Moving on, we can now modify our `AcceptAndRead` function. This function will +now pass accepted connections off into their own thread using `pthreads`. This +new thread will then loop, reading and writing to the connected client. Because +of this most of this function will be moved into the 'ThreadHandler' function. +The `AcceptAndRead` function will now look like: ```c int AcceptAndRead(socklen_t sockfd, struct sockaddr_in clientAddr) @@ -275,7 +326,7 @@ int AcceptAndRead(socklen_t sockfd, struct sockaddr_in clientAddr) pthread_t thread_id; /* Wait until a client connects */ - while ((connd = accept(sockfd, (struct sockaddr *)&clientAddr, + while ((connd = accept(sockfd, (struct sockaddr *)&clientAddr, &size))) { /* Pass the client into a new thread */ if (pthread_create(&thread_id, NULL, ThreadHandler, (void *) @@ -292,7 +343,13 @@ int AcceptAndRead(socklen_t sockfd, struct sockaddr_in clientAddr) } ``` -Now that we have that passing client connections to their own threads, we need to create the `ThreadHandler`, this function will act just like the original `AcceptAndRead` function, just in it's own thread so that we can have multiple clients connected. In this function we will be create our `ssl` object and directing it at our client. It will continuously run in a `for ( ; ; )` loop, reading and writing to the connected client until the client disconnects. It should look like: +Now that we have that passing client connections to their own threads, we need +to create the `ThreadHandler`, this function will act just like the original +`AcceptAndRead` function, just in it's own thread so that we can have multiple +clients connected. In this function we will be create our `ssl` object and +directing it at our client. It will continuously run in a `for ( ; ; )` loop, +reading and writing to the connected client until the client disconnects. It +should look like: ```c void *ThreadHandler(void* socketDesc) @@ -323,9 +380,9 @@ void *ThreadHandler(void* socketDesc) if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) > 0) { /* Print any data the client sends to the console */ printf("Client on Socket %d: %s\n", connd, buff); - + /* Reply back to the client */ - if ((ret = wolfSSL_write(ssl, reply, sizeof(reply)-1)) + if ((ret = wolfSSL_write(ssl, reply, sizeof(reply)-1)) < 0) { printf("wolfSSL_write error = %d\n", wolfSSL_get_error(ssl, ret)); } @@ -348,16 +405,30 @@ void *ThreadHandler(void* socketDesc) } ``` -And that's it. You now have a TLS server using multi-threading to handle multiple clients in seperate threads. +And that's it. You now have a TLS server using multi-threading to handle +multiple clients in seperate threads. **The finished source code for this can be [found here.](https://github.com/wolfSSL/wolfssl-examples/blob/master/tls/server-tls-threaded.c)** ### Adding Server Non-blocking I/O -A Nonblocking server, like the multi-threaded server, can handle multiple connections. It does this by moving clients to the next available socket and handling them all from a single thread. The benefit to this approach is that it there is much less memory consumption than creating a new thread for each connected client. Nonblocking is also much faster. 100 clients connected through nonblocking would be much faster than 100 clients through multi-threading. However, for non-blocking to work the connecting client must also be configured to work with non-blocking servers. +A Nonblocking server, like the multi-threaded server, can handle multiple +connections. It does this by moving clients to the next available socket and +handling them all from a single thread. The benefit to this approach is that it +there is much less memory consumption than creating a new thread for each +connected client. Nonblocking is also much faster. 100 clients connected +through nonblocking would be much faster than 100 clients through +multi-threading. However, for non-blocking to work the connecting client must +also be configured to work with non-blocking servers. -To add non-blocking input and ouput to our `server-tls.c` file we will need to create two new functions `TCPSelect` and `NonBlocking_ReadWriteAccept`. We will also create an enum that will be used to tell our `NonBlocking_ReadWriteAccept` what exactly to do, does it need to Read? Does it need to Write? or does it need to Accept? this is done so we can re-use the same code and not end up writing three more functions that practically do that same thing. +To add non-blocking input and ouput to our `server-tls.c` file we will need to +create two new functions `TCPSelect` and `NonBlocking_ReadWriteAccept`. We will +also create an enum that will be used to tell our `NonBlocking_ReadWriteAccept` +what exactly to do, does it need to Read? Does it need to Write? or does it +need to Accept? this is done so we can re-use the same code and not end up +writing three more functions that practically do that same thing. -To start, let's create our enumerator and prototype functions. The top of your file should look like: +To start, let's create our enumerator and prototype functions. The top of your +file should look like: ```c #include @@ -375,19 +446,22 @@ To start, let's create our enumerator and prototype functions. The top of your f #define DEFAULT_PORT 11111 -/* Create an enum that we will use to tell our +/* Create an enum that we will use to tell our * NonBlocking_ReadWriteAccept() method what to do */ enum read_write_t {WRITE, READ, ACCEPT}; -int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t socketfd, +int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t socketfd, struct sockaddr_in clientAddr); int TCPSelect(socklen_t socketfd); -int NonBlocking_ReadWriteAccept(WOLFSSL* ssl, socklen_t socketfd, +int NonBlocking_ReadWriteAccept(WOLFSSL* ssl, socklen_t socketfd, enum read_write_t rw); ``` -We will start by modifying our `main()` function. Because we are using non-blocking we need to give our socket specific options. To do this, we will make a `setsockopt()` call just above our `wolfSSL_init()` call. It should look something like the following: +We will start by modifying our `main()` function. Because we are using +non-blocking we need to give our socket specific options. To do this, we will +make a `setsockopt()` call just above our `wolfSSL_init()` call. It should look +something like the following: ```c /* If positive value, the socket is valid */ @@ -396,17 +470,22 @@ We will start by modifying our `main()` function. Because we are using non-block exit(EXIT_FAILURE); /* Kill the server with exit status 1 */ } /* Set the sockets options for use with nonblocking i/o */ - if (setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &on, len) - < 0) + if (setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &on, len) + < 0) printf("setsockopt SO_REUSEADDR failed\n"); /* Initialize wolfSSL */ wolfSSL_Init(); ``` -Now we are going to re-write our `AcceptAndRead()` function such that it now takes into consideration non-blocking I/O. This function will be directing our `ssl` object to our client. we will then use a `for ( ; ; )` loop to call our `NonBlocking_ReadWriteAccept()` function passing our `READ` and `WRITE` enums in respectively. This tells our `NonBlocking_ReadWriteAccept()` function what we currently want it to do; read or write. +Now we are going to re-write our `AcceptAndRead()` function such that it now +takes into consideration non-blocking I/O. This function will be directing our +`ssl` object to our client. we will then use a `for ( ; ; )` loop to call our +`NonBlocking_ReadWriteAccept()` function passing our `READ` and `WRITE` enums +in respectively. This tells our `NonBlocking_ReadWriteAccept()` function what +we currently want it to do; read or write. -The function, with more detailed comments should now look like: +The function, with more detailed comments should now look like: ```c int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t socketfd, struct sockaddr_in clientAddr) @@ -431,16 +510,16 @@ int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t socketfd, struct sockaddr_in clien /* Direct our ssl to our clients connection */ wolfSSL_set_fd(ssl, connd); - + /* Sets wolfSSL_accept(ssl) */ if(NonBlocking_ReadWriteAccept(ssl, socketfd, ACCEPT) < 0) return 0; - /* + /* * loop until the connected client disconnects * and read in any messages the client sends */ - for ( ; ; ) { + for ( ; ; ) { /* Read data in when I/O is available */ if (NonBlocking_ReadWriteAccept(ssl, socketfd, READ) == 0) break; @@ -449,19 +528,24 @@ int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t socketfd, struct sockaddr_in clien break; } wolfSSL_free(ssl); /* Free the WOLFSSL object */ - } + } close(connd); /* close the connected socket */ return 0; } ``` -Next we will write the `NonBlocking_ReadWriteAccept()` function. This function in short swtiches between doing `wolfSSL_accept()`, `wolfSSL_read()`, and `wolfSSL_write()`. It uses a while loop to loop on the socket and assign new client connections to the next available socket using the `TCPSelect()` function we will be writing soon. It then asks each socket if it wants read or wants write. This function should look like: +Next we will write the `NonBlocking_ReadWriteAccept()` function. This function +in short swtiches between doing `wolfSSL_accept()`, `wolfSSL_read()`, and +`wolfSSL_write()`. It uses a while loop to loop on the socket and assign new +client connections to the next available socket using the `TCPSelect()` +function we will be writing soon. It then asks each socket if it wants read or +wants write. This function should look like: ```c /* Checks if NonBlocking I/O is wanted, if it is wanted it will * wait until it's available on the socket before reading or writing */ -int NonBlocking_ReadWriteAccept(WOLFSSL* ssl, socklen_t socketfd, +int NonBlocking_ReadWriteAccept(WOLFSSL* ssl, socklen_t socketfd, enum read_write_t rw) { const char reply[] = "I hear ya fa shizzle!\n"; @@ -471,7 +555,7 @@ int NonBlocking_ReadWriteAccept(WOLFSSL* ssl, socklen_t socketfd, int ret; - /* Clear the buffer memory for anything possibly left + /* Clear the buffer memory for anything possibly left over */ memset(&buff, 0, sizeof(buff)); @@ -490,7 +574,7 @@ int NonBlocking_ReadWriteAccept(WOLFSSL* ssl, socklen_t socketfd, int error = wolfSSL_get_error(ssl, 0); /* while I/O is not ready, keep waiting */ - while ((error == SSL_ERROR_WANT_READ || + while ((error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE)) { if (error == SSL_ERROR_WANT_READ) @@ -507,7 +591,7 @@ int NonBlocking_ReadWriteAccept(WOLFSSL* ssl, socklen_t socketfd, rwret = wolfSSL_write(ssl, reply, sizeof(reply)-1); else if (rw == ACCEPT) rwret = wolfSSL_accept(ssl); - + error = wolfSSL_get_error(ssl, 0); } else { @@ -521,7 +605,7 @@ int NonBlocking_ReadWriteAccept(WOLFSSL* ssl, socklen_t socketfd, /* Reply back to the client */ else if (rw == WRITE) { if ((ret = wolfSSL_write(ssl, reply, sizeof(reply)-1)) < 0) { - printf("wolfSSL_write error = %d\n", + printf("wolfSSL_write error = %d\n", wolfSSL_get_error(ssl, ret)); } } @@ -530,7 +614,9 @@ int NonBlocking_ReadWriteAccept(WOLFSSL* ssl, socklen_t socketfd, return 1; } ``` -Lastly, we just need to write our `TCPSelect()` function which will check whether or not any socket is ready for reading and writing and set it accordingly. It should look like: +Lastly, we just need to write our `TCPSelect()` function which will check +whether or not any socket is ready for reading and writing and set it +accordingly. It should look like: ```c int TCPSelect(socklen_t socketfd) @@ -557,7 +643,8 @@ int TCPSelect(socklen_t socketfd) } ``` -And now you should have a functional TCP TLS Server that uses Nonblocking input and output to accept multiple connections without the use of multi-threading. +And now you should have a functional TCP TLS Server that uses Nonblocking input +and output to accept multiple connections without the use of multi-threading. **The finished source code for this can be [found here.](https://github.com/wolfSSL/wolfssl-examples/blob/master/tls/server-tls-nonblocking.c)** @@ -565,13 +652,16 @@ And now you should have a functional TCP TLS Server that uses Nonblocking input ### Basic TLS Client -Again, we will need to import the security library. Just like in the server, add an `#include` statement in your client program. Next we will need to add a global `cert` variable: +Again, we will need to import the security library. Just like in the server, +add an `#include` statement in your client program. Next we will need to add a +global `cert` variable: ```c const char* cert = "../certs/ca-cert.pem"; ``` -Now comes changing the `ClientGreet()` function so its arguments and functions incorporate the security library. +Now comes changing the `ClientGreet()` function so its arguments and functions +incorporate the security library. ```c void ClientGreet(int sock, WOLFSSL* ssl) @@ -581,7 +671,11 @@ if (wolfSSL_write(ssl, send, strlen(send)) != strlen(send)) { if (wolfSSL_read(ssl, receive, MAXDATASIZE) == 0) { ``` -You can think of this as, instead of just a normal read and write, it is now a “secure” read and write. We also need to change the call to `ClientGreet()` in `main()`. Instead of calling directly to it, we should make a call to a `Security()` that will then check the server for the correct `certs`. To do this, change: +You can think of this as, instead of just a normal read and write, it is now a +“secure” read and write. We also need to change the call to `ClientGreet()` in +`main()`. Instead of calling directly to it, we should make a call to a +`Security()` that will then check the server for the correct `certs`. To do +this, change: ```c ClientGreet(sock(fd)); @@ -593,10 +687,11 @@ to Security(sockfd); ``` -Now we just have to make the `Security()` function. It should look something like: +Now we just have to make the `Security()` function. It should look something +like: ```c -/* +/* * applies TLS 1.2 security layer to data being sent. */ int Security(int sock) @@ -638,20 +733,30 @@ int Security(int sock) } ``` -As you can see, this is where we make the call to “greet” the server. This function sends its certification, `../ca-certs.pem` to the server which checks for this. If it’s there, it establishes the connection and secures the information being sent and received between the two. Once this has been done, it frees all the data so no processes remain after the connection has been terminated. +As you can see, this is where we make the call to “greet” the server. This +function sends its certification, `../ca-certs.pem` to the server which checks +for this. If it’s there, it establishes the connection and secures the +information being sent and received between the two. Once this has been done, +it frees all the data so no processes remain after the connection has been +terminated. **The finished source code for this can be [found here.](https://github.com/wolfSSL/wolfssl-examples/blob/master/tls/client-tls.c)** ### Adding Client Session Resumption -In case the connection to the server gets lost, and you want to save time, you’ll want to be able to resume the connection. In this example, we disconnect from the server, then reconnect to the same session afterwards, bypassing the handshake process and ultimately saving time. To accomplish this, you’ll need to add some variable declarations in `Security()`. +In case the connection to the server gets lost, and you want to save time, +you’ll want to be able to resume the connection. In this example, we disconnect +from the server, then reconnect to the same session afterwards, bypassing the +handshake process and ultimately saving time. To accomplish this, you’ll need +to add some variable declarations in `Security()`. ```c -WOLFSSL_SESSION* session = 0;/* wolfssl session */ +WOLFSSL_SESSION* session = 0;/* wolfssl session */ WOLFSSL* sslResume; /* create WOLFSSL object for connection loss */ ``` -Next we'll have to add some code to disconnect and then reconnect to the server. This should be added after your `ClientGreet()` call in `Security()`. +Next we'll have to add some code to disconnect and then reconnect to the +server. This should be added after your `ClientGreet()` call in `Security()`. ```c /* saves the session */ @@ -689,7 +794,7 @@ if (ret != SSL_SUCCESS) { /* checks to see if the new session is the same as the old session */ if (wolfSSL_session_reused(sslResume)) - printf("Re-used session ID\n"); + printf("Re-used session ID\n"); else printf("Did not re-use session ID\n"); @@ -697,23 +802,26 @@ else ret = ClientGreet(sock, sslResume); ``` -We will aslo have to slightly alter our last `wolfSSL_free()` call. Instead of `wolfSSL_free(ssl);` it needs to state `wolfSSL_free(sslResume);` +We will aslo have to slightly alter our last `wolfSSL_free()` call. Instead of +`wolfSSL_free(ssl);` it needs to state `wolfSSL_free(sslResume);` **The finished source code for this can be [found here.](https://github.com/wolfSSL/wolfssl-examples/blob/master/tls/client-tls-resume.c)** ### Adding Client Non-blocking I/O -The first thing that has to be done in order to make a socket a non-blocking socket is to add another library to the top of the code. +The first thing that has to be done in order to make a socket a non-blocking +socket is to add another library to the top of the code. ```c #include /* nonblocking I/O library */ ``` -Then we will need to add a few functions and an `enum` after the `cert` variable. +Then we will need to add a few functions and an `enum` after the `cert` +variable. ```c /* - * enum used for tcp_select function + * enum used for tcp_select function */ enum { TEST_SELECT_FAIL, @@ -786,10 +894,11 @@ int NonBlockConnect(WOLFSSL* ssl) } ``` -We will also need to make some chances to `ClientGreet()` which should now look like this: +We will also need to make some chances to `ClientGreet()` which should now look +like this: ```c -/* +/* * clients initial contact with server. (socket to connect, security layer) */ int ClientGreet(WOLFSSL* ssl) @@ -808,7 +917,7 @@ int ClientGreet(WOLFSSL* ssl) return EXIT_FAILURE; } - ret = wolfSSL_read(ssl, rcvBuff, MAXDATASIZE); + ret = wolfSSL_read(ssl, rcvBuff, MAXDATASIZE); if (ret <= 0) { /* the server failed to send data, or error trying */ ret = wolfSSL_get_error(ssl, 0); @@ -828,7 +937,8 @@ int ClientGreet(WOLFSSL* ssl) } ``` -Now we need to check for non-blocking in our `Security()` function. To do this, we change: +Now we need to check for non-blocking in our `Security()` function. To do this, +we change: ```c wolfSSL_set_fd(ssl, sock); @@ -847,14 +957,17 @@ if (ret == SSL_SUCCESS) { } ``` -Next, we need to change the socket to Non-blocking. This is done by adding the following command after the creation of the socket: +Next, we need to change the socket to Non-blocking. This is done by adding the +following command after the creation of the socket: ```c -/* sets socket to non-blocking */ +/* sets socket to non-blocking */ fcntl(sockfd, F_SETFL, O_NONBLOCK); ``` -Then we will need to delay connection to the port until it finishes rewriting itself to a non-blocking state. So we have to change how we connect. Instead of this: +Then we will need to delay connection to the port until it finishes rewriting +itself to a non-blocking state. So we have to change how we connect. Instead +of this: ```c if (connect(sockfd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) { @@ -869,23 +982,35 @@ we need this: ```c /* keeps trying to connect to the socket until it is able to do so */ -while (ret != 0) - ret = connect(sockfd, (struct sockaddr *) &servAddr, - sizeof(servAddr)); +while (ret != 0) + ret = connect(sockfd, (struct sockaddr *) &servAddr, + sizeof(servAddr)); ``` -This keeps trying to connect to the socket until it stops being busy and allows the connection. +This keeps trying to connect to the socket until it stops being busy and allows +the connection. **The finished source code for this can be [found here.](https://github.com/wolfSSL/wolfssl-examples/blob/master/tls/client-tls-nonblocking.c)** ## Starting the TLS CLient & Server -Before running `make` be sure that SERV_PORT is the same on both source .c files (11111 by default). You must also have the IP address of the machine where the server is going to be on. +Before running `make` be sure that `SERV_PORT` is the same on both source .c +files (11111 by default). You must also have the IP address of the machine +where the server is going to be on. -To do this type `ifconfig` in your terminal and look for `inet addr:` under the `wlan` section. If you are going to be running the client and server off of the same machine look under the `lo` section for the local IP address. +To do this type `ifconfig` in your terminal and look for `inet addr:` under the +`wlan` section. If you are going to be running the client and server off of +the same machine look under the `lo` section for the local IP address. -Run `make` in both the server and client folders, this will compile the source files into an executable file. Anytime you make changes to these source files you will need to re-run `make`. Once this is done you can now start both the client and server. To start the server navigate to the server folder and type `./server-tls` and press enter. To activate the client navigate to the client folder and type `./client-tls ` into your terminal. This should work on both the secured and unsecured programs. +Run `make` in both the server and client folders, this will compile the source +files into an executable file. Anytime you make changes to these source files +you will need to re-run `make`. Once this is done you can now start both the +client and server. To start the server navigate to the server folder and type +`./server-tls` and press enter. To activate the client navigate to the client +folder and type `./client-tls ` into your terminal. This should +work on both the secured and unsecured programs. -Congratulations, you now have a basic client and server that can communicate with each other in an unsecured or secured manner. +Congratulations, you now have a basic client and server that can communicate +with each other in an unsecured or secured manner. ======= diff --git a/tls/client-callback.c b/tls/client-callback.c index 78021ec7..34d2a168 100644 --- a/tls/client-callback.c +++ b/tls/client-callback.c @@ -51,11 +51,11 @@ #define MAXSZ 1024 #define YASSLPORT 11111 -#define SOCKET_EWOULDBLOCK EWOULDBLOCK -#define SOCKET_EAGAIN EAGAIN -#define SOCKET_ECONNRESET ECONNRESET -#define SOCKET_EINTR EINTR -#define SOCKET_EPIPE EPIPE +#define SOCKET_EWOULDBLOCK EWOULDBLOCK +#define SOCKET_EAGAIN EAGAIN +#define SOCKET_ECONNRESET ECONNRESET +#define SOCKET_EINTR EINTR +#define SOCKET_EPIPE EPIPE #define SOCKET_ECONNREFUSED ECONNREFUSED #define SOCKET_ECONNABORTED ECONNABORTED @@ -109,7 +109,7 @@ int CbIORecv(WOLFSSL *ssl, char *buf, int sz, void *ctx) } printf("Received %d bytes\n", sz); - + return recvd; } @@ -151,7 +151,7 @@ int CbIOSend(WOLFSSL *ssl, char *buf, int sz, void *ctx) } printf("CbIOSend: sent %d bytes to %d\n", sz, sd); - + return sent; } @@ -160,7 +160,7 @@ int Client(const char* ip, word16 port) { int n; char msg[] = "hello wolfssl"; - char reply[MAXSZ]; + char reply[MAXSZ]; int msgSz = strlen(msg); SOCKET_T fd; WOLFSSL_CTX* ctx; @@ -168,7 +168,7 @@ int Client(const char* ip, word16 port) if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) err_sys("Error in setting client ctx\n"); - + if (wolfSSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_SUCCESS) err_sys("trouble loading client cert"); if (wolfSSL_CTX_use_certificate_file(ctx, cliCert, SSL_FILETYPE_PEM) diff --git a/tls/client-tls-nonblocking.c b/tls/client-tls-nonblocking.c index d74504d1..226855bb 100644 --- a/tls/client-tls-nonblocking.c +++ b/tls/client-tls-nonblocking.c @@ -19,11 +19,11 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ #include -#include +#include #include #include #include -#include /* wolfSSL security library */ +#include /* wolfSSL security library */ #include /* nonblocking I/O library */ #include @@ -33,7 +33,7 @@ const char* cert = "../certs/ca-cert.pem"; /* - * enum used for tcp_select function + * enum used for tcp_select function */ enum { TEST_SELECT_FAIL, @@ -105,7 +105,7 @@ int NonBlockConnect(WOLFSSL* ssl) return ret; } -/* +/* * clients initial contact with server. (socket to connect, security layer) */ int ClientGreet(WOLFSSL* ssl) @@ -113,7 +113,7 @@ int ClientGreet(WOLFSSL* ssl) /* data to send to the server, data recieved from the server */ char sendBuff[MAXDATASIZE], rcvBuff[MAXDATASIZE] = {0}; int ret = 0; -int count = 0; + int count = 0; printf("Message for server:\t"); fgets(sendBuff, MAXDATASIZE, stdin); @@ -125,16 +125,16 @@ int count = 0; return EXIT_FAILURE; } - ret = wolfSSL_read(ssl, rcvBuff, MAXDATASIZE); + ret = wolfSSL_read(ssl, rcvBuff, MAXDATASIZE); if (ret <= 0) { /* the server failed to send data, or error trying */ ret = wolfSSL_get_error(ssl, 0); while (ret == SSL_ERROR_WANT_READ) { -count++; + count++; ret = wolfSSL_read(ssl, rcvBuff, MAXDATASIZE); ret = wolfSSL_get_error(ssl, 0); } -printf("counter %d\n", count); + printf("counter %d\n", count); if (ret < 0) { ret = wolfSSL_get_error(ssl, 0); printf("Read error. Error: %d\n", ret); @@ -146,14 +146,14 @@ printf("counter %d\n", count); return ret; } -/* +/* * applies TLS 1.2 security layer to data being sent. */ int Security(int sock) { WOLFSSL_CTX* ctx; - WOLFSSL* ssl; /* create WOLFSSL object */ - int ret = 0; + WOLFSSL* ssl; /* create WOLFSSL object */ + int ret = 0; wolfSSL_Init(); /* initialize wolfSSL */ @@ -164,7 +164,7 @@ int Security(int sock) } /* load CA certificates into wolfSSL_CTX. which will verify the server */ - if (wolfSSL_CTX_load_verify_locations(ctx, cert, 0) != + if (wolfSSL_CTX_load_verify_locations(ctx, cert, 0) != SSL_SUCCESS) { printf("Error loading %s. Please check the file.\n", cert); return EXIT_FAILURE; @@ -185,14 +185,14 @@ int Security(int sock) wolfSSL_free(ssl); wolfSSL_CTX_free(ctx); wolfSSL_Cleanup(); - + return ret; } -/* - * Command line argumentCount and argumentValues +/* + * Command line argumentCount and argumentValues */ -int main(int argc, char** argv) +int main(int argc, char** argv) { int sockfd; /* socket file descriptor */ struct sockaddr_in servAddr; /* struct for server address */ @@ -211,11 +211,11 @@ int main(int argc, char** argv) printf("Failed to create socket. Error: %i\n", ret); return EXIT_FAILURE; } - + fcntl(sockfd, F_SETFL, O_NONBLOCK); /* sets socket to non-blocking */ 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 */ + 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) { @@ -226,9 +226,9 @@ int main(int argc, char** argv) } /* keeps trying to connect to the socket until it is able to do so */ - while (ret != 0) - ret = connect(sockfd, (struct sockaddr *) &servAddr, - sizeof(servAddr)); + while (ret != 0) + ret = connect(sockfd, (struct sockaddr *) &servAddr, + sizeof(servAddr)); Security(sockfd); diff --git a/tls/client-tls-perf.c b/tls/client-tls-perf.c old mode 100755 new mode 100644 diff --git a/tls/client-tls-resume.c b/tls/client-tls-resume.c index 9c28dcbc..8736b1a9 100644 --- a/tls/client-tls-resume.c +++ b/tls/client-tls-resume.c @@ -20,18 +20,18 @@ */ #include #include -#include +#include #include #include #include -#include /* wolfSSL security library */ +#include /* wolfSSL security library */ #define MAXDATASIZE 4096 /* maximum acceptable amount of data */ #define SERV_PORT 11111 /* define default port number */ const char* cert = "../certs/ca-cert.pem"; -/* +/* * clients initial contact with server. (socket to connect, security layer) */ int ClientGreet(int sock, WOLFSSL* ssl) @@ -61,7 +61,7 @@ int ClientGreet(int sock, WOLFSSL* ssl) return ret; } -/* +/* * applies TLS 1.2 security layer to data being sent. */ int Security(int sock, struct sockaddr_in addr) @@ -92,31 +92,31 @@ int Security(int sock, struct sockaddr_in addr) } wolfSSL_set_fd(ssl, sock); - + /* connects to wolfSSL */ ret = wolfSSL_connect(ssl); if (ret != SSL_SUCCESS) { return ret; } - + ret = ClientGreet(sock, ssl); - + /* saves the session */ session = wolfSSL_get_session(ssl); wolfSSL_free(ssl); /* closes the connection */ close(sock); - + /* new ssl to reconnect to */ sslResume = wolfSSL_new(ctx); - + /* makes a new socket to connect to */ sock = socket(AF_INET, SOCK_STREAM, 0); - + /* sets session to old session */ wolfSSL_set_session(sslResume, session); - + /* connects to new socket */ if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) { /* if socket fails to connect to the server*/ @@ -124,28 +124,28 @@ int Security(int sock, struct sockaddr_in addr) printf("Connect error. Error: %i\n", ret); return EXIT_FAILURE; } - + /* sets new file discriptior */ wolfSSL_set_fd(sslResume, sock); - + /* reconects to wolfSSL */ ret = wolfSSL_connect(sslResume); if (ret != SSL_SUCCESS) { return ret; } - + /* checks to see if the new session is the same as the old session */ if (wolfSSL_session_reused(sslResume)) - printf("Re-used session ID\n"); + printf("Re-used session ID\n"); else printf("Did not re-use session ID\n"); - + /* regreet the client */ ret = ClientGreet(sock, sslResume); - + /* closes the connection */ close(sock); - + /* frees all data before client termination */ wolfSSL_free(sslResume); wolfSSL_CTX_free(ctx); @@ -154,10 +154,10 @@ int Security(int sock, struct sockaddr_in addr) return ret; } -/* - * Command line argumentCount and argumentValues +/* + * Command line argumentCount and argumentValues */ -int main(int argc, char** argv) +int main(int argc, char** argv) { int sockfd; /* socket file descriptor */ struct sockaddr_in servAddr; /* struct for server address */ @@ -177,7 +177,7 @@ int main(int argc, char** argv) return EXIT_FAILURE; } - memset(&servAddr, 0, sizeof(servAddr)); /* clears memory block for use */ + 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 */ @@ -189,7 +189,7 @@ int main(int argc, char** argv) return EXIT_FAILURE; } - if ((ret = connect(sockfd, (struct sockaddr *) &servAddr, + if ((ret = connect(sockfd, (struct sockaddr *) &servAddr, sizeof(servAddr))) < 0) { /* if socket fails to connect to the server*/ ret = errno; diff --git a/tls/client-tls-writedup.c b/tls/client-tls-writedup.c index 069092ff..36147b8c 100644 --- a/tls/client-tls-writedup.c +++ b/tls/client-tls-writedup.c @@ -35,8 +35,8 @@ #include #include #include -#include /* wolfSSL build options */ -#include /* wolfSSL security library */ +#include /* wolfSSL build options */ +#include /* wolfSSL security library */ #define MAXDATASIZE 256 /* maximum acceptable amount of data */ #define MAXTIMES 1024*4 @@ -152,7 +152,7 @@ int Security(int sock) } else { printf("wolfSSL_connect err = %d\n", wolfSSL_get_error(ssl, 0)); return EXIT_FAILURE; - } + } /* frees all data before client termination */ wolfSSL_free(ssl); diff --git a/tls/server-callback.c b/tls/server-callback.c index f16885a2..6ab5a22b 100644 --- a/tls/server-callback.c +++ b/tls/server-callback.c @@ -160,11 +160,11 @@ int Server(word16 port) WOLFSSL* ssl; wolfSSL_Init(); - + /* create ctx and configure certificates */ if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())) == NULL) err_sys("Fatal error : wolfSSL_CTX_new error"); - + if (wolfSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM) != SSL_SUCCESS) err_sys("can't load server cert file," @@ -174,13 +174,13 @@ int Server(word16 port) != SSL_SUCCESS) err_sys("can't load server key file, " "Please run from wolfSSL home dir"); - + /*sets the IO callback methods*/ wolfSSL_SetIORecv(ctx, CbIORecv); wolfSSL_SetIOSend(ctx, CbIOSend); tcp_accept(&listenfd, &connfd, NULL, port, 1, 0, 0, 0, 1); - + if (connfd < 0) { err_sys("Fatal error : accept error"); } @@ -188,28 +188,28 @@ int Server(word16 port) /* create WOLFSSL object and respond */ if ((ssl = wolfSSL_new(ctx)) == NULL) err_sys("Fatal error : wolfSSL_new error"); - + wolfSSL_set_fd(ssl, connfd); - memset(msg, 0, MAXSZ); - n = wolfSSL_read(ssl, msg, MAXSZ - 1); - if (n > 0) { - msg[n] = '\0'; - printf("Client sent : %s\n", msg); - if (wolfSSL_write(ssl, reply, strlen(reply)) > strlen(reply)) - err_sys("Fatal error : respond: write error"); - } + memset(msg, 0, MAXSZ); + n = wolfSSL_read(ssl, msg, MAXSZ - 1); + if (n > 0) { + msg[n] = '\0'; + printf("Client sent : %s\n", msg); + if (wolfSSL_write(ssl, reply, strlen(reply)) > strlen(reply)) + err_sys("Fatal error : respond: write error"); + } + + if (n < 0) + err_sys("Fatal error :respond: read error"); - if (n < 0) - err_sys("Fatal error :respond: read error"); - /* closes the connections after responding */ wolfSSL_shutdown(ssl); wolfSSL_free(ssl); if (close(listenfd) == -1 && close(connfd) == -1) err_sys("Fatal error : close error"); } - + /* free up memory used by wolfSSL */ wolfSSL_CTX_free(ctx); diff --git a/tls/server-tcp.c b/tls/server-tcp.c index 2f00fe0d..1f74db66 100644 --- a/tls/server-tcp.c +++ b/tls/server-tcp.c @@ -35,7 +35,7 @@ int AcceptAndRead(socklen_t sockfd); int AcceptAndRead(socklen_t sockfd) { struct sockaddr_in clientAddr; - socklen_t size = sizeof(clientAddr); + socklen_t size = sizeof(clientAddr); int ret = 0; /* Wait until a client connects */ @@ -56,7 +56,7 @@ int AcceptAndRead(socklen_t sockfd) /* Clear the buffer memory for anything possibly left over */ bzero(&buff, sizeof(buff)); - + /* Read the client data into our buff array */ if ((ret = read(connd, buff, sizeof(buff)-1)) > 0){ /* Print any data the client sends to the console */ @@ -64,7 +64,7 @@ int AcceptAndRead(socklen_t sockfd) /* Create our reply message */ char reply[] = "I hear ya fa shizzle!\n"; - + /* Reply back to the client */ if ((ret = write(connd, reply, sizeof(reply)-1)) < 0) printf("write error\n"); @@ -77,37 +77,37 @@ int AcceptAndRead(socklen_t sockfd) /* Close the socket */ close(connd); - + return 0; } int main() { - /* + /* * Creates a socket that uses an internet IP address, * Sets the type to be Stream based (TCP), * 0 means choose the default protocol. */ /* Identify and access the sockets */ - socklen_t sockfd = socket(AF_INET, SOCK_STREAM, 0); - int exit = 0; /* 0 = false, 1 = true */ + socklen_t sockfd = socket(AF_INET, SOCK_STREAM, 0); + int exit = 0; /* 0 = false, 1 = true */ /* If positive value, the socket is valid */ if(sockfd == -1){ printf("ERROR: failed to create the socket\n"); - return 1; /* Kill the server with exit status 1 */ + return 1; /* Kill the server with exit status 1 */ } /* Server and client socket address structures */ - struct sockaddr_in serverAddr; + struct sockaddr_in serverAddr; - /* Initialize the server address struct to zero */ - memset((char *)&serverAddr, 0, sizeof(serverAddr)); + /* Initialize the server address struct to zero */ + memset((char *)&serverAddr, 0, sizeof(serverAddr)); /* Fill the server's address family */ - serverAddr.sin_family = AF_INET; + serverAddr.sin_family = AF_INET; serverAddr.sin_addr.s_addr = INADDR_ANY; serverAddr.sin_port = htons(DEFAULT_PORT); @@ -124,7 +124,7 @@ int main() printf("Waiting for a connection...\n"); /* Accept client connections and read from them */ - exit = AcceptAndRead(sockfd); + exit = AcceptAndRead(sockfd); } /* Close the open sockets */ diff --git a/tls/server-tls-epoll-perf.c b/tls/server-tls-epoll-perf.c old mode 100755 new mode 100644 index aad0fc0b..f5b49757 --- a/tls/server-tls-epoll-perf.c +++ b/tls/server-tls-epoll-perf.c @@ -54,11 +54,11 @@ #define OPTIONS "?p:v:al:c:k:A:n:N:R:W:B:" /* The default server certificate. */ -#define SVR_CERT "../certs/server-cert.pem" +#define SVR_CERT "../certs/server-cert.pem" /* The default server private key. */ -#define SVR_KEY "../certs/server-key.pem" +#define SVR_KEY "../certs/server-key.pem" /* The default certificate/CA file for the client. */ -#define CLI_CERT "../certs/client-cert.pem" +#define CLI_CERT "../certs/client-cert.pem" /* The states of the SSL connection. */ typedef enum SSLState { ACCEPT, READ, WRITE, CLOSED } SSLState; diff --git a/tls/server-tls-epoll-threaded.c b/tls/server-tls-epoll-threaded.c old mode 100755 new mode 100644 index e67ee436..3aaa6b6c --- a/tls/server-tls-epoll-threaded.c +++ b/tls/server-tls-epoll-threaded.c @@ -56,11 +56,11 @@ #define OPTIONS "?p:v:al:c:k:A:t:n:N:R:W:B:" /* The default server certificate. */ -#define SVR_CERT "../certs/server-cert.pem" +#define SVR_CERT "../certs/server-cert.pem" /* The default server private key. */ -#define SVR_KEY "../certs/server-key.pem" +#define SVR_KEY "../certs/server-key.pem" /* The default certificate/CA file for the client. */ -#define CLI_CERT "../certs/client-cert.pem" +#define CLI_CERT "../certs/client-cert.pem" /* The states of the SSL connection. */ typedef enum SSLState { ACCEPT, READ, WRITE, CLOSED } SSLState; diff --git a/tls/server-tls-nonblocking.c b/tls/server-tls-nonblocking.c index 1b28f9e7..9bb86cd0 100644 --- a/tls/server-tls-nonblocking.c +++ b/tls/server-tls-nonblocking.c @@ -38,15 +38,15 @@ #define DEFAULT_PORT 11111 -/* Create an enum that we will use to tell our +/* Create an enum that we will use to tell our * NonBlocking_ReadWriteAccept() method what to do */ enum read_write_t {WRITE, READ, ACCEPT}; -int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t socketfd, +int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t socketfd, struct sockaddr_in clientAddr); int TCPSelect(socklen_t socketfd); -int NonBlocking_ReadWriteAccept(WOLFSSL* ssl, socklen_t socketfd, +int NonBlocking_ReadWriteAccept(WOLFSSL* ssl, socklen_t socketfd, enum read_write_t rw); /* Check if any sockets are ready for reading and writing and set it */ @@ -75,17 +75,17 @@ int TCPSelect(socklen_t socketfd) } /* Checks if NonBlocking I/O is wanted, if it is wanted it will * wait until it's available on the socket before reading or writing */ -int NonBlocking_ReadWriteAccept(WOLFSSL* ssl, socklen_t socketfd, +int NonBlocking_ReadWriteAccept(WOLFSSL* ssl, socklen_t socketfd, enum read_write_t rw) { const char reply[] = "I hear ya fa shizzle!\n"; char buff[256]; int rwret = 0; int selectRet; - int ret; + int ret; - /* Clear the buffer memory for anything possibly left + /* Clear the buffer memory for anything possibly left over */ memset(&buff, 0, sizeof(buff)); @@ -104,7 +104,7 @@ int NonBlocking_ReadWriteAccept(WOLFSSL* ssl, socklen_t socketfd, int error = wolfSSL_get_error(ssl, 0); /* while I/O is not ready, keep waiting */ - while ((error == SSL_ERROR_WANT_READ || + while ((error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE)) { if (error == SSL_ERROR_WANT_READ) @@ -121,7 +121,7 @@ int NonBlocking_ReadWriteAccept(WOLFSSL* ssl, socklen_t socketfd, rwret = wolfSSL_write(ssl, reply, sizeof(reply)-1); else if (rw == ACCEPT) rwret = wolfSSL_accept(ssl); - + error = wolfSSL_get_error(ssl, 0); } else { @@ -135,7 +135,7 @@ int NonBlocking_ReadWriteAccept(WOLFSSL* ssl, socklen_t socketfd, /* Reply back to the client */ else if (rw == WRITE) { if ((ret = wolfSSL_write(ssl, reply, sizeof(reply)-1)) < 0) { - printf("wolfSSL_write error = %d\n", + printf("wolfSSL_write error = %d\n", wolfSSL_get_error(ssl, ret)); } } @@ -166,16 +166,16 @@ int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t socketfd, struct sockaddr_in clien /* Direct our ssl to our clients connection */ wolfSSL_set_fd(ssl, connd); - + /* Sets wolfSSL_accept(ssl) */ if(NonBlocking_ReadWriteAccept(ssl, socketfd, ACCEPT) < 0) return 0; - /* + /* * loop until the connected client disconnects * and read in any messages the client sends */ - for ( ; ; ) { + for ( ; ; ) { /* Read data in when I/O is available */ if (NonBlocking_ReadWriteAccept(ssl, socketfd, READ) == 0) break; @@ -184,7 +184,7 @@ int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t socketfd, struct sockaddr_in clien break; } wolfSSL_free(ssl); /* Free the WOLFSSL object */ - } + } close(connd); /* close the connected socket */ return 0; @@ -193,7 +193,7 @@ int AcceptAndRead(WOLFSSL_CTX* ctx, socklen_t socketfd, struct sockaddr_in clien int main() { - /* + /* * Creates a socket that uses an internet IP address, * Sets the type to be Stream based (TCP), * 0 means choose the default protocol. @@ -210,13 +210,13 @@ int main() struct sockaddr_in serverAddr = {0}, clientAddr = {0}; /* Initialize the server address struct to zero */ - memset((char *)&serverAddr, 0, sizeof(serverAddr)); + memset((char *)&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); - + socklen_t len = sizeof(on); /* If positive value, the socket is valid */ @@ -225,8 +225,8 @@ int main() exit(EXIT_FAILURE); /* Kill the server with exit status 1 */ } /* Set the sockets options for use with nonblocking i/o */ - if (setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &on, len) - < 0) + if (setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &on, len) + < 0) printf("setsockopt SO_REUSEADDR failed\n"); /* Initialize wolfSSL */ @@ -239,7 +239,7 @@ int main() } /* Load server certificate into WOLFSSL_CTX */ - if (wolfSSL_CTX_use_certificate_file(ctx, "../certs/server-cert.pem", + 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"); @@ -247,7 +247,7 @@ int main() } /* Load server key into WOLFSSL_CTX */ - if (wolfSSL_CTX_use_PrivateKey_file(ctx, "../certs/server-key.pem", + 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"); diff --git a/tls/server-tls-threaded.c b/tls/server-tls-threaded.c index 9cd35689..b686da36 100644 --- a/tls/server-tls-threaded.c +++ b/tls/server-tls-threaded.c @@ -20,8 +20,8 @@ *============================================================================= * * 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. + * might look like. This server can also resume the session if a client + * inadvertantly disconnects. */ #include @@ -73,9 +73,9 @@ void *ThreadHandler(void* socketDesc) if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) > 0) { /* Print any data the client sends to the console */ printf("Client on Socket %d: %s\n", connd, buff); - + /* Reply back to the client */ - if ((ret = wolfSSL_write(ssl, reply, sizeof(reply)-1)) + if ((ret = wolfSSL_write(ssl, reply, sizeof(reply)-1)) < 0) { printf("wolfSSL_write error = %d\n", wolfSSL_get_error(ssl, ret)); } @@ -106,7 +106,7 @@ int AcceptAndRead(socklen_t sockfd, struct sockaddr_in clientAddr) pthread_t thread_id; /* Wait until a client connects */ - while ((connd = accept(sockfd, (struct sockaddr *)&clientAddr, + while ((connd = accept(sockfd, (struct sockaddr *)&clientAddr, &size))) { /* Pass the client into a new thread */ if (pthread_create(&thread_id, NULL, ThreadHandler, (void *) @@ -125,7 +125,7 @@ int AcceptAndRead(socklen_t sockfd, struct sockaddr_in clientAddr) int main() { - /* + /* * Creates a socket that uses an internet IP address, * Sets the type to be Stream based (TCP), * 0 means choose the default protocol. @@ -138,7 +138,7 @@ int main() struct sockaddr_in serverAddr = {0}, clientAddr = {0}; /* Initialize the server address struct to zero */ - memset((char *)&serverAddr, 0, sizeof(serverAddr)); + memset((char *)&serverAddr, 0, sizeof(serverAddr)); /* Fill the server's address family */ serverAddr.sin_family = AF_INET; @@ -151,7 +151,7 @@ int main() /* If positive value, the socket is valid */ if (sockfd == -1) { printf("ERROR: failed to create the socket\n"); - return EXIT_FAILURE; + return EXIT_FAILURE; } /* Create and initialize WOLFSSL_CTX structure */ @@ -161,7 +161,7 @@ int main() } /* Load server certificate into WOLFSSL_CTX */ - if (wolfSSL_CTX_use_certificate_file(ctx, "../certs/server-cert.pem", + 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"); @@ -169,7 +169,7 @@ int main() } /* Load server key into WOLFSSL_CTX */ - if (wolfSSL_CTX_use_PrivateKey_file(ctx, "../certs/server-key.pem", + 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"); @@ -198,6 +198,6 @@ int main() wolfSSL_CTX_free(ctx); /* Free WOLFSSL_CTX */ wolfSSL_Cleanup(); /* Free wolfSSL */ - + return EXIT_SUCCESS; }