2012-09-25 23:55:46 +02:00
|
|
|
/*
|
|
|
|
* SSL session cache implementation
|
|
|
|
*
|
2014-02-01 22:50:26 +01:00
|
|
|
* Copyright (C) 2006-2014, Brainspark B.V.
|
2012-09-25 23:55:46 +02:00
|
|
|
*
|
|
|
|
* This file is part of PolarSSL (http://www.polarssl.org)
|
|
|
|
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-04-29 12:39:06 +02:00
|
|
|
#if !defined(POLARSSL_CONFIG_FILE)
|
2012-09-25 23:55:46 +02:00
|
|
|
#include "polarssl/config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2012-09-25 23:55:46 +02:00
|
|
|
|
|
|
|
#if defined(POLARSSL_SSL_CACHE_C)
|
|
|
|
|
|
|
|
#include "polarssl/ssl_cache.h"
|
|
|
|
|
2014-02-01 22:50:26 +01:00
|
|
|
#if defined(POLARSSL_PLATFORM_C)
|
|
|
|
#include "polarssl/platform.h"
|
2013-07-03 13:37:05 +02:00
|
|
|
#else
|
|
|
|
#define polarssl_malloc malloc
|
|
|
|
#define polarssl_free free
|
|
|
|
#endif
|
|
|
|
|
2012-09-25 23:55:46 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
void ssl_cache_init( ssl_cache_context *cache )
|
|
|
|
{
|
|
|
|
memset( cache, 0, sizeof( ssl_cache_context ) );
|
|
|
|
|
|
|
|
cache->timeout = SSL_CACHE_DEFAULT_TIMEOUT;
|
2012-10-24 00:18:28 +02:00
|
|
|
cache->max_entries = SSL_CACHE_DEFAULT_MAX_ENTRIES;
|
2013-09-28 15:01:27 +02:00
|
|
|
|
|
|
|
#if defined(POLARSSL_THREADING_C)
|
|
|
|
polarssl_mutex_init( &cache->mutex );
|
|
|
|
#endif
|
2012-09-25 23:55:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int ssl_cache_get( void *data, ssl_session *session )
|
|
|
|
{
|
2013-09-28 15:01:27 +02:00
|
|
|
int ret = 1;
|
2013-07-03 15:31:03 +02:00
|
|
|
#if defined(POLARSSL_HAVE_TIME)
|
2012-09-25 23:55:46 +02:00
|
|
|
time_t t = time( NULL );
|
2013-07-03 15:31:03 +02:00
|
|
|
#endif
|
2012-09-25 23:55:46 +02:00
|
|
|
ssl_cache_context *cache = (ssl_cache_context *) data;
|
|
|
|
ssl_cache_entry *cur, *entry;
|
|
|
|
|
2013-09-28 15:01:27 +02:00
|
|
|
#if defined(POLARSSL_THREADING_C)
|
|
|
|
if( polarssl_mutex_lock( &cache->mutex ) != 0 )
|
|
|
|
return( 1 );
|
|
|
|
#endif
|
|
|
|
|
2012-09-25 23:55:46 +02:00
|
|
|
cur = cache->chain;
|
|
|
|
entry = NULL;
|
|
|
|
|
|
|
|
while( cur != NULL )
|
|
|
|
{
|
|
|
|
entry = cur;
|
|
|
|
cur = cur->next;
|
|
|
|
|
2013-07-03 15:31:03 +02:00
|
|
|
#if defined(POLARSSL_HAVE_TIME)
|
2012-09-25 23:55:46 +02:00
|
|
|
if( cache->timeout != 0 &&
|
|
|
|
(int) ( t - entry->timestamp ) > cache->timeout )
|
|
|
|
continue;
|
2013-07-03 15:31:03 +02:00
|
|
|
#endif
|
2012-09-25 23:55:46 +02:00
|
|
|
|
|
|
|
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 );
|
2013-03-06 17:40:46 +01:00
|
|
|
|
2013-08-23 10:44:29 +02:00
|
|
|
session->verify_result = entry->session.verify_result;
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
#if defined(POLARSSL_X509_CRT_PARSE_C)
|
2013-03-06 17:40:46 +01:00
|
|
|
/*
|
|
|
|
* Restore peer certificate (without rest of the original chain)
|
|
|
|
*/
|
|
|
|
if( entry->peer_cert.p != NULL )
|
|
|
|
{
|
2014-11-12 22:27:42 +01:00
|
|
|
if( ( session->peer_cert = (x509_crt *) polarssl_malloc(
|
|
|
|
sizeof(x509_crt) ) ) == NULL )
|
2013-09-28 15:01:27 +02:00
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
2013-03-06 17:40:46 +01:00
|
|
|
|
2013-09-18 14:17:41 +02:00
|
|
|
x509_crt_init( session->peer_cert );
|
2013-09-18 13:46:23 +02:00
|
|
|
if( x509_crt_parse( session->peer_cert, entry->peer_cert.p,
|
|
|
|
entry->peer_cert.len ) != 0 )
|
2013-03-06 17:40:46 +01:00
|
|
|
{
|
2013-07-03 13:37:05 +02:00
|
|
|
polarssl_free( session->peer_cert );
|
2013-03-06 17:40:46 +01:00
|
|
|
session->peer_cert = NULL;
|
2013-09-28 15:01:27 +02:00
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
2013-03-06 17:40:46 +01:00
|
|
|
}
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif /* POLARSSL_X509_CRT_PARSE_C */
|
2013-03-06 17:40:46 +01:00
|
|
|
|
2013-09-28 15:01:27 +02:00
|
|
|
ret = 0;
|
|
|
|
goto exit;
|
2012-09-25 23:55:46 +02:00
|
|
|
}
|
|
|
|
|
2013-09-28 15:01:27 +02:00
|
|
|
exit:
|
|
|
|
#if defined(POLARSSL_THREADING_C)
|
|
|
|
if( polarssl_mutex_unlock( &cache->mutex ) != 0 )
|
|
|
|
ret = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return( ret );
|
2012-09-25 23:55:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int ssl_cache_set( void *data, const ssl_session *session )
|
|
|
|
{
|
2013-09-28 15:01:27 +02:00
|
|
|
int ret = 1;
|
2013-07-03 15:31:03 +02:00
|
|
|
#if defined(POLARSSL_HAVE_TIME)
|
2012-10-24 00:18:28 +02:00
|
|
|
time_t t = time( NULL ), oldest = 0;
|
2013-07-03 15:31:03 +02:00
|
|
|
ssl_cache_entry *old = NULL;
|
|
|
|
#endif
|
2012-09-25 23:55:46 +02:00
|
|
|
ssl_cache_context *cache = (ssl_cache_context *) data;
|
2013-07-03 15:31:03 +02:00
|
|
|
ssl_cache_entry *cur, *prv;
|
2012-10-24 00:18:28 +02:00
|
|
|
int count = 0;
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2013-09-28 15:01:27 +02:00
|
|
|
#if defined(POLARSSL_THREADING_C)
|
|
|
|
if( ( ret = polarssl_mutex_lock( &cache->mutex ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
#endif
|
|
|
|
|
2012-09-25 23:55:46 +02:00
|
|
|
cur = cache->chain;
|
|
|
|
prv = NULL;
|
|
|
|
|
|
|
|
while( cur != NULL )
|
|
|
|
{
|
2012-10-24 00:18:28 +02:00
|
|
|
count++;
|
|
|
|
|
2013-07-03 15:31:03 +02:00
|
|
|
#if defined(POLARSSL_HAVE_TIME)
|
2012-09-25 23:55:46 +02:00
|
|
|
if( cache->timeout != 0 &&
|
|
|
|
(int) ( t - cur->timestamp ) > cache->timeout )
|
|
|
|
{
|
|
|
|
cur->timestamp = t;
|
|
|
|
break; /* expired, reuse this slot, update timestamp */
|
|
|
|
}
|
2013-07-03 15:31:03 +02:00
|
|
|
#endif
|
2012-09-25 23:55:46 +02:00
|
|
|
|
|
|
|
if( memcmp( session->id, cur->session.id, cur->session.length ) == 0 )
|
|
|
|
break; /* client reconnected, keep timestamp for session id */
|
|
|
|
|
2013-07-03 15:31:03 +02:00
|
|
|
#if defined(POLARSSL_HAVE_TIME)
|
2012-10-24 00:18:28 +02:00
|
|
|
if( oldest == 0 || cur->timestamp < oldest )
|
|
|
|
{
|
|
|
|
oldest = cur->timestamp;
|
|
|
|
old = cur;
|
|
|
|
}
|
2013-07-03 15:31:03 +02:00
|
|
|
#endif
|
2012-10-24 00:18:28 +02:00
|
|
|
|
2012-09-25 23:55:46 +02:00
|
|
|
prv = cur;
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( cur == NULL )
|
|
|
|
{
|
2013-07-03 15:31:03 +02:00
|
|
|
#if defined(POLARSSL_HAVE_TIME)
|
2012-10-24 00:18:28 +02:00
|
|
|
/*
|
|
|
|
* Reuse oldest entry if max_entries reached
|
|
|
|
*/
|
2014-02-20 21:32:08 +01:00
|
|
|
if( count >= cache->max_entries )
|
2012-10-24 00:18:28 +02:00
|
|
|
{
|
2014-02-20 21:32:08 +01:00
|
|
|
if( old == NULL )
|
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2012-10-24 00:18:28 +02:00
|
|
|
cur = old;
|
|
|
|
}
|
2013-07-03 15:31:03 +02:00
|
|
|
#else /* POLARSSL_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 15:01:27 +02:00
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
2013-07-03 15:31:03 +02:00
|
|
|
|
|
|
|
cur = cache->chain;
|
|
|
|
cache->chain = cur->next;
|
2014-02-26 17:38:55 +01:00
|
|
|
cur->next = NULL;
|
2013-07-03 15:31:03 +02:00
|
|
|
prv->next = cur;
|
|
|
|
}
|
|
|
|
#endif /* POLARSSL_HAVE_TIME */
|
2012-10-24 00:18:28 +02:00
|
|
|
else
|
|
|
|
{
|
2014-02-20 21:32:08 +01:00
|
|
|
/*
|
|
|
|
* max_entries not reached, create new entry
|
|
|
|
*/
|
2014-11-12 22:27:42 +01:00
|
|
|
cur = (ssl_cache_entry *) polarssl_malloc( sizeof(ssl_cache_entry) );
|
2012-10-24 00:18:28 +02:00
|
|
|
if( cur == NULL )
|
2013-09-28 15:01:27 +02:00
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2013-03-06 17:40:46 +01:00
|
|
|
memset( cur, 0, sizeof(ssl_cache_entry) );
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2012-10-24 00:18:28 +02:00
|
|
|
if( prv == NULL )
|
|
|
|
cache->chain = cur;
|
|
|
|
else
|
|
|
|
prv->next = cur;
|
|
|
|
}
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2013-07-03 15:31:03 +02:00
|
|
|
#if defined(POLARSSL_HAVE_TIME)
|
2012-09-25 23:55:46 +02:00
|
|
|
cur->timestamp = t;
|
2013-07-03 15:31:03 +02:00
|
|
|
#endif
|
2012-09-25 23:55:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( &cur->session, session, sizeof( ssl_session ) );
|
2013-04-18 22:46:23 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
#if defined(POLARSSL_X509_CRT_PARSE_C)
|
2014-02-26 17:38:55 +01:00
|
|
|
/*
|
|
|
|
* If we're reusing an entry, free its certificate first
|
|
|
|
*/
|
|
|
|
if( cur->peer_cert.p != NULL )
|
|
|
|
{
|
|
|
|
polarssl_free( cur->peer_cert.p );
|
|
|
|
memset( &cur->peer_cert, 0, sizeof(x509_buf) );
|
|
|
|
}
|
|
|
|
|
2013-03-06 17:40:46 +01:00
|
|
|
/*
|
|
|
|
* Store peer certificate
|
|
|
|
*/
|
|
|
|
if( session->peer_cert != NULL )
|
|
|
|
{
|
2014-11-12 22:27:42 +01:00
|
|
|
cur->peer_cert.p = (unsigned char *) polarssl_malloc(
|
|
|
|
session->peer_cert->raw.len );
|
2013-03-06 17:40:46 +01:00
|
|
|
if( cur->peer_cert.p == NULL )
|
2013-09-28 15:01:27 +02:00
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
2013-03-06 17:40:46 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif /* POLARSSL_X509_CRT_PARSE_C */
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2013-09-28 15:01:27 +02:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
#if defined(POLARSSL_THREADING_C)
|
|
|
|
if( polarssl_mutex_unlock( &cache->mutex ) != 0 )
|
|
|
|
ret = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return( ret );
|
2012-09-25 23:55:46 +02:00
|
|
|
}
|
|
|
|
|
2013-07-03 15:31:03 +02:00
|
|
|
#if defined(POLARSSL_HAVE_TIME)
|
2012-09-25 23:55:46 +02:00
|
|
|
void ssl_cache_set_timeout( ssl_cache_context *cache, int timeout )
|
|
|
|
{
|
|
|
|
if( timeout < 0 ) timeout = 0;
|
|
|
|
|
|
|
|
cache->timeout = timeout;
|
|
|
|
}
|
2013-07-03 15:31:03 +02:00
|
|
|
#endif /* POLARSSL_HAVE_TIME */
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2012-10-24 00:18:28 +02:00
|
|
|
void ssl_cache_set_max_entries( ssl_cache_context *cache, int max )
|
|
|
|
{
|
|
|
|
if( max < 0 ) max = 0;
|
|
|
|
|
|
|
|
cache->max_entries = max;
|
|
|
|
}
|
|
|
|
|
2012-09-25 23:55:46 +02:00
|
|
|
void ssl_cache_free( ssl_cache_context *cache )
|
|
|
|
{
|
|
|
|
ssl_cache_entry *cur, *prv;
|
|
|
|
|
|
|
|
cur = cache->chain;
|
|
|
|
|
|
|
|
while( cur != NULL )
|
|
|
|
{
|
|
|
|
prv = cur;
|
|
|
|
cur = cur->next;
|
|
|
|
|
|
|
|
ssl_session_free( &prv->session );
|
2013-03-06 17:40:46 +01:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
#if defined(POLARSSL_X509_CRT_PARSE_C)
|
2014-05-28 11:33:54 +02:00
|
|
|
polarssl_free( prv->peer_cert.p );
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif /* POLARSSL_X509_CRT_PARSE_C */
|
2013-03-06 17:40:46 +01:00
|
|
|
|
2013-07-03 13:37:05 +02:00
|
|
|
polarssl_free( prv );
|
2012-09-25 23:55:46 +02:00
|
|
|
}
|
2013-09-28 15:01:27 +02:00
|
|
|
|
|
|
|
#if defined(POLARSSL_THREADING_C)
|
|
|
|
polarssl_mutex_free( &cache->mutex );
|
|
|
|
#endif
|
2012-09-25 23:55:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* POLARSSL_SSL_CACHE_C */
|