Move from gmtime_r to gmtime + mutexes

* gmtime_r is not standard so -std=c99 warns about it
* Anyway we need global mutexes in the threading layer, so better depend only
  on that, rather that global mutexes + some _r functions
This commit is contained in:
Manuel Pégourié-Gonnard 2015-05-29 10:11:03 +02:00
parent ba19432d2e
commit 864108daab
3 changed files with 35 additions and 16 deletions

View file

@ -96,6 +96,7 @@ extern int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t *mutex );
* Global mutexes * Global mutexes
*/ */
extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex; extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex;
extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex;
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -112,6 +112,7 @@ void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t *
mbedtls_mutex_unlock = mutex_unlock; mbedtls_mutex_unlock = mutex_unlock;
mbedtls_mutex_init( &mbedtls_threading_readdir_mutex ); mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
} }
/* /*
@ -120,6 +121,7 @@ void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t *
void mbedtls_threading_free_alt( void ) void mbedtls_threading_free_alt( void )
{ {
mbedtls_mutex_free( &mbedtls_threading_readdir_mutex ); mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
} }
#endif /* MBEDTLS_THREADING_ALT */ #endif /* MBEDTLS_THREADING_ALT */
@ -130,5 +132,6 @@ void mbedtls_threading_free_alt( void )
#define MUTEX_INIT #define MUTEX_INIT
#endif #endif
mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT; mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
#endif /* MBEDTLS_THREADING_C */ #endif /* MBEDTLS_THREADING_C */

View file

@ -879,7 +879,7 @@ int mbedtls_x509_key_size_helper( char *buf, size_t size, const char *name )
*/ */
#if defined(MBEDTLS_HAVE_TIME) #if defined(MBEDTLS_HAVE_TIME)
static void x509_get_current_time( mbedtls_x509_time *now ) static int x509_get_current_time( mbedtls_x509_time *now )
{ {
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
SYSTEMTIME st; SYSTEMTIME st;
@ -887,25 +887,38 @@ static void x509_get_current_time( mbedtls_x509_time *now )
GetSystemTime( &st ); GetSystemTime( &st );
now->year = st.wYear; now->year = st.wYear;
now->mon = st.wMonth; now->mon = st.wMonth;
now->day = st.wDay; now->day = st.wDay;
now->hour = st.wHour; now->hour = st.wHour;
now->min = st.wMinute; now->min = st.wMinute;
now->sec = st.wSecond; now->sec = st.wSecond;
#else #else
struct tm lt; struct tm *lt;
time_t tt; time_t tt;
tt = time( NULL ); #if defined(MBEDTLS_THREADING_C)
gmtime_r( &tt, &lt ); if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 )
return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
#endif
tt = time( NULL );
lt = gmtime( &tt );
now->year = lt->tm_year + 1900;
now->mon = lt->tm_mon + 1;
now->day = lt->tm_mday;
now->hour = lt->tm_hour;
now->min = lt->tm_min;
now->sec = lt->tm_sec;
#if defined(MBEDTLS_THREADING_C)
if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 )
return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
#endif
now->year = lt.tm_year + 1900;
now->mon = lt.tm_mon + 1;
now->day = lt.tm_mday;
now->hour = lt.tm_hour;
now->min = lt.tm_min;
now->sec = lt.tm_sec;
#endif /* _WIN32 && !EFIX64 && !EFI32 */ #endif /* _WIN32 && !EFIX64 && !EFI32 */
return( 0 );
} }
/* /*
@ -953,7 +966,8 @@ int mbedtls_x509_time_expired( const mbedtls_x509_time *to )
{ {
mbedtls_x509_time now; mbedtls_x509_time now;
x509_get_current_time( &now ); if( x509_get_current_time( &now ) != 0 )
return( -1 );
return( x509_check_time( &now, to ) ); return( x509_check_time( &now, to ) );
} }
@ -962,7 +976,8 @@ int mbedtls_x509_time_future( const mbedtls_x509_time *from )
{ {
mbedtls_x509_time now; mbedtls_x509_time now;
x509_get_current_time( &now ); if( x509_get_current_time( &now ) != 0 )
return( -1 );
return( x509_check_time( from, &now ) ); return( x509_check_time( from, &now ) );
} }