From 76428359b374fc3b9b5bdc87142407ff8b1a32bd Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Tue, 5 Mar 2019 15:29:23 +0000 Subject: [PATCH] Move existence check for pk/frame to mbedtls_x509_crt_provide_xxx() --- include/mbedtls/x509_crt.h | 58 +++++++++----------------------------- library/x509_crt.c | 8 +++++- 2 files changed, 21 insertions(+), 45 deletions(-) diff --git a/include/mbedtls/x509_crt.h b/include/mbedtls/x509_crt.h index c7f816bc5..b3570be58 100644 --- a/include/mbedtls/x509_crt.h +++ b/include/mbedtls/x509_crt.h @@ -817,53 +817,26 @@ 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 ) ) + 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 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 ) ) + 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 91b29b614..9004be4f0 100644 --- a/library/x509_crt.c +++ b/library/x509_crt.c @@ -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 */