Skip to content

Commit 8a43879

Browse files
authored
Merge pull request #8022 from sbutcher-arm/update-mbedtls-2.13.1
Update Mbed TLS version to 2.13.1
2 parents 48c149b + 1578519 commit 8a43879

File tree

9 files changed

+204
-16
lines changed

9 files changed

+204
-16
lines changed

features/mbedtls/VERSION.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mbedtls-2.13.0
1+
mbedtls-2.13.1

features/mbedtls/importer/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#
2828

2929
# Set the mbed TLS release to import (this can/should be edited before import)
30-
MBED_TLS_RELEASE ?= mbedtls-2.13.0
30+
MBED_TLS_RELEASE ?= mbedtls-2.13.1
3131

3232
# Translate between mbed TLS namespace and mbed namespace
3333
TARGET_PREFIX:=../

features/mbedtls/inc/mbedtls/config.h

+29-1
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,21 @@
152152
/**
153153
* \def MBEDTLS_HAVE_TIME_DATE
154154
*
155-
* System has time.h and time(), gmtime() and the clock is correct.
155+
* System has time.h, time(), and an implementation for
156+
* mbedtls_platform_gmtime_r() (see below).
156157
* The time needs to be correct (not necesarily very accurate, but at least
157158
* the date should be correct). This is used to verify the validity period of
158159
* X.509 certificates.
159160
*
160161
* Comment if your system does not have a correct clock.
162+
*
163+
* \note mbedtls_platform_gmtime_r() is an abstraction in platform_util.h that
164+
* behaves similarly to the gmtime_r() function from the C standard. Refer to
165+
* the documentation for mbedtls_platform_gmtime_r() for more information.
166+
*
167+
* \note It is possible to configure an implementation for
168+
* mbedtls_platform_gmtime_r() at compile-time by using the macro
169+
* MBEDTLS_PLATFORM_GMTIME_R_ALT.
161170
*/
162171
//#define MBEDTLS_HAVE_TIME_DATE
163172

@@ -3115,6 +3124,25 @@
31153124
*/
31163125
//#define MBEDTLS_PLATFORM_ZEROIZE_ALT
31173126

3127+
/**
3128+
* Uncomment the macro to let Mbed TLS use your alternate implementation of
3129+
* mbedtls_platform_gmtime_r(). This replaces the default implementation in
3130+
* platform_util.c.
3131+
*
3132+
* gmtime() is not a thread-safe function as defined in the C standard. The
3133+
* library will try to use safer implementations of this function, such as
3134+
* gmtime_r() when available. However, if Mbed TLS cannot identify the target
3135+
* system, the implementation of mbedtls_platform_gmtime_r() will default to
3136+
* using the standard gmtime(). In this case, calls from the library to
3137+
* gmtime() will be guarded by the global mutex mbedtls_threading_gmtime_mutex
3138+
* if MBEDTLS_THREADING_C is enabled. We recommend that calls from outside the
3139+
* library are also guarded with this mutex to avoid race conditions. However,
3140+
* if the macro MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, Mbed TLS will
3141+
* unconditionally use the implementation for mbedtls_platform_gmtime_r()
3142+
* supplied at compile time.
3143+
*/
3144+
//#define MBEDTLS_PLATFORM_GMTIME_R_ALT
3145+
31183146
/* \} name SECTION: Customisation configuration options */
31193147

31203148
/* Target and application specific configurations */

features/mbedtls/inc/mbedtls/platform_util.h

+41
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,17 @@
2525
#ifndef MBEDTLS_PLATFORM_UTIL_H
2626
#define MBEDTLS_PLATFORM_UTIL_H
2727

28+
#if !defined(MBEDTLS_CONFIG_FILE)
29+
#include "mbedtls/config.h"
30+
#else
31+
#include MBEDTLS_CONFIG_FILE
32+
#endif
33+
2834
#include <stddef.h>
35+
#if defined(MBEDTLS_HAVE_TIME_DATE)
36+
#include "mbedtls/platform_time.h"
37+
#include <time.h>
38+
#endif /* MBEDTLS_HAVE_TIME_DATE */
2939

3040
#ifdef __cplusplus
3141
extern "C" {
@@ -55,6 +65,37 @@ extern "C" {
5565
*/
5666
void mbedtls_platform_zeroize( void *buf, size_t len );
5767

68+
#if defined(MBEDTLS_HAVE_TIME_DATE)
69+
/**
70+
* \brief Platform-specific implementation of gmtime_r()
71+
*
72+
* The function is a thread-safe abstraction that behaves
73+
* similarly to the gmtime_r() function from Unix/POSIX.
74+
*
75+
* Mbed TLS will try to identify the underlying platform and
76+
* make use of an appropriate underlying implementation (e.g.
77+
* gmtime_r() for POSIX and gmtime_s() for Windows). If this is
78+
* not possible, then gmtime() will be used. In this case, calls
79+
* from the library to gmtime() will be guarded by the mutex
80+
* mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is
81+
* enabled. It is recommended that calls from outside the library
82+
* are also guarded by this mutex.
83+
*
84+
* If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will
85+
* unconditionally use the alternative implementation for
86+
* mbedtls_platform_gmtime_r() supplied by the user at compile time.
87+
*
88+
* \param tt Pointer to an object containing time (in seconds) since the
89+
* epoch to be converted
90+
* \param tm_buf Pointer to an object where the results will be stored
91+
*
92+
* \return Pointer to an object of type struct tm on success, otherwise
93+
* NULL
94+
*/
95+
struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
96+
struct tm *tm_buf );
97+
#endif /* MBEDTLS_HAVE_TIME_DATE */
98+
5899
#ifdef __cplusplus
59100
}
60101
#endif

