Move existence check for pk/frame to mbedtls_x509_crt_provide_xxx()

This commit is contained in:
Hanno Becker 2019-03-05 15:29:23 +00:00
parent bc685199d9
commit 76428359b3
2 changed files with 21 additions and 45 deletions

View file

@ -817,41 +817,15 @@ int mbedtls_x509_crt_flush_cache_pk( mbedtls_x509_crt const *crt );
int mbedtls_x509_crt_cache_provide_frame( mbedtls_x509_crt const *crt );
int mbedtls_x509_crt_cache_provide_pk( mbedtls_x509_crt const *crt );
static inline int mbedtls_x509_crt_cache_frame_set(
mbedtls_x509_crt_cache *cache )
{
return( cache->frame != NULL );
}
static inline mbedtls_x509_crt_frame* mbedtls_x509_crt_cache_get_frame(
mbedtls_x509_crt_cache *cache )
{
return( cache->frame );
}
static inline int mbedtls_x509_crt_cache_pk_set(
mbedtls_x509_crt_cache *cache )
{
return( cache->pk != NULL );
}
static inline mbedtls_pk_context* mbedtls_x509_crt_cache_get_pk(
mbedtls_x509_crt_cache *cache )
{
return( cache->pk );
}
static inline int mbedtls_x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
mbedtls_x509_crt_frame **frame_ptr )
{
int ret;
#if defined(MBEDTLS_THREADING_C)
if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
#endif
if( !mbedtls_x509_crt_cache_frame_set( crt->cache ) )
{
int ret;
ret = mbedtls_x509_crt_cache_provide_frame( crt );
if( ret != 0 )
{
@ -861,9 +835,8 @@ static inline int mbedtls_x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
#endif
return( ret );
}
}
*frame_ptr = mbedtls_x509_crt_cache_get_frame( crt->cache );
*frame_ptr = crt->cache->frame;
return( 0 );
}
@ -883,14 +856,12 @@ static inline void mbedtls_x509_crt_frame_release( mbedtls_x509_crt const *crt )
static inline int mbedtls_x509_crt_pk_acquire( mbedtls_x509_crt const *crt,
mbedtls_pk_context **pk_ptr )
{
int ret;
#if defined(MBEDTLS_THREADING_C)
if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
#endif
if( !mbedtls_x509_crt_cache_pk_set( crt->cache ) )
{
int ret;
ret = mbedtls_x509_crt_cache_provide_pk( crt );
if( ret != 0 )
{
@ -900,9 +871,8 @@ static inline int mbedtls_x509_crt_pk_acquire( mbedtls_x509_crt const *crt,
#endif
return( ret );
}
}
*pk_ptr = mbedtls_x509_crt_cache_get_pk( crt->cache );
*pk_ptr = crt->cache->pk;
return( 0 );
}

View file

@ -165,6 +165,9 @@ int mbedtls_x509_crt_cache_provide_frame( mbedtls_x509_crt const *crt )
mbedtls_x509_crt_cache *cache = crt->cache;
mbedtls_x509_crt_frame *frame;
if( cache->frame != NULL )
return( 0 );
frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
if( frame == NULL )
return( MBEDTLS_ERR_X509_ALLOC_FAILED );
@ -214,6 +217,9 @@ int mbedtls_x509_crt_cache_provide_pk( mbedtls_x509_crt const *crt )
mbedtls_x509_crt_cache *cache = crt->cache;
mbedtls_pk_context *pk;
if( cache->pk != NULL )
return( 0 );
pk = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) );
if( pk == NULL )
return( MBEDTLS_ERR_X509_ALLOC_FAILED );
@ -1459,7 +1465,7 @@ static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
if( ret != 0 )
goto exit;
frame = mbedtls_x509_crt_cache_get_frame( crt->cache );
frame = crt->cache->frame;
#else /* MBEDTLS_X509_ON_DEMAND_PARSING */