mbedtls/library/ssl_cache.c

330 lines
8.1 KiB
C
Raw Normal View History

/*
* SSL session cache implementation
*
2015-01-23 09:45:19 +00:00
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
*
2015-03-06 13:17:10 +00:00
* This file is part of mbed TLS (https://tls.mbed.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
* These session callbacks use a simple chained list
* to store and retrieve the session information.
*/
#if !defined(MBEDTLS_CONFIG_FILE)
2015-03-09 17:05:11 +00:00
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if defined(MBEDTLS_SSL_CACHE_C)
2015-03-09 17:05:11 +00:00
#include "mbedtls/ssl_cache.h"
#include <string.h>
#if defined(MBEDTLS_PLATFORM_C)
2015-03-09 17:05:11 +00:00
#include "mbedtls/platform.h"
#else
#include <stdlib.h>
#define mbedtls_malloc malloc
#define mbedtls_free free
#endif
void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache )
{
memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) );
cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
2013-09-28 13:01:27 +00:00
#if defined(MBEDTLS_THREADING_C)
mbedtls_mutex_init( &cache->mutex );
2013-09-28 13:01:27 +00:00
#endif
}
int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session )
{
2013-09-28 13:01:27 +00:00
int ret = 1;
#if defined(MBEDTLS_HAVE_TIME)
time_t t = time( NULL );
#endif
mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
mbedtls_ssl_cache_entry *cur, *entry;
#if defined(MBEDTLS_THREADING_C)
if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
2013-09-28 13:01:27 +00:00
return( 1 );
#endif
cur = cache->chain;
entry = NULL;
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->ciphersuite != entry->session.ciphersuite ||
session->compression != entry->session.compression ||
session->length != entry->session.length )
continue;
if( memcmp( session->id, entry->session.id,
entry->session.length ) != 0 )
continue;
memcpy( session->master, entry->session.master, 48 );
session->verify_result = entry->session.verify_result;
#if defined(MBEDTLS_X509_CRT_PARSE_C)
/*
* Restore peer certificate (without rest of the original chain)
*/
if( entry->peer_cert.p != NULL )
{
if( ( session->peer_cert = mbedtls_malloc(
sizeof(mbedtls_x509_crt) ) ) == NULL )
2013-09-28 13:01:27 +00:00
{
ret = 1;
goto exit;
}
mbedtls_x509_crt_init( session->peer_cert );
if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p,
entry->peer_cert.len ) != 0 )
{
mbedtls_free( session->peer_cert );
session->peer_cert = NULL;
2013-09-28 13:01:27 +00:00
ret = 1;
goto exit;
}
}
#endif /* MBEDTLS_X509_CRT_PARSE_C */
2013-09-28 13:01:27 +00:00
ret = 0;
goto exit;
}
2013-09-28 13:01:27 +00:00
exit:
#if defined(MBEDTLS_THREADING_C)
if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
2013-09-28 13:01:27 +00:00
ret = 1;
#endif
return( ret );
}
int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session )
{
2013-09-28 13:01:27 +00:00
int ret = 1;
#if defined(MBEDTLS_HAVE_TIME)
time_t t = time( NULL ), oldest = 0;
mbedtls_ssl_cache_entry *old = NULL;
#endif
mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
mbedtls_ssl_cache_entry *cur, *prv;
int count = 0;
#if defined(MBEDTLS_THREADING_C)
if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
2013-09-28 13:01:27 +00:00
return( ret );
#endif
cur = cache->chain;
prv = NULL;
while( cur != NULL )
{
count++;
#if defined(MBEDTLS_HAVE_TIME)
if( cache->timeout != 0 &&
(int) ( t - cur->timestamp ) > cache->timeout )
{
cur->timestamp = t;
break; /* expired, reuse this slot, update timestamp */
}
#endif
if( memcmp( session->id, cur->session.id, cur->session.length ) == 0 )
break; /* client reconnected, keep timestamp for session id */
#if defined(MBEDTLS_HAVE_TIME)
if( oldest == 0 || cur->timestamp < oldest )
{
oldest = cur->timestamp;
old = cur;
}
#endif
prv = cur;
cur = cur->next;
}
if( cur == NULL )
{
#if defined(MBEDTLS_HAVE_TIME)
/*
* Reuse oldest entry if max_entries reached
*/
if( count >= cache->max_entries )
{
if( old == NULL )
{
ret = 1;
goto exit;
}
cur = old;
}
#else /* MBEDTLS_HAVE_TIME */
/*
* Reuse first entry in chain if max_entries reached,
* but move to last place
*/
if( count >= cache->max_entries )
{
if( cache->chain == NULL )
2013-09-28 13:01:27 +00:00
{
ret = 1;
goto exit;
}
cur = cache->chain;
cache->chain = cur->next;
2014-02-26 16:38:55 +00:00
cur->next = NULL;
prv->next = cur;
}
#endif /* MBEDTLS_HAVE_TIME */
else
{
/*
* max_entries not reached, create new entry
*/
cur = mbedtls_malloc( sizeof(mbedtls_ssl_cache_entry) );
if( cur == NULL )
2013-09-28 13:01:27 +00:00
{
ret = 1;
goto exit;
}
memset( cur, 0, sizeof(mbedtls_ssl_cache_entry) );
if( prv == NULL )
cache->chain = cur;
else
prv->next = cur;
}
#if defined(MBEDTLS_HAVE_TIME)
cur->timestamp = t;
#endif
}
memcpy( &cur->session, session, sizeof( mbedtls_ssl_session ) );
#if defined(MBEDTLS_X509_CRT_PARSE_C)
2014-02-26 16:38:55 +00:00
/*
* If we're reusing an entry, free its certificate first
*/
if( cur->peer_cert.p != NULL )
{
mbedtls_free( cur->peer_cert.p );
memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) );
2014-02-26 16:38:55 +00:00
}
/*
* Store peer certificate
*/
if( session->peer_cert != NULL )
{
cur->peer_cert.p = mbedtls_malloc( session->peer_cert->raw.len );
if( cur->peer_cert.p == NULL )
2013-09-28 13:01:27 +00:00
{
ret = 1;
goto exit;
}
memcpy( cur->peer_cert.p, session->peer_cert->raw.p,
session->peer_cert->raw.len );
cur->peer_cert.len = session->peer_cert->raw.len;
cur->session.peer_cert = NULL;
}
#endif /* MBEDTLS_X509_CRT_PARSE_C */
2013-09-28 13:01:27 +00:00
ret = 0;
exit:
#if defined(MBEDTLS_THREADING_C)
if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
2013-09-28 13:01:27 +00:00
ret = 1;
#endif
return( ret );
}
#if defined(MBEDTLS_HAVE_TIME)
void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
{
if( timeout < 0 ) timeout = 0;
cache->timeout = timeout;
}
#endif /* MBEDTLS_HAVE_TIME */
void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
{
if( max < 0 ) max = 0;
cache->max_entries = max;
}
void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
{
mbedtls_ssl_cache_entry *cur, *prv;
cur = cache->chain;
while( cur != NULL )
{
prv = cur;
cur = cur->next;
mbedtls_ssl_session_free( &prv->session );
#if defined(MBEDTLS_X509_CRT_PARSE_C)
mbedtls_free( prv->peer_cert.p );
#endif /* MBEDTLS_X509_CRT_PARSE_C */
mbedtls_free( prv );
}
2013-09-28 13:01:27 +00:00
#if defined(MBEDTLS_THREADING_C)
mbedtls_mutex_free( &cache->mutex );
2013-09-28 13:01:27 +00:00
#endif
}
#endif /* MBEDTLS_SSL_CACHE_C */