mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-04-02 16:21:20 +00:00
Add helper function to find entry in SSL session cache
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
This commit is contained in:
parent
02a68ebc0e
commit
f938c436fb
@ -50,50 +50,66 @@ void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache )
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int ssl_cache_find_entry( mbedtls_ssl_cache_context *cache,
|
||||||
|
unsigned char const *session_id,
|
||||||
|
size_t session_id_len,
|
||||||
|
mbedtls_ssl_cache_entry **dst )
|
||||||
|
{
|
||||||
|
int ret = 1;
|
||||||
|
#if defined(MBEDTLS_HAVE_TIME)
|
||||||
|
mbedtls_time_t t = mbedtls_time( NULL );
|
||||||
|
#endif
|
||||||
|
mbedtls_ssl_cache_entry *cur;
|
||||||
|
|
||||||
|
for( cur = cache->chain; cur != NULL; cur = cur->next )
|
||||||
|
{
|
||||||
|
#if defined(MBEDTLS_HAVE_TIME)
|
||||||
|
if( cache->timeout != 0 &&
|
||||||
|
(int) ( t - cur->timestamp ) > cache->timeout )
|
||||||
|
continue;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if( session_id_len != cur->session.id_len ||
|
||||||
|
memcmp( session_id, cur->session.id,
|
||||||
|
cur->session.id_len ) != 0 )
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( cur != NULL )
|
||||||
|
{
|
||||||
|
*dst = cur;
|
||||||
|
ret = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return( ret );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int mbedtls_ssl_cache_get( void *data,
|
int mbedtls_ssl_cache_get( void *data,
|
||||||
unsigned char const *session_id,
|
unsigned char const *session_id,
|
||||||
size_t session_id_len,
|
size_t session_id_len,
|
||||||
mbedtls_ssl_session *session )
|
mbedtls_ssl_session *session )
|
||||||
{
|
{
|
||||||
int ret = 1;
|
int ret = 1;
|
||||||
#if defined(MBEDTLS_HAVE_TIME)
|
|
||||||
mbedtls_time_t t = mbedtls_time( NULL );
|
|
||||||
#endif
|
|
||||||
mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
|
mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
|
||||||
mbedtls_ssl_cache_entry *cur, *entry;
|
mbedtls_ssl_cache_entry *entry;
|
||||||
|
|
||||||
#if defined(MBEDTLS_THREADING_C)
|
#if defined(MBEDTLS_THREADING_C)
|
||||||
if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
|
if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
|
||||||
return( 1 );
|
return( 1 );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
cur = cache->chain;
|
ret = ssl_cache_find_entry( cache, session_id, session_id_len, &entry );
|
||||||
entry = NULL;
|
if( ret != 0 )
|
||||||
|
goto exit;
|
||||||
while( cur != NULL )
|
|
||||||
{
|
|
||||||
entry = cur;
|
|
||||||
cur = cur->next;
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_HAVE_TIME)
|
|
||||||
if( cache->timeout != 0 &&
|
|
||||||
(int) ( t - entry->timestamp ) > cache->timeout )
|
|
||||||
continue;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if( session_id_len != entry->session.id_len ||
|
|
||||||
memcmp( session_id, entry->session.id,
|
|
||||||
entry->session.id_len ) != 0 )
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = mbedtls_ssl_session_copy( session, &entry->session );
|
ret = mbedtls_ssl_session_copy( session, &entry->session );
|
||||||
if( ret != 0 )
|
if( ret != 0 )
|
||||||
{
|
|
||||||
ret = 1;
|
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
||||||
defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
||||||
@ -126,8 +142,6 @@ int mbedtls_ssl_cache_get( void *data,
|
|||||||
#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
#if defined(MBEDTLS_THREADING_C)
|
#if defined(MBEDTLS_THREADING_C)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user