summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriabdalkader <i.abdalkader@gmail.com>2022-10-10 19:41:33 +0200
committerDamien George <damien@micropython.org>2022-11-08 23:41:03 +1100
commitefe7dac0aaf98018d8725df873024a0f9ab91c10 (patch)
treeb6b9321bbc5754d16df6ba820aa4488cb2b8e1fc
parent9d0aefe719b8a56650e840a87a5e659dd884a0db (diff)
stm32/mbedtls: Enable mbedtls certificate time validation.
This is a reimplementation of b560b9fe715e293caef80533d61d8c3b179b0339 for the stm32 port.
-rw-r--r--ports/stm32/mbedtls/mbedtls_config.h9
-rw-r--r--ports/stm32/mbedtls/mbedtls_port.c36
2 files changed, 45 insertions, 0 deletions
diff --git a/ports/stm32/mbedtls/mbedtls_config.h b/ports/stm32/mbedtls/mbedtls_config.h
index 4140bb514..bdac07339 100644
--- a/ports/stm32/mbedtls/mbedtls_config.h
+++ b/ports/stm32/mbedtls/mbedtls_config.h
@@ -26,6 +26,15 @@
#ifndef MICROPY_INCLUDED_MBEDTLS_CONFIG_H
#define MICROPY_INCLUDED_MBEDTLS_CONFIG_H
+// Enable mbedtls modules.
+#define MBEDTLS_HAVE_TIME
+#define MBEDTLS_HAVE_TIME_DATE
+
+// Time hook.
+#include <time.h>
+extern time_t stm32_rtctime_seconds(time_t *timer);
+#define MBEDTLS_PLATFORM_TIME_MACRO stm32_rtctime_seconds
+
// Set MicroPython-specific options.
#define MICROPY_MBEDTLS_CONFIG_BARE_METAL (1)
diff --git a/ports/stm32/mbedtls/mbedtls_port.c b/ports/stm32/mbedtls/mbedtls_port.c
index 5c4f8d0f9..cdfcd172a 100644
--- a/ports/stm32/mbedtls/mbedtls_port.c
+++ b/ports/stm32/mbedtls/mbedtls_port.c
@@ -27,6 +27,11 @@
#include "rng.h"
#include "mbedtls_config.h"
+#if defined(MBEDTLS_HAVE_TIME) || defined(MBEDTLS_HAVE_TIME_DATE)
+#include "rtc.h"
+#include "shared/timeutils/timeutils.h"
+#endif
+
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) {
uint32_t val = 0;
int n = 0;
@@ -42,3 +47,34 @@ int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t
}
return 0;
}
+
+#if defined(MBEDTLS_HAVE_TIME)
+time_t stm32_rtctime_seconds(time_t *timer) {
+ rtc_init_finalise();
+ RTC_DateTypeDef date;
+ RTC_TimeTypeDef time;
+ HAL_RTC_GetTime(&RTCHandle, &time, RTC_FORMAT_BIN);
+ HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN);
+ return timeutils_seconds_since_epoch(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds);
+}
+#endif
+
+#if defined(MBEDTLS_HAVE_TIME_DATE)
+struct tm *gmtime(const time_t *timep) {
+ static struct tm tm;
+ timeutils_struct_time_t tm_buf = {0};
+ timeutils_seconds_since_epoch_to_struct_time(*timep, &tm_buf);
+
+ tm.tm_sec = tm_buf.tm_sec;
+ tm.tm_min = tm_buf.tm_min;
+ tm.tm_hour = tm_buf.tm_hour;
+ tm.tm_mday = tm_buf.tm_mday;
+ tm.tm_mon = tm_buf.tm_mon - 1;
+ tm.tm_year = tm_buf.tm_year - 1900;
+ tm.tm_wday = tm_buf.tm_wday;
+ tm.tm_yday = tm_buf.tm_yday;
+ tm.tm_isdst = -1;
+
+ return &tm;
+}
+#endif