PR Review Changes

1. Add more documentation
2. Add cleanup functions for client and server before return
3. Add the ability for client to use a hostname for SERVER_IP
This commit is contained in:
Aaron Jense 2019-09-17 14:59:43 -06:00
parent 88036db223
commit 4ef8f53c9e
5 changed files with 70 additions and 55 deletions

View File

@ -9,27 +9,33 @@ Each project uses `user_settings.h` for to enable and disable features.
### Set Up Steps
0. Open the wolfssl Microsoft Visual Studio Solution
0. Setup your Azure Sphere device.
[Install Azure Sphere](https://docs.microsoft.com/en-us/azure-sphere/install/install)
[Set up an account](https://docs.microsoft.com/en-us/azure-sphere/install/azure-directory-account)
[Claim your device](https://docs.microsoft.com/en-us/azure-sphere/install/claim-device)
[Configure networking](https://docs.microsoft.com/en-us/azure-sphere/install/configure-wifi)
1. Build All the Projects
1. Open the wolfssl Microsoft Visual Studio Solution
2. Build All the Projects
+ Right Click: `Solution 'wolfssl' (4 of 4 projects)`
+ Click: `Build Solution`
2. Connect your Azure Sphere MT3620 Development Board using USB.
3. Connect your Azure Sphere MT3620 Development Board using USB.
3. Run the wolfCrypt Library Test
4. Run the wolfCrypt Library Test
+ Right Click: `wolfcrypt_test (Azure Sphere)`
+ Click: `Debug->'Start new instance'.`
4. Wait for the wolfCrypt Library Test to finish.
5. Wait for the wolfCrypt Library Test to finish.
5. Test the client.
6. Test the client.
+ Run client(Azure Sphere) using: `Debug->'Start new instance'`
It's OK if the HTTP GET request returns an error.
The TLS connection was successful.
6. Test the server.
7. Test the server.
+ Run server(Azure Sphere) using: `Debug->'Start new instance'`
+ Run the following wolfSSL example client command inside wolfssl directory.

View File

@ -6,7 +6,7 @@
"CmdArgs": [],
"Capabilities": {
"AllowedConnections": [
"151.101.26.217",
"www.wolfssl.com",
"192.168.1.128",
"192.168.1.150",
"192.168.1.200",

View File

@ -40,17 +40,25 @@
#include <applibs/log.h>
#include <applibs/networking.h>
static void client_Cleanup(int sockfd, WOLFSSL_CTX* ctx, WOLFSSL* ssl)
{
wolfSSL_free(ssl); /* Free the wolfSSL object */
wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
close(sockfd); /* Close the connection to the server */
}
int main(int argc, char** argv)
{
bool isNetworkingReady = false;
int sockfd;
struct sockaddr_in servAddr;
SOCKET_T sockfd = 0;
char buff[256];
size_t len;
int ret;
/* declare wolfSSL objects */
WOLFSSL_CTX* ctx;
WOLFSSL* ssl;
WOLFSSL_CTX* ctx = NULL;
WOLFSSL* ssl = NULL;
/* Check if the Azure Sphere Dev Board has network connectivity. */
if ((Networking_IsNetworkingReady(&isNetworkingReady) < 0) || !isNetworkingReady) {
@ -58,53 +66,35 @@ int main(int argc, char** argv)
return -1;
}
/* Initialize wolfSSL */
wolfSSL_Init();
/* Create a socket that uses an internet IPv4 address,
* Sets the socket to be stream based (TCP),
* 0 means choose the default protocol. */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr, "ERROR: failed to create the socket\n");
ret = wolfIO_TcpConnect(&sockfd, SERVER_IP, DEFAULT_PORT, 0);
if ((ret != 0) || ((int)sockfd < 0)) {
fprintf(stderr, "ERROR: failed to create socket.");
return -1;
}
/* Initialize wolfSSL */
wolfSSL_Init();
/* Create and initialize WOLFSSL_CTX */
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
if (ctx == NULL) {
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
client_Cleanup(sockfd,ctx,ssl);
return -1;
}
/* Load client certificates into WOLFSSL_CTX */
if (wolfSSL_CTX_load_verify_buffer(ctx, CERT, SIZEOF_CERT, WOLFSSL_FILETYPE_ASN1)
!= SSL_SUCCESS) {
ret = wolfSSL_CTX_load_verify_buffer(ctx, CERT, SIZEOF_CERT, WOLFSSL_FILETYPE_ASN1);
if (ret != SSL_SUCCESS) {
fprintf(stderr, "ERROR: failed to load %s, please check the buffer.\n");
return -1;
}
/* Initialize the server address struct with zeros */
memset(&servAddr, 0, sizeof(servAddr));
/* Fill in the server address */
servAddr.sin_family = AF_INET; /* using IPv4 */
servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */
/* Get the server IPv4 address from SERVER_IP in user_settings.h */
if (inet_pton(AF_INET, SERVER_IP, &servAddr.sin_addr) != 1) {
fprintf(stderr, "ERROR: invalid address\n");
return -1;
}
/* Connect to the server */
if (connect(sockfd, (struct sockaddr*) & servAddr, sizeof(servAddr))
== -1) {
fprintf(stderr, "ERROR: failed to connect\n");
client_Cleanup(sockfd,ctx,ssl);
return -1;
}
/* Create a WOLFSSL object */
if ((ssl = wolfSSL_new(ctx)) == NULL) {
fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
client_Cleanup(sockfd,ctx,ssl);
return -1;
}
@ -114,6 +104,7 @@ int main(int argc, char** argv)
/* Connect to wolfSSL on the server side */
if (wolfSSL_connect(ssl) != SSL_SUCCESS) {
fprintf(stderr, "ERROR: failed to connect to wolfSSL\n");
client_Cleanup(sockfd,ctx,ssl);
return -1;
}
@ -124,6 +115,7 @@ int main(int argc, char** argv)
/* Send the message to the server */
if (wolfSSL_write(ssl, msg, (int)len) != len) {
fprintf(stderr, "ERROR: failed to write\n");
client_Cleanup(sockfd,ctx,ssl);
return -1;
}
@ -131,6 +123,7 @@ int main(int argc, char** argv)
memset(buff, 0, sizeof(buff));
if (wolfSSL_read(ssl, buff, sizeof(buff) - 1) == -1) {
fprintf(stderr, "ERROR: failed to read\n");
client_Cleanup(sockfd,ctx,ssl);
return -1;
}
@ -138,9 +131,6 @@ int main(int argc, char** argv)
printf("Server Reply: %s\n", buff);
/* Cleanup and return */
wolfSSL_free(ssl); /* Free the wolfSSL object */
wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
close(sockfd); /* Close the connection to the server */
return 0; /* Return reporting a success */
client_Cleanup(sockfd,ctx,ssl);
return 0; /* Return reporting a success */
}

View File

@ -45,6 +45,14 @@
#define KEY_BUF server_key_der_2048
#define SIZEOF_KEY_BUF sizeof_server_key_der_2048
static void server_Cleanup(int sockfd, WOLFSSL_CTX* ctx, WOLFSSL* ssl)
{
wolfSSL_free(ssl); /* Free the wolfSSL object */
wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
close(sockfd); /* Close the socket listening for clients */
}
int main(void)
{
bool isNetworkingReady = false;
@ -60,8 +68,8 @@ int main(void)
const char* reply = "I hear ya fa shizzle!\n";
/* declare wolfSSL objects */
WOLFSSL_CTX* ctx;
WOLFSSL* ssl;
WOLFSSL_CTX* ctx = NULL;
WOLFSSL* ssl = NULL;
/* Check if the Azure Sphere Dev Board has network connectivity. */
if ((Networking_IsNetworkingReady(&isNetworkingReady) < 0) || !isNetworkingReady) {
@ -77,12 +85,14 @@ int main(void)
* 0 means choose the default protocol. */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr, "ERROR: failed to create the socket\n");
server_Cleanup(sockfd, ctx, ssl);
return -1;
}
/* Create and initialize WOLFSSL_CTX */
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())) == NULL) {
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
server_Cleanup(sockfd, ctx, ssl);
return -1;
}
@ -91,6 +101,7 @@ int main(void)
!= SSL_SUCCESS) {
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
CERT_BUF);
server_Cleanup(sockfd, ctx, ssl);
return -1;
}
@ -99,6 +110,7 @@ int main(void)
!= SSL_SUCCESS) {
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
KEY_BUF);
server_Cleanup(sockfd, ctx, ssl);
return -1;
}
@ -113,12 +125,14 @@ int main(void)
/* Bind the server socket to our port */
if (bind(sockfd, (struct sockaddr*)&servAddr, sizeof(servAddr)) == -1) {
fprintf(stderr, "ERROR: failed to bind\n");
server_Cleanup(sockfd, ctx, ssl);
return -1;
}
/* Listen for a new connection, allow 5 pending connections */
if (listen(sockfd, 5) == -1) {
fprintf(stderr, "ERROR: failed to listen\n");
server_Cleanup(sockfd, ctx, ssl);
return -1;
}
@ -130,12 +144,14 @@ int main(void)
if ((connd = accept(sockfd, (struct sockaddr*)&clientAddr, &size))
== -1) {
fprintf(stderr, "ERROR: failed to accept the connection\n\n");
server_Cleanup(sockfd, ctx, ssl);
return -1;
}
/* Create a WOLFSSL object */
if ((ssl = wolfSSL_new(ctx)) == NULL) {
fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
server_Cleanup(sockfd, ctx, ssl);
return -1;
}
@ -147,6 +163,7 @@ int main(void)
if (ret != SSL_SUCCESS) {
fprintf(stderr, "wolfSSL_accept error = %d\n",
wolfSSL_get_error(ssl, ret));
server_Cleanup(sockfd, ctx, ssl);
return -1;
}
@ -156,6 +173,7 @@ int main(void)
memset(buff, 0, sizeof(buff));
if (wolfSSL_read(ssl, buff, sizeof(buff)-1) == -1) {
fprintf(stderr, "ERROR: failed to read\n");
server_Cleanup(sockfd, ctx, ssl);
return -1;
}
@ -176,6 +194,7 @@ int main(void)
/* Reply back to the client */
if (wolfSSL_write(ssl, buff, (int)len) != len) {
fprintf(stderr, "ERROR: failed to write\n");
server_Cleanup(sockfd, ctx, ssl);
return -1;
}
@ -187,8 +206,6 @@ int main(void)
printf("Shutdown complete\n");
/* Cleanup and return */
wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
close(sockfd); /* Close the socket listening for clients */
server_Cleanup(sockfd, ctx, ssl);
return 0; /* Return reporting a success */
}

View File

@ -2,7 +2,6 @@
#define _USER_SETTINGS_H_
#define WOLFSSL_AZSPHERE
/* Client connects to the server with these details. */
#ifdef CUSTOM_SERVER_CONNECTION
#ifndef SERVER_IP
@ -14,7 +13,7 @@
static const char msg[] = "Are you listening wolfSSL Server?";
#else
#ifndef SERVER_IP
#define SERVER_IP "151.101.26.217" /* www.wolfssl.com */
#define SERVER_IP "www.wolfssl.com"
#endif
#define CERT wolfssl_website_root_ca
#define SIZEOF_CERT sizeof_wolfssl_website_root_ca
@ -63,4 +62,7 @@
/* Filesystem */
#define NO_FILESYSTEM
/* Debug */
#define WOLFIO_DEBUG
#endif /* _USER_SETTINGS_H_ */