From 200708b5f513012b5bf2d77b6bbb02948fd2c4ce Mon Sep 17 00:00:00 2001 From: Conner Date: Tue, 16 May 2017 16:29:22 -0600 Subject: [PATCH] Finished changing the files in /psk to more closely follow the coding standard. --- psk/client-psk-nonblocking.c | 63 ++++++++++++++++-------------- psk/client-psk-resume.c | 43 ++++++++++---------- psk/client-psk.c | 42 ++++++++++---------- psk/client-tcp.c | 24 ++++++------ psk/server-psk-nonblocking.c | 76 +++++++++++++++++++++--------------- 5 files changed, 133 insertions(+), 115 deletions(-) diff --git a/psk/client-psk-nonblocking.c b/psk/client-psk-nonblocking.c index 0d221176..11dfdf13 100644 --- a/psk/client-psk-nonblocking.c +++ b/psk/client-psk-nonblocking.c @@ -36,7 +36,7 @@ #define SERV_PORT 11111 /* default port*/ /* - * enum used for tcp_select function + * enum used for tcp_select function */ enum { TEST_SELECT_FAIL, @@ -60,25 +60,28 @@ static inline int tcp_select(int socketfd, int to_sec) result = select(nfds, &recvfds, NULL, &errfds, &timeout); - if (result == 0) + if (result == 0) { return TEST_TIMEOUT; + } else if (result > 0) { - if (FD_ISSET(socketfd, &recvfds)) + if (FD_ISSET(socketfd, &recvfds)) { return TEST_RECV_READY; - else if(FD_ISSET(socketfd, &errfds)) + } + else if(FD_ISSET(socketfd, &errfds)) { return TEST_ERROR_READY; + } } return TEST_SELECT_FAIL; } /* - * sets up and uses nonblocking protocols using wolfssl + * sets up and uses nonblocking protocols using wolfssl */ static int NonBlockingSSL_Connect(WOLFSSL* ssl) { int ret, error, sockfd, select_ret, currTimeout; - + ret = wolfSSL_connect(ssl); error = wolfSSL_get_error(ssl, 0); sockfd = (int)wolfSSL_get_fd(ssl); @@ -87,10 +90,12 @@ static int NonBlockingSSL_Connect(WOLFSSL* ssl) error == SSL_ERROR_WANT_WRITE)) { currTimeout = 1; - if (error == SSL_ERROR_WANT_READ) + if (error == SSL_ERROR_WANT_READ) { printf("... client would read block\n"); - else + } + else { printf("... client would write block\n"); + } select_ret = tcp_select(sockfd, currTimeout); @@ -118,7 +123,7 @@ static int NonBlockingSSL_Connect(WOLFSSL* ssl) *psk client set up. */ static inline unsigned int My_Psk_Client_Cb(WOLFSSL* ssl, const char* hint, - char* identity, unsigned int id_max_len, unsigned char* key, + char* identity, unsigned int id_max_len, unsigned char* key, unsigned int key_max_len) { (void)ssl; @@ -139,20 +144,20 @@ static inline unsigned int My_Psk_Client_Cb(WOLFSSL* ssl, const char* hint, } /* - * this function will send the inputted string to the server and then + * this function will send the inputted string to the server and then * recieve the string from the server outputing it to the termial - */ + */ int SendReceive(WOLFSSL* ssl) { char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ char recvline[MAXLINE]; /* string received from the server */ - + /* write string to the server */ if (wolfSSL_write(ssl, sendline, MAXLINE) != sizeof(sendline)) { printf("Write Error to Server\n"); return 1; } - + /* flags if the Server stopped before the client could end */ if (wolfSSL_read(ssl, recvline, MAXLINE) < 0 ) { printf("Client: Server Terminated Prematurely!\n"); @@ -161,7 +166,7 @@ int SendReceive(WOLFSSL* ssl) /* show message from the server */ printf("Server Message: %s\n", recvline); - + return 0; } @@ -177,20 +182,20 @@ int main(int argc, char **argv) printf("Usage: tcpClient \n"); return 1; } - + wolfSSL_Init(); /* initialize wolfSSL */ - - + + /* create and initialize WOLFSSL_CTX structure */ if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) { fprintf(stderr, "SSL_CTX_new error.\n"); return 1; - } - + } + /* create a stream socket using tcp,internet protocal IPv4, * full-duplex stream */ sockfd = socket(AF_INET, SOCK_STREAM, 0); - + /* places n zero-valued bytes in the address servaddr */ memset(&servaddr, 0, sizeof(servaddr)); @@ -199,23 +204,23 @@ int main(int argc, char **argv) /* converts IPv4 addresses from text to binary form */ ret = inet_pton(AF_INET, argv[1], &servaddr.sin_addr); - + if (ret != 1) { printf("inet_pton error\n"); return 1; } - + /* set up pre shared keys */ wolfSSL_CTX_set_psk_client_callback(ctx,My_Psk_Client_Cb); /* attempts to make a connection on a socket */ ret = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); - + if (ret != 0) { printf("Connection Error\n"); return 1; } - + /* create wolfSSL object after each tcp connect */ if ((ssl = wolfSSL_new(ctx)) == NULL) { fprintf(stderr, "wolfSSL_new error.\n"); @@ -228,7 +233,7 @@ int main(int argc, char **argv) /* tell wolfSSL that nonblocking is going to be used */ wolfSSL_set_using_nonblock(ssl, 1); - /* invokes the fcntl callable service to get the file status + /* invokes the fcntl callable service to get the file status * flags for a file. checks if it returns an error, if it does * stop program */ int flags = fcntl(sockfd, F_GETFL, 0); @@ -238,8 +243,8 @@ int main(int argc, char **argv) } /* invokes the fcntl callable service to set file status flags. - * Do not block an open, a read, or a write on the file - * (do not wait for terminal input. If an error occurs, + * Do not block an open, a read, or a write on the file + * (do not wait for terminal input. If an error occurs, * stop program*/ flags = fcntl(sockfd, F_SETFL, flags | O_NONBLOCK); if (flags < 0) { @@ -262,11 +267,11 @@ int main(int argc, char **argv) /* cleanup */ wolfSSL_free(ssl); - /* when completely done using SSL/TLS, free the + /* when completely done using SSL/TLS, free the * wolfssl_ctx object */ wolfSSL_CTX_free(ctx); wolfSSL_Cleanup(); - + return ret; } diff --git a/psk/client-psk-resume.c b/psk/client-psk-resume.c index 35746c3d..000361fc 100644 --- a/psk/client-psk-resume.c +++ b/psk/client-psk-resume.c @@ -40,7 +40,7 @@ *psk client set up. */ static inline unsigned int My_Psk_Client_Cb(WOLFSSL* ssl, const char* hint, - char* identity, unsigned int id_max_len, unsigned char* key, + char* identity, unsigned int id_max_len, unsigned char* key, unsigned int key_max_len) { (void)ssl; @@ -61,20 +61,20 @@ static inline unsigned int My_Psk_Client_Cb(WOLFSSL* ssl, const char* hint, } /* - * this function will send the inputted string to the server and then + * this function will send the inputted string to the server and then * recieve the string from the server outputing it to the termial - */ + */ int SendReceive(WOLFSSL* ssl) { char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ char recvline[MAXLINE]; /* string received from the server */ - + /* write string to the server */ if (wolfSSL_write(ssl, sendline, MAXLINE) != sizeof(sendline)) { printf("Write Error to Server\n"); return 1; } - + /* flags if the Server stopped before the client could end */ if (wolfSSL_read(ssl, recvline, MAXLINE) < 0 ) { printf("Client: Server Terminated Prematurely!\n"); @@ -83,12 +83,12 @@ int SendReceive(WOLFSSL* ssl) /* show message from the server */ printf("Server Message: %s\n", recvline); - - return 0; + + return 0; } int main(int argc, char **argv){ - + int sockfd, sock, ret; WOLFSSL* ssl; WOLFSSL* sslResume = 0; @@ -101,19 +101,19 @@ int main(int argc, char **argv){ printf("Usage: tcpClient \n"); return 1; } - + wolfSSL_Init(); /* initialize wolfSSL */ - + /* create and initialize WOLFSSL_CTX structure */ if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) { fprintf(stderr, "SSL_CTX_new error.\n"); return 1; } - + /* create a stream socket using tcp,internet protocal IPv4, * full-duplex stream */ sockfd = socket(AF_INET, SOCK_STREAM, 0); - + /* places n zero-valued bytes in the address servaddr */ memset(&servaddr, 0, sizeof(servaddr)); @@ -122,11 +122,11 @@ int main(int argc, char **argv){ /* converts IPv4 addresses from text to binary form */ ret = inet_pton(AF_INET, argv[1], &servaddr.sin_addr); - + if (ret != 1){ return 1; } - + /* set up pre shared keys */ wolfSSL_CTX_set_psk_client_callback(ctx, My_Psk_Client_Cb); @@ -135,7 +135,7 @@ int main(int argc, char **argv){ if (ret != 0 ){ return 1; } - + /* create wolfSSL object after each tcp connect */ if ( (ssl = wolfSSL_new(ctx)) == NULL) { fprintf(stderr, "wolfSSL_new error.\n"); @@ -164,15 +164,15 @@ int main(int argc, char **argv){ wolfSSL_Cleanup(); /* - * resume session, start new connection and socket + * resume session, start new connection and socket */ /* start a new socket connection */ sock = socket(AF_INET, SOCK_STREAM, 0); - + /* connect to the socket */ ret = connect(sock, (struct sockaddr *) &servaddr, sizeof(servaddr)); - + if (ret != 0){ return 1; } @@ -194,8 +194,9 @@ int main(int argc, char **argv){ } /* check to see if the session id is being reused */ - if (wolfSSL_session_reused(sslResume)) + if (wolfSSL_session_reused(sslResume)) { printf("reused session id\n"); + } else printf("didn't reuse session id!!!\n"); @@ -206,9 +207,9 @@ int main(int argc, char **argv){ close(sock); /* clean up */ - wolfSSL_free(sslResume); + wolfSSL_free(sslResume); wolfSSL_CTX_free(ctx); wolfSSL_Cleanup(); - + return ret; } diff --git a/psk/client-psk.c b/psk/client-psk.c index 62cdd0c7..cbaf6f71 100755 --- a/psk/client-psk.c +++ b/psk/client-psk.c @@ -38,7 +38,7 @@ *psk client set up. */ static inline unsigned int My_Psk_Client_Cb(WOLFSSL* ssl, const char* hint, - char* identity, unsigned int id_max_len, unsigned char* key, + char* identity, unsigned int id_max_len, unsigned char* key, unsigned int key_max_len) { (void)ssl; @@ -59,20 +59,20 @@ static inline unsigned int My_Psk_Client_Cb(WOLFSSL* ssl, const char* hint, } /* - * this function will send the inputted string to the server and then + * this function will send the inputted string to the server and then * recieve the string from the server outputing it to the termial - */ + */ int SendReceive(WOLFSSL* ssl) { char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ char recvline[MAXLINE]; /* string received from the server */ - + /* write string to the server */ if (wolfSSL_write(ssl, sendline, MAXLINE) != sizeof(sendline)) { printf("Write Error to Server\n"); return 1; } - + /* flags if the Server stopped before the client could end */ if (wolfSSL_read(ssl, recvline, MAXLINE) < 0 ) { printf("Client: Server Terminated Prematurely!\n"); @@ -81,7 +81,7 @@ int SendReceive(WOLFSSL* ssl) /* show message from the server */ printf("Server Message: %s\n", recvline); - + return 0; } @@ -97,19 +97,19 @@ int main(int argc, char **argv) printf("Usage: tcpClient \n"); return 1; } - + wolfSSL_Init(); /* initialize wolfSSL */ - + /* create and initialize WOLFSSL_CTX structure */ if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) { fprintf(stderr, "SSL_CTX_new error.\n"); return 1; } - + /* create a stream socket using tcp,internet protocal IPv4, * full-duplex stream */ sockfd = socket(AF_INET, SOCK_STREAM, 0); - + /* places n zero-valued bytes in the address servaddr */ memset(&servaddr, 0, sizeof(servaddr)); @@ -120,44 +120,44 @@ int main(int argc, char **argv) ret = inet_pton(AF_INET, argv[1], &servaddr.sin_addr); if (ret != 1) { - printf("inet_pton error\n"); + printf("inet_pton error\n"); return 1; } - + /* set up pre shared keys */ wolfSSL_CTX_set_psk_client_callback(ctx, My_Psk_Client_Cb); - + /* attempts to make a connection on a socket */ ret = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); - + if (ret != 0) { printf("Connection Error\n"); return 1; } - + /* creat wolfssl object after each tcp connct */ if ( (ssl = wolfSSL_new(ctx)) == NULL) { fprintf(stderr, "wolfSSL_new error.\n"); return 1; } - + /* associate the file descriptor with the session */ ret = wolfSSL_set_fd(ssl, sockfd); - - if (ret != SSL_SUCCESS){ + + if (ret != SSL_SUCCESS) { return 1; } - + /* takes inputting string and outputs it to the server */ ret = SendReceive(ssl); - if(ret != 0){ + if (ret != 0) { return 1; } /* cleanup */ wolfSSL_free(ssl); - /* when completely done using SSL/TLS, free the + /* when completely done using SSL/TLS, free the * wolfssl_ctx object */ wolfSSL_CTX_free(ctx); wolfSSL_Cleanup(); diff --git a/psk/client-tcp.c b/psk/client-tcp.c index 3e4658ab..6ea36f23 100644 --- a/psk/client-tcp.c +++ b/psk/client-tcp.c @@ -34,9 +34,9 @@ #define SERV_PORT 11111 /* - * this function will send the inputted string to the server and then + * this function will send the inputted string to the server and then * recieve the string from the server outputing it to the termial - */ + */ int SendReceive(int sockfd) { char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ @@ -47,21 +47,21 @@ int SendReceive(int sockfd) printf("Write Error to Server\n"); return 1; } - - /* flags if the server stopped before the client could end */ + + /* flags if the server stopped before the client could end */ if (read(sockfd, recvline, MAXLINE) == 0) { printf("Client: Server Terminated Prematurely!\n"); return 1; } printf("Server Message: %s\n", recvline); - + return 0; } int main(int argc, char **argv) { - int sockfd, ret; + int sockfd, ret; struct sockaddr_in servaddr; /* must include an ip address or this will flag */ @@ -72,17 +72,17 @@ int main(int argc, char **argv) /* create a stream socket using tcp,internet protocal IPv4, * full-duplex stream */ - sockfd = socket(AF_INET, SOCK_STREAM, 0); + sockfd = socket(AF_INET, SOCK_STREAM, 0); /* places n zero-valued bytes in the address servaddr */ memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; - servaddr.sin_port = htons(SERV_PORT); + servaddr.sin_port = htons(SERV_PORT); /* converts IPv4 addresses from text to binary form */ ret = inet_pton(AF_INET, argv[1], &servaddr.sin_addr); - + if (ret != 1) { printf("Not a Valid network address"); return 1; @@ -90,11 +90,11 @@ int main(int argc, char **argv) /* attempts to make a connection on a socket */ ret = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); - + if (ret != 0) { return 1; } - + /* takes inputting string and outputs it to the server */ ret = SendReceive(sockfd); if (ret != 0){ @@ -103,6 +103,6 @@ int main(int argc, char **argv) } /* close socket and connection */ close(sockfd); - + return ret; } diff --git a/psk/server-psk-nonblocking.c b/psk/server-psk-nonblocking.c index 23cc6085..aa123e63 100644 --- a/psk/server-psk-nonblocking.c +++ b/psk/server-psk-nonblocking.c @@ -1,6 +1,6 @@ /* server-psk-nonblocking.c - * A server ecample using a TCP connection with PSK security and non blocking. - * + * A server ecample using a TCP connection with PSK security and non blocking. + * * Copyright (C) 2006-2015 wolfSSL Inc. * * This file is part of wolfSSL. (formerly known as CyaSSL) @@ -58,7 +58,7 @@ int tcp_select(int sockfd, int to_sec) int nfds = sockfd + 1; struct timeval timeout = {to_sec, 0}; int result; - + /* reset socket values */ FD_ZERO(&recvfds); FD_SET(sockfd, &recvfds); @@ -68,13 +68,16 @@ int tcp_select(int sockfd, int to_sec) result = select(nfds, &recvfds, NULL, &errfds, &timeout); /* logic for which enumerated value is returned */ - if (result == 0) + if (result == 0) { return TEST_TIMEOUT; + } else if (result > 0) { - if (FD_ISSET(sockfd, &recvfds)) + if (FD_ISSET(sockfd, &recvfds)) { return TEST_RECV_READY; - else if (FD_ISSET(sockfd, &errfds)) + } + else if (FD_ISSET(sockfd, &errfds)) { return TEST_ERROR_READY; + } } return TEST_SELECT_FAIL; @@ -84,7 +87,7 @@ int tcp_select(int sockfd, int to_sec) /* * Pulled in from examples/server/server.c * Function to handle nonblocking. Loops until tcp_select notifies that it's - * ready for action. + * ready for action. */ int NonBlockingSSL(WOLFSSL* ssl) { @@ -99,15 +102,17 @@ int NonBlockingSSL(WOLFSSL* ssl) int currTimeout = 1; /* print out for user notification */ - if (error == SSL_ERROR_WANT_READ) + if (error == SSL_ERROR_WANT_READ) { printf("... server would read block\n"); - else + } + else { printf("... server would write block\n"); + } select_ret = tcp_select(sockfd, currTimeout); - + /* if tcp_select signals ready try to accept otherwise continue loop*/ - if ((select_ret == TEST_RECV_READY) || + if ((select_ret == TEST_RECV_READY) || (select_ret == TEST_ERROR_READY)) { ret = wolfSSL_accept(ssl); error = wolfSSL_get_error(ssl, 0); @@ -129,10 +134,10 @@ int NonBlockingSSL(WOLFSSL* ssl) } -/* +/* * Handles response to client. */ -int respond(WOLFSSL* ssl) +int Respond(WOLFSSL* ssl) { int n; /* length of string read */ char buf[MAXLINE]; /* string read from client */ @@ -140,17 +145,21 @@ int respond(WOLFSSL* ssl) memset(buf, 0, MAXLINE); do { - if (NonBlockingSSL(ssl) != SSL_SUCCESS) + if (NonBlockingSSL(ssl) != SSL_SUCCESS) { return 1; + } + n = wolfSSL_read(ssl, buf, MAXLINE); if (n > 0) { printf("%s\n", buf); - } + } } while(n < 0); - - if (NonBlockingSSL(ssl) != SSL_SUCCESS) + + if (NonBlockingSSL(ssl) != SSL_SUCCESS) { return 1; + } + if (wolfSSL_write(ssl, response, strlen(response)) != strlen(response)) { printf("Fatal error : respond: write error\n"); return 1; @@ -168,8 +177,9 @@ static inline unsigned int my_psk_server_cb(WOLFSSL* ssl, const char* identity, (void)ssl; (void)key_max_len; - if (strncmp(identity, "Client_identity", 15) != 0) + if (strncmp(identity, "Client_identity", 15) != 0) { return 0; + } key[0] = 26; key[1] = 43; @@ -190,20 +200,21 @@ int main() WOLFSSL_CTX* ctx; wolfSSL_Init(); - + if ((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())) == NULL) { printf("Fatal error : wolfSSL_CTX_new error\n"); return 1; } - /* use psk suite for security */ + /* use psk suite for security */ wolfSSL_CTX_set_psk_server_callback(ctx, my_psk_server_cb); wolfSSL_CTX_use_psk_identity_hint(ctx, "wolfssl server"); if (wolfSSL_CTX_set_cipher_list(ctx, "PSK-AES128-CBC-SHA256") - != SSL_SUCCESS) + != SSL_SUCCESS) { printf("Fatal error : server can't set cipher list\n"); + } - /* find a socket */ + /* find a socket */ listenfd = socket(AF_INET, SOCK_STREAM, 0); if (listenfd < 0) { printf("Fatal error : socket error\n"); @@ -221,40 +232,40 @@ int main() if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void*)&opt, sizeof(int)) != 0) { printf("Fatal error : setsockopt errer"); - return 1; + return 1; } if (bind(listenfd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) { printf("Fatal error : bind error\n"); return 1; } - + /* main loop for accepting and responding to clients */ for ( ; ; ) { WOLFSSL* ssl; - - /* listen to the socket */ + + /* listen to the socket */ if (listen(listenfd, LISTENQ) < 0) { printf("Fatal error : listen error\n"); return 1; } - + cliLen = sizeof(cliAddr); connfd = accept(listenfd, (struct sockaddr *) &cliAddr, &cliLen); if (connfd < 0) { if (errno != EINTR) { printf("Fatal error : accept error\n"); - return 1; + return 1; } } else { printf("Connection from %s, port %d\n", inet_ntop(AF_INET, &cliAddr.sin_addr, buff, sizeof(buff)), ntohs(cliAddr.sin_port)); - + /* create WOLFSSL object */ if ((ssl = wolfSSL_new(ctx)) == NULL) { printf("Fatal error : wolfSSL_new error\n"); - return 1; + return 1; } wolfSSL_set_fd(ssl, connfd); @@ -264,9 +275,10 @@ int main() printf("Fatal error : fcntl set failed\n"); return 1; } - if (respond(ssl) != 0) + if (Respond(ssl) != 0) { printf("Fatal error : respond error\n"); return 1; + } /* closes the connections after responding */ wolfSSL_shutdown(ssl); @@ -280,7 +292,7 @@ int main() /* free up memory used by wolfssl */ wolfSSL_CTX_free(ctx); wolfSSL_Cleanup(); - + return 0; }