From 61d99304daf501fb1aed23960905447190b8bcd4 Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Thu, 30 Jun 2022 05:25:56 -0400 Subject: [PATCH] mbedtls_x509_time_gmtime() to fill struct w/ time Signed-off-by: Glenn Strauss --- include/mbedtls/x509.h | 13 +++++++++++++ library/x509.c | 13 ++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/include/mbedtls/x509.h b/include/mbedtls/x509.h index ac8ff9ad3..ef6b098f2 100644 --- a/include/mbedtls/x509.h +++ b/include/mbedtls/x509.h @@ -378,6 +378,19 @@ int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *ser */ int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1, const mbedtls_x509_time *t2); +#if defined(MBEDTLS_HAVE_TIME_DATE) +/** + * \brief Fill mbedtls_x509_time with provided mbedtls_time_t. + * + * \param tt mbedtls_time_t to convert + * \param now mbedtls_x509_time to fill with converted mbedtls_time_t + * + * \return \c 0 on success + * \return A non-zero return value on failure. + */ +int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now); +#endif /* MBEDTLS_HAVE_TIME_DATE */ + /** * \brief Check a given mbedtls_x509_time against the system time * and tell if it's in the past. diff --git a/library/x509.c b/library/x509.c index ba800377c..2e58462b8 100644 --- a/library/x509.c +++ b/library/x509.c @@ -1011,17 +1011,11 @@ int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1, } #if defined(MBEDTLS_HAVE_TIME_DATE) -/* - * Set the time structure to the current time. - * Return 0 on success, non-zero on failure. - */ -static int x509_get_current_time(mbedtls_x509_time *now) +int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now) { struct tm *lt, tm_buf; - mbedtls_time_t tt; int ret = 0; - tt = mbedtls_time(NULL); lt = mbedtls_platform_gmtime_r(&tt, &tm_buf); if (lt == NULL) { @@ -1038,6 +1032,11 @@ static int x509_get_current_time(mbedtls_x509_time *now) return ret; } +static int x509_get_current_time(mbedtls_x509_time *now) +{ + return mbedtls_x509_time_gmtime(mbedtls_time(NULL), now); +} + int mbedtls_x509_time_is_past(const mbedtls_x509_time *to) { mbedtls_x509_time now;