Skip to content

Commit

Permalink
polish wolfssl_client
Browse files Browse the repository at this point in the history
  • Loading branch information
gojimmypi committed Feb 9, 2024
1 parent 9b98879 commit 91f24f5
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 63 deletions.
41 changes: 23 additions & 18 deletions IDE/Espressif/ESP-IDF/examples/wolfssl_client/Makefile
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
# This is a project Makefile.
# It is assumed the directory this Makefile resides in is a
# project subdirectory containing an entire project.
#

PROJECT_NAME := wolfssl_client

#OS := $(shell uname -s)
#ifeq ($(OS),Linux)
#CFLAGS += -DOS_LINUX
#endif
#ifeq ($(OS),Darwin)
#CFLAGS += -DOS_MACOS
#endif
#ifneq (,$(findstring MINGW,$(OS)))
#CFLAGS += -DOS_WINDOWS_MINGW
#endif
#ifneq (,$(findstring CYGWIN,$(OS)))
#CFLAGS += -DOS_WINDOWS_CYGWIN
#endif
#CFLAGS += -DOS_WINDOWS
# Calling shell causes unintuiive error in Windows:
# OS := $(shell uname -s)
# But OS should already be defined:
ifeq ($(OS),Linux)
CFLAGS += -DOS_LINUX
endif
ifeq ($(OS),Windows_NT)
CFLAGS += -DWOLFSSL_MAKE_SYSTEM_NAME_WINDOWS
endif
ifeq ($(OS),Darwin)
CFLAGS += -DWOLFSSL_MAKE_SYSTEM_NAME_APPLE
endif
ifneq (,$(findstring MINGW,$(OS)))
CFLAGS += -DWOLFSSL_MAKE_SYSTEM_NAME_MINGW
endif
ifneq (,$(findstring CYGWIN,$(OS)))
CFLAGS += -DWOLFSSL_MAKE_SYSTEM_NAME_CYGWIN
endif

# It is essential that the build process sees the WOLFSSL_USER_SETTINGS
CFLAGS += -DWOLFSSL_USER_SETTINGS

# if directory not available, please disable the line below.
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common

# LDFLAGS += -T ./my.esp8266.project.ld

include $(IDF_PATH)/make/project.mk
62 changes: 33 additions & 29 deletions IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/client-tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/

#include "client-tls.h"

/* Espressif FreeRTOS */
Expand All @@ -28,6 +27,9 @@
#include <freertos/event_groups.h>
#endif

/* Espressif */
#include <esp_log.h>

/* socket includes */
#include <lwip/netdb.h>
#include <lwip/sockets.h>
Expand Down Expand Up @@ -62,7 +64,7 @@
* -h 192.168.1.128 -v 4 -l TLS13-SM4-CCM-SM3 -c ./certs/sm2/client-sm2.pem -k ./certs/sm2/client-sm2-priv.pem -A ./certs/sm2/root-sm2.pem -C
*
**/
static const char* const TAG = "client-tls";
#define TAG "client-tls"

#if defined(DEBUG_WOLFSSL)
int stack_start = -1;
Expand Down Expand Up @@ -239,46 +241,46 @@ WOLFSSL_ESP_TASK tls_smp_client_task(void* args)
*
* reference code for SM Ciphers:
*
#if defined(HAVE_AESGCM) && !defined(NO_DH)
#ifdef WOLFSSL_TLS13
defaultCipherList = "TLS13-AES128-GCM-SHA256"
#ifndef WOLFSSL_NO_TLS12
":DHE-PSK-AES128-GCM-SHA256"
#endif
;
#else
defaultCipherList = "DHE-PSK-AES128-GCM-SHA256";
#if defined(HAVE_AESGCM) && !defined(NO_DH)
#ifdef WOLFSSL_TLS13
defaultCipherList = "TLS13-AES128-GCM-SHA256"
#ifndef WOLFSSL_NO_TLS12
":DHE-PSK-AES128-GCM-SHA256"
#endif
#elif defined(HAVE_AESGCM) && defined(WOLFSSL_TLS13)
defaultCipherList = "TLS13-AES128-GCM-SHA256:PSK-AES128-GCM-SHA256"
#ifndef WOLFSSL_NO_TLS12
":PSK-AES128-GCM-SHA256"
#endif
;
#elif defined(HAVE_NULL_CIPHER)
defaultCipherList = "PSK-NULL-SHA256";
#elif !defined(NO_AES_CBC)
defaultCipherList = "PSK-AES128-CBC-SHA256";
;
#else
defaultCipherList = "PSK-AES128-GCM-SHA256";
defaultCipherList = "DHE-PSK-AES128-GCM-SHA256";
#endif
#elif defined(HAVE_AESGCM) && defined(WOLFSSL_TLS13)
defaultCipherList = "TLS13-AES128-GCM-SHA256:PSK-AES128-GCM-SHA256"
#ifndef WOLFSSL_NO_TLS12
":PSK-AES128-GCM-SHA256"
#endif
;
#elif defined(HAVE_NULL_CIPHER)
defaultCipherList = "PSK-NULL-SHA256";
#elif !defined(NO_AES_CBC)
defaultCipherList = "PSK-AES128-CBC-SHA256";
#else
defaultCipherList = "PSK-AES128-GCM-SHA256";
#endif
*/