features/mbedtls/inc/mbedtls/threading.h

+11
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ extern int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t *mutex );
9999
#if defined(MBEDTLS_FS_IO)
100100
extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex;
101101
#endif
102+
103+
#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
104+
/* This mutex may or may not be used in the default definition of
105+
* mbedtls_platform_gmtime_r(), but in order to determine that,
106+
* we need to check POSIX features, hence modify _POSIX_C_SOURCE.
107+
* With the current approach, this declaration is orphaned, lacking
108+
* an accompanying definition, in case mbedtls_platform_gmtime_r()
109+
* doesn't need it, but that's not a problem. */
110+
extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex;
111+
#endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
112+
102113
#endif /* MBEDTLS_THREADING_C */
103114

104115
#ifdef __cplusplus

features/mbedtls/inc/mbedtls/version.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@
4040
*/
4141
#define MBEDTLS_VERSION_MAJOR 2
4242
#define MBEDTLS_VERSION_MINOR 13
43-
#define MBEDTLS_VERSION_PATCH 0
43+
#define MBEDTLS_VERSION_PATCH 1
4444

4545
/**
4646
* The single version number has the following structure:
4747
* MMNNPP00
4848
* Major version | Minor version | Patch version
4949
*/
50-
#define MBEDTLS_VERSION_NUMBER 0x020D0000
51-
#define MBEDTLS_VERSION_STRING "2.13.0"
52-
#define MBEDTLS_VERSION_STRING_FULL "mbed TLS 2.13.0"
50+
#define MBEDTLS_VERSION_NUMBER 0x020D0100
51+
#define MBEDTLS_VERSION_STRING "2.13.1"
52+
#define MBEDTLS_VERSION_STRING_FULL "mbed TLS 2.13.1"
5353

5454
#if defined(MBEDTLS_VERSION_C)
5555

features/mbedtls/src/platform_util.c

+68
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@
2020
* This file is part of Mbed TLS (https://tls.mbed.org)
2121
*/
2222

23+
/*
24+
* Ensure gmtime_r is available even with -std=c99; must be defined before
25+
* config.h, which pulls in glibc's features.h. Harmless on other platforms.
26+
*/
27+
#if !defined(_POSIX_C_SOURCE)
28+
#define _POSIX_C_SOURCE 200112L
29+
#endif
30+
2331
#if !defined(MBEDTLS_CONFIG_FILE)
2432
#include "mbedtls/config.h"
2533
#else
2634
#include MBEDTLS_CONFIG_FILE
2735
#endif
2836

2937
#include "mbedtls/platform_util.h"
38+
#include "mbedtls/threading.h"
3039

3140
#include <stddef.h>
3241
#include <string.h>
@@ -65,3 +74,62 @@ void mbedtls_platform_zeroize( void *buf, size_t len )
6574
memset_func( buf, 0, len );
6675
}
6776
#endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */
77+
78+
#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
79+
#include <time.h>
80+
#if !defined(_WIN32) && (defined(unix) || \
81+
defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
82+
defined(__MACH__)))
83+
#include <unistd.h>
84+
#endif /* !_WIN32 && (unix || __unix || __unix__ ||
85+
* (__APPLE__ && __MACH__)) */
86+
87+
#if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
88+
( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
89+
_POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) )
90+
/*
91+
* This is a convenience shorthand macro to avoid checking the long
92+
* preprocessor conditions above. Ideally, we could expose this macro in
93+
* platform_util.h and simply use it in platform_util.c, threading.c and
94+
* threading.h. However, this macro is not part of the Mbed TLS public API, so
95+
* we keep it private by only defining it in this file
96+
*/
97+
#if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) )
98+
#define PLATFORM_UTIL_USE_GMTIME
99+
#endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
100+
101+
#endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
102+
( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
103+
_POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) ) */
104+
105+
struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
106+
struct tm *tm_buf )
107+
{
108+
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
109+
return( ( gmtime_s( tm_buf, tt ) == 0 ) ? tm_buf : NULL );
110+
#elif !defined(PLATFORM_UTIL_USE_GMTIME)
111+
return( gmtime_r( tt, tm_buf ) );
112+
#else
113+
struct tm *lt;
114+
115+
#if defined(MBEDTLS_THREADING_C)
116+
if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 )
117+
return( NULL );
118+
#endif /* MBEDTLS_THREADING_C */
119+
120+
lt = gmtime( tt );
121+
122+
if( lt != NULL )
123+
{
124+
memcpy( tm_buf, lt, sizeof( struct tm ) );
125+
}
126+
127+
#if defined(MBEDTLS_THREADING_C)
128+
if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 )
129+
return( NULL );
130+
#endif /* MBEDTLS_THREADING_C */
131+
132+
return( ( lt == NULL ) ? NULL : tm_buf );
133+
#endif /* _WIN32 && !EFIX64 && !EFI32 */
134+
}
135+
#endif /* MBEDTLS_HAVE_TIME_DATE && MBEDTLS_PLATFORM_GMTIME_R_ALT */

