Move existence check for pk/frame to mbedtls_x509_crt_provide_xxx()
diff --git a/include/mbedtls/x509_crt.h b/include/mbedtls/x509_crt.h
index c7f816b..b3570be 100644
--- a/include/mbedtls/x509_crt.h
+++ b/include/mbedtls/x509_crt.h
@@ -817,53 +817,26 @@
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 ) )
+ ret = mbedtls_x509_crt_cache_provide_frame( crt );
+ if( ret != 0 )
{
- int ret;
- ret = mbedtls_x509_crt_cache_provide_frame( crt );
- if( ret != 0 )
- {
#if defined(MBEDTLS_THREADING_C)
- if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
- return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
+ if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
+ return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
#endif
- return( ret );
- }
+ return( ret );
}
- *frame_ptr = mbedtls_x509_crt_cache_get_frame( crt->cache );
+ *frame_ptr = crt->cache->frame;
return( 0 );
}
@@ -883,26 +856,23 @@
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 ) )
+ ret = mbedtls_x509_crt_cache_provide_pk( crt );
+ if( ret != 0 )
{
- int ret;
- ret = mbedtls_x509_crt_cache_provide_pk( crt );
- if( ret != 0 )
- {
#if defined(MBEDTLS_THREADING_C)
- if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
- return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
+ if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
+ return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
#endif
- return( ret );
- }
+ return( ret );
}
- *pk_ptr = mbedtls_x509_crt_cache_get_pk( crt->cache );
+ *pk_ptr = crt->cache->pk;
return( 0 );
}
diff --git a/library/x509_crt.c b/library/x509_crt.c
index 91b29b6..9004be4 100644
--- a/library/x509_crt.c
+++ b/library/x509_crt.c
@@ -165,6 +165,9 @@
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 @@
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 @@
if( ret != 0 )
goto exit;
- frame = mbedtls_x509_crt_cache_get_frame( crt->cache );
+ frame = crt->cache->frame;
#else /* MBEDTLS_X509_ON_DEMAND_PARSING */