ret = wolfSSL_CTX_set_cipher_list(ctx, WOLFSSL_ESP32_CIPHER_SUITE);
if (ret == WOLFSSL_SUCCESS) {
ESP_LOGI(TAG, "Set cipher list: %s\n", WOLFSSL_ESP32_CIPHER_SUITE);
}
else {
ESP_LOGE(TAG, "ERROR: failed to set cipher list: %s\n", WOLFSSL_ESP32_CIPHER_SUITE);
ESP_LOGE(TAG, "ERROR: failed to set cipher list: %s\n",
WOLFSSL_ESP32_CIPHER_SUITE);
}
#endif

#ifdef DEBUG_WOLFSSL
ShowCiphers(NULL);
ESP_LOGI(TAG,
"Stack used: %d\n",
CONFIG_ESP_MAIN_TASK_STACK_SIZE
- uxTaskGetStackHighWaterMark(NULL));
ESP_LOGI(TAG, "Stack used: %d\n",
CONFIG_ESP_MAIN_TASK_STACK_SIZE
- uxTaskGetStackHighWaterMark(NULL));
#endif

/* see user_settings PROJECT_DH for HAVE_DH and HAVE_FFDHE_2048 */
Expand All @@ -303,7 +305,8 @@ WOLFSSL_ESP_TASK tls_smp_client_task(void* args)
CTX_CLIENT_CERT_SIZE,
CTX_CLIENT_CERT_TYPE);
if (ret_i != SSL_SUCCESS) {
ESP_LOGE(TAG, "ERROR: failed to load chain %d, please check the file.\n", ret_i);
ESP_LOGE(TAG, "ERROR: failed to load chain %d, "
"please check the file.", ret_i);
}

