8000 ports/stm32: Enable mbedtls certificate time validation. · iabdalkader/micropython@00007ff · GitHub
[go: up one dir, main page]

Skip to content

Commit 00007ff

Browse files
committed
ports/stm32: Enable mbedtls certificate time validation.
* This is a reimplementation of micropython#8854 for the stm32 port.
1 parent 89b3207 commit 00007ff

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

ports/stm32/mbedtls/mbedtls_config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
#define MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE
8686
#define MBEDTLS_X509_CRT_PARSE_C
8787
#define MBEDTLS_X509_USE_C
88+
#define MBEDTLS_HAVE_TIME
89+
#define MBEDTLS_HAVE_TIME_DATE
8890

8991
// Memory allocation hooks
9092
#include <stdlib.h>
@@ -95,6 +97,11 @@ void m_tracked_free(void *ptr);
9597
#define MBEDTLS_PLATFORM_STD_FREE m_tracked_free
9698
#define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf
9799

100+
// Time hook
101+
#include <time.h>
102+
extern time_t stm32_rtctime_seconds(time_t *timer);
103+
#define MBEDTLS_PLATFORM_TIME_MACRO stm32_rtctime_seconds
104+
98105
#include "mbedtls/check_config.h"
99106

100107
#endif /* MICROPY_INCLUDED_MBEDTLS_CONFIG_H */

ports/stm32/mbedtls/mbedtls_port.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727
#include "rng.h"
2828
#include "mbedtls_config.h"
29+
#if defined(MBEDTLS_HAVE_TIME) || defined(MBEDTLS_HAVE_TIME_DATE)
30+
#include "rtc.h"
31+
#include "shared/timeutils/timeutils.h"
32+
#endif
2933

3034
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) {
3135
uint32_t val = 0;
@@ -42,3 +46,34 @@ int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t
4246
}
4347
return 0;
4448
}
49+
50+
#if defined(MBEDTLS_HAVE_TIME)
51+
time_t stm32_rtctime_seconds(time_t *timer) {
52+
rtc_init_finalise();
53+
RTC_DateTypeDef date;
54+
RTC_TimeTypeDef time;
55+
HAL_RTC_GetTime(&RTCHandle, &time, RTC_FORMAT_BIN);
56+
HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN);
57+
return timeutils_seconds_since_epoch(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds);
58+
}
59+
#endif
60+
61+
#if defined(MBEDTLS_HAVE_TIME_DATE)
62+
struct tm *gmtime(const time_t *timep) {
63+
static struct tm tm;
64+
timeutils_struct_time_t tm_buf = {0};
65+
timeutils_seconds_since_epoch_to_struct_time(*timep, &tm_buf);
66+
67+
tm.tm_sec = tm_buf.tm_sec;
68+
tm.tm_min = tm_buf.tm_min;
69+
tm.tm_hour = tm_buf.tm_hour;
70+
tm.tm_mday = tm_buf.tm_mday;
71+
tm.tm_mon = tm_buf.tm_mon - 1;
72+
tm.tm_year = tm_buf.tm_year - 1900;
73+
tm.tm_wday = tm_buf.tm_wday;
74+
tm.tm_yday = tm_buf.tm_yday;
75+
tm.tm_isdst = -1;
76+
77+
return &tm;
78+
}
79+
#endif

0 commit comments

Comments
 (0)
0