Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ESP32/DTLS13-wifi-station-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ idf.py save-defconfig

Press `Ctrl+]` to exit `idf.py monitor`. See [additional monitor keyboard commands](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/tools/idf-monitor.html).

## Credentials

The trusted CA comes from `wolfssl/certs_test.h` (see `CTX_CA_CERT` in
`user_settings.h`): its private key is public, so anyone can sign a server
certificate this client accepts. Replace it with your own CA before using this
code outside a demo, and set `TLS_SMP_SERVER_NAME` in `client-dtls13.h` to
your server's name.

## Other Examples:

For examples, see:
Expand Down
8 changes: 8 additions & 0 deletions ESP32/DTLS13-wifi-station-client/main/client-dtls13.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ WOLFSSL_ESP_TASK dtls13_smp_client_task(void *pvParameters)
goto cleanup;
}

/* Accept only a server certificate issued to TLS_SMP_SERVER_NAME,
* not any certificate that chains to the CA. */
if (wolfSSL_check_domain_name(ssl, TLS_SMP_SERVER_NAME)
!= WOLFSSL_SUCCESS) {
ESP_LOGE(TAG, "wolfSSL_check_domain_name failed\n");
goto cleanup;
}

/* Perform SSL connection */
ESP_LOGI(TAG, "Connecting to DTLS 1.3 server...");
if (wolfSSL_connect(ssl) != SSL_SUCCESS) {
Expand Down
10 changes: 10 additions & 0 deletions ESP32/DTLS13-wifi-station-client/main/include/client-dtls13.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
#include <wolfssl/wolfcrypt/settings.h>
#include "user_settings.h"

/* The name the server certificate must carry: the SM2 test cert has only
* CN www.wolfssl.com, the RSA one has SAN example.com. */
#ifndef TLS_SMP_SERVER_NAME
#if defined(WOLFSSL_SM2) || defined(WOLFSSL_SM3) || defined(WOLFSSL_SM4)
#define TLS_SMP_SERVER_NAME "www.wolfssl.com"
#else
#define TLS_SMP_SERVER_NAME "example.com"
#endif
#endif

#include <wolfssl/ssl.h>

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion ESP32/DTLS13-wifi-station-client/main/time_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ int set_time(void)
#else
/* otherwise set a fixed time that was hard coded */
set_fixed_default_time();
restrict = -3;
res = -3;
#endif

#ifndef NTP_SERVER_COUNT
Expand Down
7 changes: 7 additions & 0 deletions ESP32/DTLS13-wifi-station-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ idf.py save-defconfig

Press `Ctrl+]` to exit `idf.py monitor`. See [additional monitor keyboard commands](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/tools/idf-monitor.html).

## Credentials

The server certificate and key come from `wolfssl/certs_test.h` (see
`CTX_SERVER_CERT` in `user_settings.h`): they are the public wolfSSL test
credentials, so anyone can impersonate this server. Replace them with your
own before using this code outside a demo.

## Other Examples:

For examples, see:
Expand Down
33 changes: 25 additions & 8 deletions ESP32/DTLS13-wifi-station-server/main/server-dtls13.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ WOLFSSL_ESP_TASK dtls13_smp_server_task(void *pvParameters)
int recvLen = 0; /* length of message */
int ip_protocol = 0;
int ret;
int timedOut = 0; /* the last recvfrom() hit a timeout */

ESP_LOGI(TAG, "Init Stack: %d words", TLS_SMP_SERVER_TASK_WORDS);
ShowStackInfo("Begin Stack");
Expand Down Expand Up @@ -308,7 +309,11 @@ WOLFSSL_ESP_TASK dtls13_smp_server_task(void *pvParameters)
- (uxTaskGetStackHighWaterMark(NULL)));
ShowStackInfo("While Stack");
while (1) {
ESP_LOGI(TAG, "\n\nAwaiting client connection on port %d\n", SERV_PORT);
if (!timedOut) {
ESP_LOGI(TAG, "\n\nAwaiting client connection on port %d\n",
SERV_PORT);
}
timedOut = 0;

cliLen = sizeof(cliaddr);
ret = (int)recvfrom(listenfd,
Expand All @@ -318,12 +323,22 @@ WOLFSSL_ESP_TASK dtls13_smp_server_task(void *pvParameters)
);

if (ret < 0) {
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
/* A receive timeout, such as the one wolfSSL leaves on
* listenfd after a failed handshake: keep waiting. */
timedOut = 1;
continue;
}
ESP_LOGE(TAG, "ERROR during recvfrom()");
goto cleanup;
}
else if (ret == 0) {
ESP_LOGE(TAG, "recvfrom zero return\n");
goto cleanup;
/* An empty datagram: drop it, as MSG_PEEK left it queued. */
ESP_LOGW(TAG, "recvfrom zero return, dropping empty datagram\n");
cliLen = sizeof(cliaddr);
(void)recvfrom(listenfd, (char *)&buff, sizeof(buff), 0,
(struct sockaddr*)&cliaddr, &cliLen);
continue;
}