features/mbedtls/src/threading.c

+47
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919
* This file is part of mbed TLS (https://tls.mbed.org)
2020
*/
2121

22+
/*
23+
* Ensure gmtime_r is available even with -std=c99; must be defined before
24+
* config.h, which pulls in glibc's features.h. Harmless on other platforms.
25+
*/
26+
#if !defined(_POSIX_C_SOURCE)
27+
#define _POSIX_C_SOURCE 200112L
28+
#endif
29+
2230
#if !defined(MBEDTLS_CONFIG_FILE)
2331
#include "mbedtls/config.h"
2432
#else
@@ -29,6 +37,36 @@
2937

3038
#include "mbedtls/threading.h"
3139

40+
#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
41+
42+
#if !defined(_WIN32) && (defined(unix) || \
43+
defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
44+
defined(__MACH__)))
45+
#include <unistd.h>
46+
#endif /* !_WIN32 && (unix || __unix || __unix__ ||
47+
* (__APPLE__ && __MACH__)) */
48+
49+
#if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
50+
( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
51+
_POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) )
52+
/*
53+
* This is a convenience shorthand macro to avoid checking the long
54+
* preprocessor conditions above. Ideally, we could expose this macro in
55+
* platform_util.h and simply use it in platform_util.c, threading.c and
56+
* threading.h. However, this macro is not part of the Mbed TLS public API, so
57+
* we keep it private by only defining it in this file
58+
*/
59+
60+
#if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) )
61+
#define THREADING_USE_GMTIME
62+
#endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
63+
64+
#endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
65+
( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
66+
_POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) ) */
67+
68+
#endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
69+
3270
#if defined(MBEDTLS_THREADING_PTHREAD)
3371
static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
3472
{
@@ -114,6 +152,9 @@ void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t *
114152
#if defined(MBEDTLS_FS_IO)
115153
mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
116154
#endif
155+
#if defined(THREADING_USE_GMTIME)
156+
mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
157+
#endif
117158
}
118159

119160
/*
@@ -124,6 +165,9 @@ void mbedtls_threading_free_alt( void )
124165
#if defined(MBEDTLS_FS_IO)
125166
mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
126167
#endif
168+
#if defined(THREADING_USE_GMTIME)
169+
mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
170+
#endif
127171
}
128172
#endif /* MBEDTLS_THREADING_ALT */
129173

@@ -136,5 +180,8 @@ void mbedtls_threading_free_alt( void )
136180
#if defined(MBEDTLS_FS_IO)
137181
mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
138182
#endif
183+
#if defined(THREADING_USE_GMTIME)
184+
mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
185+
#endif
139186

140187
#endif /* MBEDTLS_THREADING_C */

features/mbedtls/src/x509.c

+2-9
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@
2929
* http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
3030
*/
3131

32-
/* Ensure gmtime_r is available even with -std=c99; must be included before
33-
* config.h, which pulls in glibc's features.h. Harmless on other platforms. */
34-
#define _POSIX_C_SOURCE 200112L
35-
3632
#if !defined(MBEDTLS_CONFIG_FILE)
3733
#include "mbedtls/config.h"
3834
#else
@@ -67,6 +63,7 @@
6763
#include "mbedtls/platform_time.h"
6864
#endif
6965
#if defined(MBEDTLS_HAVE_TIME_DATE)
66+
#include "mbedtls/platform_util.h"
7067
#include <time.h>
7168
#endif
7269

@@ -901,11 +898,7 @@ static int x509_get_current_time( mbedtls_x509_time *now )
901898
int ret = 0;
902899

903900
tt = mbedtls_time( NULL );
904-
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
905-
lt = gmtime_s( &tm_buf, &tt ) == 0 ? &tm_buf : NULL;
906-
#else
907-
lt = gmtime_r( &tt, &tm_buf );
908-
#endif
901+
lt = mbedtls_platform_gmtime_r( &tt, &tm_buf );
909902

910903
if( lt == NULL )
911904
ret = -1;

0 commit comments

Comments
 (0)