Skip to content

ports/esp32: Enable mbedtls cert time validation. #13100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2023
Merged
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
3 changes: 3 additions & 0 deletions ports/esp32/boards/sdkconfig.base
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ CONFIG_LWIP_PPP_CHAP_SUPPORT=y
# SSL
# Use 4kiB output buffer instead of default 16kiB
CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y
CONFIG_MBEDTLS_HAVE_TIME_DATE=y
CONFIG_MBEDTLS_PLATFORM_TIME_ALT=y
CONFIG_MBEDTLS_HAVE_TIME=y

# Disable ALPN support as it's not implemented in MicroPython
CONFIG_MBEDTLS_SSL_ALPN=n
Expand Down
17 changes: 17 additions & 0 deletions ports/esp32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <sys/time.h>
#include <time.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
Expand All @@ -49,6 +51,9 @@
#include "py/mphal.h"
#include "shared/readline/readline.h"
#include "shared/runtime/pyexec.h"
#include "shared/timeutils/timeutils.h"
#include "mbedtls/platform_time.h"

#include "uart.h"
#include "usb.h"
#include "usb_serial_jtag.h"
Expand Down Expand Up @@ -83,6 +88,15 @@ int vprintf_null(const char *format, va_list ap) {
return 0;
}

time_t platform_mbedtls_time(time_t *timer) {
// mbedtls_time requires time in seconds from EPOCH 1970

struct timeval tv;
gettimeofday(&tv, NULL);

return tv.tv_sec + TIMEUTILS_SECONDS_1970_TO_2000;
}

void mp_task(void *pvParameter) {
volatile uint32_t sp = (uint32_t)esp_cpu_get_sp();
#if MICROPY_PY_THREAD
Expand All @@ -98,6 +112,9 @@ void mp_task(void *pvParameter) {
#endif
machine_init();

// Configure time function, for mbedtls certificate time validation.
mbedtls_platform_set_time(platform_mbedtls_time);

esp_err_t err = esp_event_loop_create_default();
if (err != ESP_OK) {
ESP_LOGE("esp_init", "can't create event loop: 0x%x\n", err);
Expand Down