/* Load client certificates into WOLFSSL_CTX */
Expand Down Expand Up @@ -440,7 +443,8 @@ WOLFSSL_ESP_TASK tls_smp_client_task(void* args)
printf("%s\n", buff);
}
else {
ESP_LOGE(TAG, "ERROR: failed to connect to wolfSSL. Error: %d\n", ret_i);
ESP_LOGE(TAG, "ERROR: failed to connect to wolfSSL. "
"Error: %d\n", ret_i);
}
#ifdef DEBUG_WOLFSSL
ShowCiphers(ssl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@
# in the build directory. This behaviour is entirely configurable,
# please read the ESP-IDF documents if you need to do this.
#

COMPONENT_SRCDIRS +=
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@
#endif

#define TLS_SMP_CLIENT_TASK_NAME "tls_client_example"
//#define TLS_SMP_CLIENT_TASK_BYTES (26 * 1024)
// #define TLS_SMP_CLIENT_TASK_BYTES (20 * 1024) /* leave handshake fail -125*/

/* Reminder: Vanilla FreeRTOS is words, Espressif is bytes. */
#if defined(WOLFSSL_ESP8266)
#define TLS_SMP_CLIENT_TASK_BYTES (6 * 1024)
#define TLS_SMP_CLIENT_TASK_BYTES (6 * 1024)
#else
#define TLS_SMP_CLIENT_TASK_BYTES (8 * 1024)
#endif

#define TLS_SMP_CLIENT_TASK_PRIORITY 8

#if defined(SINGLE_THREADED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
#ifndef _WIFI_CONNECT_H_
#define _WIFI_CONNECT_H_

#include <esp_idf_version.h>
#include <esp_log.h>

/* ESP lwip */
#define EXAMPLE_ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY

Expand All @@ -36,6 +33,10 @@
#include "protocol_examples_common.h" /* see project CMakeLists.txt */
#endif

#ifdef OS_WINDOWS
// #error "OS_WINDOWS"
#endif

/**
******************************************************************************
******************************************************************************
Expand All @@ -48,15 +49,35 @@
* file my_private_config.h should be excluded from git updates */
#define USE_MY_PRIVATE_CONFIG

/* Note that IntelliSense may not work properly in the next section for the
* Espressif SDK 3.4 on the ESP8266. Macros should still be defined.
* See the project-level Makefile. Example found in:
* https://github.com/wolfSSL/wolfssl/tree/master/IDE/Espressif/ESP-IDF/examples/template
*/
#ifdef USE_MY_PRIVATE_CONFIG
#if defined(WOLFSSL_CMAKE_SYSTEM_NAME_WINDOWS)
#define WOLFSSL_CMAKE
#include "/workspace/my_private_config.h"
#elif defined(WOLFSSL_MAKE_SYSTEM_NAME_WINDOWS)
#define WOLFSSL_MAKE
#include "/workspace/my_private_config.h"
#elif defined(WOLFSSL_CMAKE_SYSTEM_NAME_WSL)
#define WOLFSSL_CMAKE
#include "/mnt/c/workspace/my_private_config.h"
#elif defined(WOLFSSL_MAKE_SYSTEM_NAME_WSL)
#define WOLFSSL_MAKE
#include "/mnt/c/workspace/my_private_config.h"
#elif defined(WOLFSSL_CMAKE_SYSTEM_NAME_LINUX)
#define WOLFSSL_CMAKE
#include "~/workspace/my_private_config.h"
#elif defined(WOLFSSL_MAKE_SYSTEM_NAME_LINUX)
#define WOLFSSL_MAKE
#include "~/workspace/my_private_config.h"
#elif defined(WOLFSSL_CMAKE_SYSTEM_NAME_APPLE)
#include "~/Documents/my_private_config.h"
#elif defined(WOLFSSL_MAKE_SYSTEM_NAME_APPLE)
#define WOLFSSL_MAKE
#include "~/Documents/my_private_config.h"
#elif defined(OS_WINDOWS)
#include "/workspace/my_private_config.h"
#else
Expand Down
3 changes: 2 additions & 1 deletion IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ void app_main(void)
args[0].port = 11111;

int this_heap = esp_get_free_heap_size();
ESP_LOGI(TAG, "main tls_smp_client_init heap @ %p = %d", &this_heap, this_heap);
ESP_LOGI(TAG, "main tls_smp_client_init heap @ %p = %d",
&this_heap, this_heap);
tls_smp_client_init(args);
/* optional additional client threads
tls_smp_client_init(args);
Expand Down
11 changes: 6 additions & 5 deletions IDE/Espressif/ESP-IDF/examples/wolfssl_client/main/time_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ int probably_valid_time_string(const char* str)
int set_time_from_string(const char* time_buffer)
{
/* expecting github default formatting: 'Thu Aug 31 12:41:45 2023 -0700' */
char offset[28]; /* large arrays, just in case there's still bad data */
char day_str[28];
char month_str[28];
const char *format = "%3s %3s %d %d:%d:%d %d %s";
struct tm this_timeinfo;
struct timeval now;
time_t interim_time;
char offset[28]; /* large arrays, just in case there's still bad data */
char day_str[28];
char month_str[28];
int day, year, hour, minute, second;
int quote_offset = 0;
int ret = 0;
Expand Down Expand Up @@ -224,8 +224,9 @@ int set_time_from_string(const char* time_buffer)
ESP_LOGI(TAG, "Time updated to %s", time_buffer);
}
else {
ESP_LOGE(TAG, "Failed to convert \"%s\" to a tm date.", time_buffer);
ESP_LOGI(TAG, "Trying fixed date that was hard-coded.");
ESP_LOGE(TAG, "Failed to convert \"%s\" to a tm date.",
time_buffer);
ESP_LOGI(TAG, "Trying fixed date that was hard-coded....");
set_fixed_default_time();
ret = ESP_FAIL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include "wifi_connect.h"
#include "wifi_connect.h"

/* FreeRTOS */
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/event_groups.h>
#include <esp_wifi.h>

/* Espressif */
#include <esp_log.h>
#include <esp_idf_version.h>
#include <esp_wifi.h>

/* wolfSSL */
#include <wolfssl/wolfcrypt/settings.h>
Expand Down

0 comments on commit 91f24f5

Please sign in to comment.