Finished changing the files in /psk to more closely follow the coding standard.

pull/50/head
Conner 2017-05-16 16:29:22 -06:00
parent 8bfdd52d09
commit 200708b5f5
5 changed files with 133 additions and 115 deletions

View File

@ -36,7 +36,7 @@
#define SERV_PORT 11111 /* default port*/ #define SERV_PORT 11111 /* default port*/
/* /*
* enum used for tcp_select function * enum used for tcp_select function
*/ */
enum { enum {
TEST_SELECT_FAIL, TEST_SELECT_FAIL,
@ -60,25 +60,28 @@ static inline int tcp_select(int socketfd, int to_sec)
result = select(nfds, &recvfds, NULL, &errfds, &timeout); result = select(nfds, &recvfds, NULL, &errfds, &timeout);
if (result == 0) if (result == 0) {
return TEST_TIMEOUT; return TEST_TIMEOUT;
}
else if (result > 0) { else if (result > 0) {
if (FD_ISSET(socketfd, &recvfds)) if (FD_ISSET(socketfd, &recvfds)) {
return TEST_RECV_READY; return TEST_RECV_READY;
else if(FD_ISSET(socketfd, &errfds)) }
else if(FD_ISSET(socketfd, &errfds)) {
return TEST_ERROR_READY; return TEST_ERROR_READY;
}
} }
return TEST_SELECT_FAIL; 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) static int NonBlockingSSL_Connect(WOLFSSL* ssl)
{ {
int ret, error, sockfd, select_ret, currTimeout; int ret, error, sockfd, select_ret, currTimeout;
ret = wolfSSL_connect(ssl); ret = wolfSSL_connect(ssl);
error = wolfSSL_get_error(ssl, 0); error = wolfSSL_get_error(ssl, 0);
sockfd = (int)wolfSSL_get_fd(ssl); sockfd = (int)wolfSSL_get_fd(ssl);
@ -87,10 +90,12 @@ static int NonBlockingSSL_Connect(WOLFSSL* ssl)
error == SSL_ERROR_WANT_WRITE)) { error == SSL_ERROR_WANT_WRITE)) {
currTimeout = 1; currTimeout = 1;
if (error == SSL_ERROR_WANT_READ) if (error == SSL_ERROR_WANT_READ) {
printf("... client would read block\n"); printf("... client would read block\n");
else }
else {
printf("... client would write block\n"); printf("... client would write block\n");
}
select_ret = tcp_select(sockfd, currTimeout); select_ret = tcp_select(sockfd, currTimeout);
@ -118,7 +123,7 @@ static int NonBlockingSSL_Connect(WOLFSSL* ssl)
*psk client set up. *psk client set up.
*/ */
static inline unsigned int My_Psk_Client_Cb(WOLFSSL* ssl, const char* hint, 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) unsigned int key_max_len)
{ {
(void)ssl; (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 * recieve the string from the server outputing it to the termial
*/ */
int SendReceive(WOLFSSL* ssl) int SendReceive(WOLFSSL* ssl)
{ {
char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ char sendline[MAXLINE]="Hello Server"; /* string to send to the server */
char recvline[MAXLINE]; /* string received from the server */ char recvline[MAXLINE]; /* string received from the server */
/* write string to the server */ /* write string to the server */
if (wolfSSL_write(ssl, sendline, MAXLINE) != sizeof(sendline)) { if (wolfSSL_write(ssl, sendline, MAXLINE) != sizeof(sendline)) {
printf("Write Error to Server\n"); printf("Write Error to Server\n");
return 1; return 1;
} }
/* flags if the Server stopped before the client could end */ /* flags if the Server stopped before the client could end */
if (wolfSSL_read(ssl, recvline, MAXLINE) < 0 ) { if (wolfSSL_read(ssl, recvline, MAXLINE) < 0 ) {
printf("Client: Server Terminated Prematurely!\n"); printf("Client: Server Terminated Prematurely!\n");
@ -161,7 +166,7 @@ int SendReceive(WOLFSSL* ssl)
/* show message from the server */ /* show message from the server */
printf("Server Message: %s\n", recvline); printf("Server Message: %s\n", recvline);
return 0; return 0;
} }
@ -177,20 +182,20 @@ int main(int argc, char **argv)
printf("Usage: tcpClient <IPaddress>\n"); printf("Usage: tcpClient <IPaddress>\n");
return 1; return 1;
} }
wolfSSL_Init(); /* initialize wolfSSL */ wolfSSL_Init(); /* initialize wolfSSL */
/* create and initialize WOLFSSL_CTX structure */ /* create and initialize WOLFSSL_CTX structure */
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) { if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
fprintf(stderr, "SSL_CTX_new error.\n"); fprintf(stderr, "SSL_CTX_new error.\n");
return 1; return 1;
} }
/* create a stream socket using tcp,internet protocal IPv4, /* create a stream socket using tcp,internet protocal IPv4,
* full-duplex stream */ * 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 */ /* places n zero-valued bytes in the address servaddr */
memset(&servaddr, 0, sizeof(servaddr)); memset(&servaddr, 0, sizeof(servaddr));
@ -199,23 +204,23 @@ int main(int argc, char **argv)
/* converts IPv4 addresses from text to binary form */ /* converts IPv4 addresses from text to binary form */
ret = inet_pton(AF_INET, argv[1], &servaddr.sin_addr); ret = inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
if (ret != 1) { if (ret != 1) {
printf("inet_pton error\n"); printf("inet_pton error\n");
return 1; return 1;
} }
/* set up pre shared keys */ /* set up pre shared keys */
wolfSSL_CTX_set_psk_client_callback(ctx,My_Psk_Client_Cb); wolfSSL_CTX_set_psk_client_callback(ctx,My_Psk_Client_Cb);
/* attempts to make a connection on a socket */ /* attempts to make a connection on a socket */
ret = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); ret = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (ret != 0) { if (ret != 0) {
printf("Connection Error\n"); printf("Connection Error\n");
return 1; return 1;
} }
/* create wolfSSL object after each tcp connect */ /* create wolfSSL object after each tcp connect */
if ((ssl = wolfSSL_new(ctx)) == NULL) { if ((ssl = wolfSSL_new(ctx)) == NULL) {
fprintf(stderr, "wolfSSL_new error.\n"); 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 */ /* tell wolfSSL that nonblocking is going to be used */
wolfSSL_set_using_nonblock(ssl, 1); 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 * flags for a file. checks if it returns an error, if it does
* stop program */ * stop program */
int flags = fcntl(sockfd, F_GETFL, 0); 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. /* 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 block an open, a read, or a write on the file
* (do not wait for terminal input. If an error occurs, * (do not wait for terminal input. If an error occurs,
* stop program*/ * stop program*/
flags = fcntl(sockfd, F_SETFL, flags | O_NONBLOCK); flags = fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
if (flags < 0) { if (flags < 0) {
@ -262,11 +267,11 @@ int main(int argc, char **argv)
/* cleanup */ /* cleanup */
wolfSSL_free(ssl); wolfSSL_free(ssl);
/* when completely done using SSL/TLS, free the /* when completely done using SSL/TLS, free the
* wolfssl_ctx object */ * wolfssl_ctx object */
wolfSSL_CTX_free(ctx); wolfSSL_CTX_free(ctx);
wolfSSL_Cleanup(); wolfSSL_Cleanup();
return ret; return ret;
} }

View File

@ -40,7 +40,7 @@
*psk client set up. *psk client set up.
*/ */
static inline unsigned int My_Psk_Client_Cb(WOLFSSL* ssl, const char* hint, 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) unsigned int key_max_len)
{ {
(void)ssl; (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 * recieve the string from the server outputing it to the termial
*/ */
int SendReceive(WOLFSSL* ssl) int SendReceive(WOLFSSL* ssl)
{ {
char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ char sendline[MAXLINE]="Hello Server"; /* string to send to the server */
char recvline[MAXLINE]; /* string received from the server */ char recvline[MAXLINE]; /* string received from the server */
/* write string to the server */ /* write string to the server */
if (wolfSSL_write(ssl, sendline, MAXLINE) != sizeof(sendline)) { if (wolfSSL_write(ssl, sendline, MAXLINE) != sizeof(sendline)) {
printf("Write Error to Server\n"); printf("Write Error to Server\n");
return 1; return 1;
} }
/* flags if the Server stopped before the client could end */ /* flags if the Server stopped before the client could end */
if (wolfSSL_read(ssl, recvline, MAXLINE) < 0 ) { if (wolfSSL_read(ssl, recvline, MAXLINE) < 0 ) {
printf("Client: Server Terminated Prematurely!\n"); printf("Client: Server Terminated Prematurely!\n");
@ -83,12 +83,12 @@ int SendReceive(WOLFSSL* ssl)
/* show message from the server */ /* show message from the server */
printf("Server Message: %s\n", recvline); printf("Server Message: %s\n", recvline);
return 0; return 0;
} }
int main(int argc, char **argv){ int main(int argc, char **argv){
int sockfd, sock, ret; int sockfd, sock, ret;
WOLFSSL* ssl; WOLFSSL* ssl;
WOLFSSL* sslResume = 0; WOLFSSL* sslResume = 0;
@ -101,19 +101,19 @@ int main(int argc, char **argv){
printf("Usage: tcpClient <IPaddress>\n"); printf("Usage: tcpClient <IPaddress>\n");
return 1; return 1;
} }
wolfSSL_Init(); /* initialize wolfSSL */ wolfSSL_Init(); /* initialize wolfSSL */
/* create and initialize WOLFSSL_CTX structure */ /* create and initialize WOLFSSL_CTX structure */
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) { if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
fprintf(stderr, "SSL_CTX_new error.\n"); fprintf(stderr, "SSL_CTX_new error.\n");
return 1; return 1;
} }
/* create a stream socket using tcp,internet protocal IPv4, /* create a stream socket using tcp,internet protocal IPv4,
* full-duplex stream */ * 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 */ /* places n zero-valued bytes in the address servaddr */
memset(&servaddr, 0, sizeof(servaddr)); memset(&servaddr, 0, sizeof(servaddr));
@ -122,11 +122,11 @@ int main(int argc, char **argv){
/* converts IPv4 addresses from text to binary form */ /* converts IPv4 addresses from text to binary form */
ret = inet_pton(AF_INET, argv[1], &servaddr.sin_addr); ret = inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
if (ret != 1){ if (ret != 1){
return 1; return 1;
} }
/* set up pre shared keys */ /* set up pre shared keys */
wolfSSL_CTX_set_psk_client_callback(ctx, My_Psk_Client_Cb); wolfSSL_CTX_set_psk_client_callback(ctx, My_Psk_Client_Cb);
@ -135,7 +135,7 @@ int main(int argc, char **argv){
if (ret != 0 ){ if (ret != 0 ){
return 1; return 1;
} }
/* create wolfSSL object after each tcp connect */ /* create wolfSSL object after each tcp connect */
if ( (ssl = wolfSSL_new(ctx)) == NULL) { if ( (ssl = wolfSSL_new(ctx)) == NULL) {
fprintf(stderr, "wolfSSL_new error.\n"); fprintf(stderr, "wolfSSL_new error.\n");
@ -164,15 +164,15 @@ int main(int argc, char **argv){
wolfSSL_Cleanup(); wolfSSL_Cleanup();
/* /*
* resume session, start new connection and socket * resume session, start new connection and socket
*/ */
/* start a new socket connection */ /* start a new socket connection */
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
/* connect to the socket */ /* connect to the socket */
ret = connect(sock, (struct sockaddr *) &servaddr, sizeof(servaddr)); ret = connect(sock, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (ret != 0){ if (ret != 0){
return 1; return 1;
} }
@ -194,8 +194,9 @@ int main(int argc, char **argv){
} }
/* check to see if the session id is being reused */ /* 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"); printf("reused session id\n");
}
else else
printf("didn't reuse session id!!!\n"); printf("didn't reuse session id!!!\n");
@ -206,9 +207,9 @@ int main(int argc, char **argv){
close(sock); close(sock);
/* clean up */ /* clean up */
wolfSSL_free(sslResume); wolfSSL_free(sslResume);
wolfSSL_CTX_free(ctx); wolfSSL_CTX_free(ctx);
wolfSSL_Cleanup(); wolfSSL_Cleanup();
return ret; return ret;
} }

View File

@ -38,7 +38,7 @@
*psk client set up. *psk client set up.
*/ */
static inline unsigned int My_Psk_Client_Cb(WOLFSSL* ssl, const char* hint, 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) unsigned int key_max_len)
{ {
(void)ssl; (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 * recieve the string from the server outputing it to the termial
*/ */
int SendReceive(WOLFSSL* ssl) int SendReceive(WOLFSSL* ssl)
{ {
char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ char sendline[MAXLINE]="Hello Server"; /* string to send to the server */
char recvline[MAXLINE]; /* string received from the server */ char recvline[MAXLINE]; /* string received from the server */
/* write string to the server */ /* write string to the server */
if (wolfSSL_write(ssl, sendline, MAXLINE) != sizeof(sendline)) { if (wolfSSL_write(ssl, sendline, MAXLINE) != sizeof(sendline)) {
printf("Write Error to Server\n"); printf("Write Error to Server\n");
return 1; return 1;
} }
/* flags if the Server stopped before the client could end */ /* flags if the Server stopped before the client could end */
if (wolfSSL_read(ssl, recvline, MAXLINE) < 0 ) { if (wolfSSL_read(ssl, recvline, MAXLINE) < 0 ) {
printf("Client: Server Terminated Prematurely!\n"); printf("Client: Server Terminated Prematurely!\n");
@ -81,7 +81,7 @@ int SendReceive(WOLFSSL* ssl)
/* show message from the server */ /* show message from the server */
printf("Server Message: %s\n", recvline); printf("Server Message: %s\n", recvline);
return 0; return 0;
} }
@ -97,19 +97,19 @@ int main(int argc, char **argv)
printf("Usage: tcpClient <IPaddress>\n"); printf("Usage: tcpClient <IPaddress>\n");
return 1; return 1;
} }
wolfSSL_Init(); /* initialize wolfSSL */ wolfSSL_Init(); /* initialize wolfSSL */
/* create and initialize WOLFSSL_CTX structure */ /* create and initialize WOLFSSL_CTX structure */
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) { if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
fprintf(stderr, "SSL_CTX_new error.\n"); fprintf(stderr, "SSL_CTX_new error.\n");
return 1; return 1;
} }
/* create a stream socket using tcp,internet protocal IPv4, /* create a stream socket using tcp,internet protocal IPv4,
* full-duplex stream */ * 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 */ /* places n zero-valued bytes in the address servaddr */
memset(&servaddr, 0, sizeof(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); ret = inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
if (ret != 1) { if (ret != 1) {
printf("inet_pton error\n"); printf("inet_pton error\n");
return 1; return 1;
} }
/* set up pre shared keys */ /* set up pre shared keys */
wolfSSL_CTX_set_psk_client_callback(ctx, My_Psk_Client_Cb); wolfSSL_CTX_set_psk_client_callback(ctx, My_Psk_Client_Cb);
/* attempts to make a connection on a socket */ /* attempts to make a connection on a socket */
ret = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); ret = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (ret != 0) { if (ret != 0) {
printf("Connection Error\n"); printf("Connection Error\n");
return 1; return 1;
} }
/* creat wolfssl object after each tcp connct */ /* creat wolfssl object after each tcp connct */
if ( (ssl = wolfSSL_new(ctx)) == NULL) { if ( (ssl = wolfSSL_new(ctx)) == NULL) {
fprintf(stderr, "wolfSSL_new error.\n"); fprintf(stderr, "wolfSSL_new error.\n");
return 1; return 1;
} }
/* associate the file descriptor with the session */ /* associate the file descriptor with the session */
ret = wolfSSL_set_fd(ssl, sockfd); ret = wolfSSL_set_fd(ssl, sockfd);
if (ret != SSL_SUCCESS){ if (ret != SSL_SUCCESS) {
return 1; return 1;
} }
/* takes inputting string and outputs it to the server */ /* takes inputting string and outputs it to the server */
ret = SendReceive(ssl); ret = SendReceive(ssl);
if(ret != 0){ if (ret != 0) {
return 1; return 1;
} }
/* cleanup */ /* cleanup */
wolfSSL_free(ssl); wolfSSL_free(ssl);
/* when completely done using SSL/TLS, free the /* when completely done using SSL/TLS, free the
* wolfssl_ctx object */ * wolfssl_ctx object */
wolfSSL_CTX_free(ctx); wolfSSL_CTX_free(ctx);
wolfSSL_Cleanup(); wolfSSL_Cleanup();

View File

@ -34,9 +34,9 @@
#define SERV_PORT 11111 #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 * recieve the string from the server outputing it to the termial
*/ */
int SendReceive(int sockfd) int SendReceive(int sockfd)
{ {
char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ 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"); printf("Write Error to Server\n");
return 1; 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) { if (read(sockfd, recvline, MAXLINE) == 0) {
printf("Client: Server Terminated Prematurely!\n"); printf("Client: Server Terminated Prematurely!\n");
return 1; return 1;
} }
printf("Server Message: %s\n", recvline); printf("Server Message: %s\n", recvline);
return 0; return 0;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int sockfd, ret; int sockfd, ret;
struct sockaddr_in servaddr; struct sockaddr_in servaddr;
/* must include an ip address or this will flag */ /* 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, /* create a stream socket using tcp,internet protocal IPv4,
* full-duplex stream */ * 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 */ /* places n zero-valued bytes in the address servaddr */
memset(&servaddr, 0, sizeof(servaddr)); memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET; 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 */ /* converts IPv4 addresses from text to binary form */
ret = inet_pton(AF_INET, argv[1], &servaddr.sin_addr); ret = inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
if (ret != 1) { if (ret != 1) {
printf("Not a Valid network address"); printf("Not a Valid network address");
return 1; return 1;
@ -90,11 +90,11 @@ int main(int argc, char **argv)
/* attempts to make a connection on a socket */ /* attempts to make a connection on a socket */
ret = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); ret = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (ret != 0) { if (ret != 0) {
return 1; return 1;
} }
/* takes inputting string and outputs it to the server */ /* takes inputting string and outputs it to the server */
ret = SendReceive(sockfd); ret = SendReceive(sockfd);
if (ret != 0){ if (ret != 0){
@ -103,6 +103,6 @@ int main(int argc, char **argv)
} }
/* close socket and connection */ /* close socket and connection */
close(sockfd); close(sockfd);
return ret; return ret;
} }

View File

@ -1,6 +1,6 @@
/* server-psk-nonblocking.c /* 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. * Copyright (C) 2006-2015 wolfSSL Inc.
* *
* This file is part of wolfSSL. (formerly known as CyaSSL) * 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; int nfds = sockfd + 1;
struct timeval timeout = {to_sec, 0}; struct timeval timeout = {to_sec, 0};
int result; int result;
/* reset socket values */ /* reset socket values */
FD_ZERO(&recvfds); FD_ZERO(&recvfds);
FD_SET(sockfd, &recvfds); FD_SET(sockfd, &recvfds);
@ -68,13 +68,16 @@ int tcp_select(int sockfd, int to_sec)
result = select(nfds, &recvfds, NULL, &errfds, &timeout); result = select(nfds, &recvfds, NULL, &errfds, &timeout);
/* logic for which enumerated value is returned */ /* logic for which enumerated value is returned */
if (result == 0) if (result == 0) {
return TEST_TIMEOUT; return TEST_TIMEOUT;
}
else if (result > 0) { else if (result > 0) {
if (FD_ISSET(sockfd, &recvfds)) if (FD_ISSET(sockfd, &recvfds)) {
return TEST_RECV_READY; return TEST_RECV_READY;
else if (FD_ISSET(sockfd, &errfds)) }
else if (FD_ISSET(sockfd, &errfds)) {
return TEST_ERROR_READY; return TEST_ERROR_READY;
}
} }
return TEST_SELECT_FAIL; return TEST_SELECT_FAIL;
@ -84,7 +87,7 @@ int tcp_select(int sockfd, int to_sec)
/* /*
* Pulled in from examples/server/server.c * Pulled in from examples/server/server.c
* Function to handle nonblocking. Loops until tcp_select notifies that it's * Function to handle nonblocking. Loops until tcp_select notifies that it's
* ready for action. * ready for action.
*/ */
int NonBlockingSSL(WOLFSSL* ssl) int NonBlockingSSL(WOLFSSL* ssl)
{ {
@ -99,15 +102,17 @@ int NonBlockingSSL(WOLFSSL* ssl)
int currTimeout = 1; int currTimeout = 1;
/* print out for user notification */ /* print out for user notification */
if (error == SSL_ERROR_WANT_READ) if (error == SSL_ERROR_WANT_READ) {
printf("... server would read block\n"); printf("... server would read block\n");
else }
else {
printf("... server would write block\n"); printf("... server would write block\n");
}
select_ret = tcp_select(sockfd, currTimeout); select_ret = tcp_select(sockfd, currTimeout);
/* if tcp_select signals ready try to accept otherwise continue loop*/ /* 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)) { (select_ret == TEST_ERROR_READY)) {
ret = wolfSSL_accept(ssl); ret = wolfSSL_accept(ssl);
error = wolfSSL_get_error(ssl, 0); error = wolfSSL_get_error(ssl, 0);
@ -129,10 +134,10 @@ int NonBlockingSSL(WOLFSSL* ssl)
} }
/* /*
* Handles response to client. * Handles response to client.
*/ */
int respond(WOLFSSL* ssl) int Respond(WOLFSSL* ssl)
{ {
int n; /* length of string read */ int n; /* length of string read */
char buf[MAXLINE]; /* string read from client */ char buf[MAXLINE]; /* string read from client */
@ -140,17 +145,21 @@ int respond(WOLFSSL* ssl)
memset(buf, 0, MAXLINE); memset(buf, 0, MAXLINE);
do { do {
if (NonBlockingSSL(ssl) != SSL_SUCCESS) if (NonBlockingSSL(ssl) != SSL_SUCCESS) {
return 1; return 1;
}
n = wolfSSL_read(ssl, buf, MAXLINE); n = wolfSSL_read(ssl, buf, MAXLINE);
if (n > 0) { if (n > 0) {
printf("%s\n", buf); printf("%s\n", buf);
} }
} }
while(n < 0); while(n < 0);
if (NonBlockingSSL(ssl) != SSL_SUCCESS) if (NonBlockingSSL(ssl) != SSL_SUCCESS) {
return 1; return 1;
}
if (wolfSSL_write(ssl, response, strlen(response)) != strlen(response)) { if (wolfSSL_write(ssl, response, strlen(response)) != strlen(response)) {
printf("Fatal error : respond: write error\n"); printf("Fatal error : respond: write error\n");
return 1; return 1;
@ -168,8 +177,9 @@ static inline unsigned int my_psk_server_cb(WOLFSSL* ssl, const char* identity,
(void)ssl; (void)ssl;
(void)key_max_len; (void)key_max_len;
if (strncmp(identity, "Client_identity", 15) != 0) if (strncmp(identity, "Client_identity", 15) != 0) {
return 0; return 0;
}
key[0] = 26; key[0] = 26;
key[1] = 43; key[1] = 43;
@ -190,20 +200,21 @@ int main()
WOLFSSL_CTX* ctx; WOLFSSL_CTX* ctx;
wolfSSL_Init(); wolfSSL_Init();
if ((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())) == NULL) { if ((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())) == NULL) {
printf("Fatal error : wolfSSL_CTX_new error\n"); printf("Fatal error : wolfSSL_CTX_new error\n");
return 1; 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_set_psk_server_callback(ctx, my_psk_server_cb);
wolfSSL_CTX_use_psk_identity_hint(ctx, "wolfssl server"); wolfSSL_CTX_use_psk_identity_hint(ctx, "wolfssl server");
if (wolfSSL_CTX_set_cipher_list(ctx, "PSK-AES128-CBC-SHA256") 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"); printf("Fatal error : server can't set cipher list\n");
}
/* find a socket */ /* find a socket */
listenfd = socket(AF_INET, SOCK_STREAM, 0); listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0) { if (listenfd < 0) {
printf("Fatal error : socket error\n"); printf("Fatal error : socket error\n");
@ -221,40 +232,40 @@ int main()
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void*)&opt, if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void*)&opt,
sizeof(int)) != 0) { sizeof(int)) != 0) {
printf("Fatal error : setsockopt errer"); printf("Fatal error : setsockopt errer");
return 1; return 1;
} }
if (bind(listenfd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) { if (bind(listenfd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) {
printf("Fatal error : bind error\n"); printf("Fatal error : bind error\n");
return 1; return 1;
} }
/* main loop for accepting and responding to clients */ /* main loop for accepting and responding to clients */
for ( ; ; ) { for ( ; ; ) {
WOLFSSL* ssl; WOLFSSL* ssl;
/* listen to the socket */ /* listen to the socket */
if (listen(listenfd, LISTENQ) < 0) { if (listen(listenfd, LISTENQ) < 0) {
printf("Fatal error : listen error\n"); printf("Fatal error : listen error\n");
return 1; return 1;
} }
cliLen = sizeof(cliAddr); cliLen = sizeof(cliAddr);
connfd = accept(listenfd, (struct sockaddr *) &cliAddr, &cliLen); connfd = accept(listenfd, (struct sockaddr *) &cliAddr, &cliLen);
if (connfd < 0) { if (connfd < 0) {
if (errno != EINTR) { if (errno != EINTR) {
printf("Fatal error : accept error\n"); printf("Fatal error : accept error\n");
return 1; return 1;
} }
} }
else { else {
printf("Connection from %s, port %d\n", printf("Connection from %s, port %d\n",
inet_ntop(AF_INET, &cliAddr.sin_addr, buff, sizeof(buff)), inet_ntop(AF_INET, &cliAddr.sin_addr, buff, sizeof(buff)),
ntohs(cliAddr.sin_port)); ntohs(cliAddr.sin_port));
/* create WOLFSSL object */ /* create WOLFSSL object */
if ((ssl = wolfSSL_new(ctx)) == NULL) { if ((ssl = wolfSSL_new(ctx)) == NULL) {
printf("Fatal error : wolfSSL_new error\n"); printf("Fatal error : wolfSSL_new error\n");
return 1; return 1;
} }
wolfSSL_set_fd(ssl, connfd); wolfSSL_set_fd(ssl, connfd);
@ -264,9 +275,10 @@ int main()
printf("Fatal error : fcntl set failed\n"); printf("Fatal error : fcntl set failed\n");
return 1; return 1;
} }
if (respond(ssl) != 0) if (Respond(ssl) != 0) {
printf("Fatal error : respond error\n"); printf("Fatal error : respond error\n");
return 1; return 1;
}
/* closes the connections after responding */ /* closes the connections after responding */
wolfSSL_shutdown(ssl); wolfSSL_shutdown(ssl);
@ -280,7 +292,7 @@ int main()
/* free up memory used by wolfssl */ /* free up memory used by wolfssl */
wolfSSL_CTX_free(ctx); wolfSSL_CTX_free(ctx);
wolfSSL_Cleanup(); wolfSSL_Cleanup();
return 0; return 0;
} }