/* Create the WOLFSSL Object */
Expand Down Expand Up @@ -360,15 +375,14 @@ WOLFSSL_ESP_TASK dtls13_smp_server_task(void *pvParameters)
err = wolfSSL_get_error(ssl, 0);
ESP_LOGE(TAG, "error = %d, %s\n", err, wolfSSL_ERR_reason_error_string(err));
ESP_LOGE(TAG, "SSL_accept failed.\n");
goto cleanup;
goto next_peer;
}
showConnInfo(ssl);
while (1) {
if ((recvLen = wolfSSL_read(ssl, buff, sizeof(buff)-1)) > 0) {
buff[recvLen] = '\0';
ESP_LOGI(TAG, "wolfSSL_read heard %d bytes:\n\n"
"%s", recvLen, buff);

buff[recvLen] = '\0';
}
else if (recvLen <= 0) {
err = wolfSSL_get_error(ssl, 0);
Expand All @@ -378,7 +392,7 @@ WOLFSSL_ESP_TASK dtls13_smp_server_task(void *pvParameters)
ESP_LOGE(TAG, "error = %d, %s\n",
err, wolfSSL_ERR_reason_error_string(err));
ESP_LOGE(TAG, "SSL_read failed.\n");
goto cleanup;
goto next_peer;
}
ESP_LOGI(TAG, "Sending reply (check client for this text): %s",
ack);
Expand All @@ -387,7 +401,7 @@ WOLFSSL_ESP_TASK dtls13_smp_server_task(void *pvParameters)
ESP_LOGE(TAG, "error = %d, %s\n",
err, wolfSSL_ERR_reason_error_string(err));
ESP_LOGE(TAG, "wolfSSL_write failed.\n");
goto cleanup;
goto next_peer;
}
ESP_LOGI(TAG, "Sending complete. Waiting for next message...");
}
Expand All @@ -407,6 +421,9 @@ WOLFSSL_ESP_TASK dtls13_smp_server_task(void *pvParameters)
err, wolfSSL_ERR_reason_error_string(err));
ESP_LOGE(TAG, "wolfSSL_shutdown failed\n");
}

next_peer:
/* A failure with one peer must not stop the server for the others. */
wolfSSL_free(ssl);
ssl = NULL;

Expand Down
7 changes: 7 additions & 0 deletions ESP32/TLS13-ENC28J60-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ To open this solution in Visual Studio with the VisualGDB extension, see the [So

With wolfSSL debugging turned on, sample serial output of the ESP32 can been found in the [example output](./README-output.txt)

## Credentials

The certificates and keys in `main/embedded_*.h` are the public wolfSSL test
credentials from `certs/`: anyone can sign a server certificate this client
accepts, or use the client key. Replace them with your own before using this
code outside a demo, and set `TLS_SMP_SERVER_NAME` to your server's name.


# ENC28J60 Example
(See the Espressif [README.md](https://github.com/espressif/esp-idf/tree/master/examples#readme) file in the upper level '[examples](https://github.com/espressif/esp-idf/tree/master/examples)'
Expand Down
19 changes: 17 additions & 2 deletions ESP32/TLS13-ENC28J60-client/main/enc28j60_example_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
#define TLS_SMP_CLIENT_TASK_PRIORITY 8

#define TLS_SMP_TARGET_HOST "192.168.75.55"
/* The name the server certificate must carry (the test cert has example.com) */
#define TLS_SMP_SERVER_NAME "example.com"


/* include certificates. Note that there is an experiation date!
Expand Down Expand Up @@ -134,7 +136,7 @@ static void LogSocketError(const char* fmt, int err)

int tls_smp_client_task() {
int ret = WOLFSSL_SUCCESS; /* assume success until proven wrong */
int sockfd = 0; /* the socket that will carry our secure connection */
int sockfd = SOCKET_INVALID; /* the socket that will carry our secure connection */
struct sockaddr_in servAddr;
const int BUFF_SIZE = 256;
char buff[BUFF_SIZE];
Expand Down Expand Up @@ -205,6 +207,10 @@ int tls_smp_client_task() {
WOLFSSL_ERROR_MSG("ERROR: invalid address\n");
}
}
else if (ip4_addr == NULL) {
/* gethostbyname() failed above */
ret = WOLFSSL_FAILURE;
}
else {
servAddr.sin_addr.s_addr = ip4_addr->addr;
}
Expand Down Expand Up @@ -594,6 +600,15 @@ int tls_smp_client_task() {
WOLFSSL_ERROR_MSG("skipping wolfSSL_set_fd\n");
}

/* Accept only a server certificate issued to TLS_SMP_SERVER_NAME,
* not any certificate that chains to CA_FILE. */
if (ret == WOLFSSL_SUCCESS) {
ret = wolfSSL_check_domain_name(ssl, TLS_SMP_SERVER_NAME);
if (ret != WOLFSSL_SUCCESS) {
WOLFSSL_ERROR_MSG("ERROR: wolfSSL_check_domain_name failed\n");
}
}


/*
***************************************************************************
Expand Down Expand Up @@ -925,7 +940,7 @@ int set_time() {
int res = 0;

//*ideally, we'd like to set time from network, but let's set a default time, just in case */
struct tm timeinfo;
struct tm timeinfo = { 0 }; /* tm_isdst and the rest zeroed for mktime */
timeinfo.tm_year = 2022 - 1900;
timeinfo.tm_mon = 3;
timeinfo.tm_mday = 15;
Expand Down
7 changes: 7 additions & 0 deletions ESP32/TLS13-ENC28J60-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ To open this solution in Visual Studio with the VisualGDB extension, see the [So

With wolfSSL debugging turned on, sample serial output of the ESP32 can been found in the [example output](./README-output.txt)

## Credentials

The certificates and keys in `main/embedded_*.h` are the public wolfSSL test
credentials from `certs/`: anyone can use them to connect, or to impersonate
this server. Replace them with your own before using this code outside a demo.
A connected client can also stop the server by sending `shutdown`.

# ENC28J60 Example
(See the README.md file in the upper level 'examples' directory for more information about examples.)

Expand Down
16 changes: 12 additions & 4 deletions ESP32/TLS13-ENC28J60-server/main/enc28j60_example_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ TickType_t DelayTicks = 5000 / portTICK_PERIOD_MS;



/* Set by the "shutdown" command; app_main() then stops the server. */
static int mShutdown = 0;

#ifdef HAVE_SIGNAL
static void sig_handler(const int sig) {
// fprintf(stderr, "SIGINT handled = %d.\n", sig);
Expand Down Expand Up @@ -175,7 +178,6 @@ int tls_smp_server_task() {
int on;

static int mConnd = SOCKET_INVALID;
static int mShutdown = 0;

/* declare wolfSSL objects */
WOLFSSL_CTX *ctx = NULL; /* the wolfSSL context object*/
Expand Down Expand Up @@ -944,7 +946,7 @@ int set_time() {
int res = 0;

//*ideally, we'd like to set time from network, but let's set a default time, just in case */
struct tm timeinfo;
struct tm timeinfo = { 0 }; /* tm_isdst and the rest zeroed for mktime */
timeinfo.tm_year = 2022 - 1900;
timeinfo.tm_mon = 3;
timeinfo.tm_mday = 15;
Expand Down Expand Up @@ -1053,9 +1055,15 @@ void app_main(void) {
// one of the most important aspects of security is the time and date values
set_time();

for (;;) {
/* Restart the server after an error, but not after "shutdown". */
while (!mShutdown) {
ESP_LOGI(TAG, "main loop");
vTaskDelay(DelayTicks ? DelayTicks : 1); /* Minimum delay = 1 tick */
vTaskDelay(DelayTicks ? DelayTicks : 1); /* Minimum delay = 1 tick */
tls_smp_server_task();
}

/* done. wait forever */
for (;;) {
vTaskDelay(DelayTicks ? DelayTicks : 1); /* Minimum delay = 1 tick */
}
}
7 changes: 7 additions & 0 deletions ESP32/TLS13-wifi_station-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ To open this solution in Visual Studio with the VisualGDB extension, see the [So

With wolfSSL debugging turned on, sample serial output of the ESP32 can been found in the [example output]()

## Credentials

The certificates and keys in `main/embedded_*.h` are the public wolfSSL test
credentials from `certs/`: anyone can sign a server certificate this client
accepts, or use the client key. Replace them with your own before using this
code outside a demo, and set `TLS_SMP_SERVER_NAME` to your server's name.




Expand Down
19 changes: 17 additions & 2 deletions ESP32/TLS13-wifi_station-client/main/station_example_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
#define TLS_SMP_CLIENT_TASK_PRIORITY 8

#define TLS_SMP_TARGET_HOST "192.168.25.114"
/* The name the server certificate must carry (the test cert has example.com) */
#define TLS_SMP_SERVER_NAME "example.com"


/* include certificates. Note that there is an experiation date!
Expand Down Expand Up @@ -167,7 +169,7 @@ int set_time() {
/* ideally, we'd like to set time from network,
** but let's set a default time, just in case
*/
struct tm timeinfo;
struct tm timeinfo = { 0 }; /* tm_isdst and the rest zeroed for mktime */
timeinfo.tm_year = 2022 - 1900;
timeinfo.tm_mon = 3;
timeinfo.tm_mday = 15;
Expand Down Expand Up @@ -277,7 +279,7 @@ void wifi_init_sta(void)

int tls_smp_client_task() {
int ret = WOLFSSL_SUCCESS; /* assume success until proven wrong */
int sockfd = 0; /* the socket that will carry our secure connection */
int sockfd = SOCKET_INVALID; /* the socket that will carry our secure connection */
struct sockaddr_in servAddr;
const int BUFF_SIZE = 256;
char buff[BUFF_SIZE];
Expand Down Expand Up @@ -336,6 +338,10 @@ int tls_smp_client_task() {
ESP_LOGE(TAG, "ERROR: invalid address\n");
}
}
else if (ip4_addr == NULL) {
/* gethostbyname() failed above */
ret = WOLFSSL_FAILURE;
}
else {
servAddr.sin_addr.s_addr = ip4_addr->addr;
}
Expand Down Expand Up @@ -730,6 +736,15 @@ int tls_smp_client_task() {
ESP_LOGE(TAG, "skipping wolfSSL_set_fd\n");
}

/* Accept only a server certificate issued to TLS_SMP_SERVER_NAME,
* not any certificate that chains to CA_FILE. */
if (ret == WOLFSSL_SUCCESS) {
ret = wolfSSL_check_domain_name(ssl, TLS_SMP_SERVER_NAME);
if (ret != WOLFSSL_SUCCESS) {
ESP_LOGE(TAG, "ERROR: wolfSSL_check_domain_name failed\n");
}
}


/*
***************************************************************************
Expand Down
7 changes: 7 additions & 0 deletions ESP32/TLS13-wifi_station-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ To open this solution in Visual Studio with the VisualGDB extension, see the [So

With wolfSSL debugging turned on, sample serial output of the ESP32 can been found in the [example output]()

## Credentials

The certificates and keys in `main/embedded_*.h` are the public wolfSSL test
credentials from `certs/`: anyone can use them to connect, or to impersonate
this server. Replace them with your own before using this code outside a demo.
A connected client can also stop the server by sending `shutdown`.



# Wi-Fi Station Example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ int set_time() {

/*ideally, we'd like to set time from network,
but let's set a default time, just in case */
struct tm timeinfo;
struct tm timeinfo = { 0 }; /* tm_isdst and the rest zeroed for mktime */
timeinfo.tm_year = 2022 - 1900;
timeinfo.tm_mon = 3;
timeinfo.tm_mday = 15;
Expand Down
Loading