2 * SSL session cache implementation
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 * These session callbacks use a simple chained list
23 * to store and retrieve the session information.
26 #if !defined(MBEDTLS_CONFIG_FILE)
27 #include "mbedtls/config.h"
29 #include MBEDTLS_CONFIG_FILE
32 #if defined(MBEDTLS_SSL_CACHE_C)
34 #if defined(MBEDTLS_PLATFORM_C)
35 #include "mbedtls/platform.h"
38 #define mbedtls_calloc calloc
39 #define mbedtls_free free
42 #include "mbedtls/ssl_cache.h"
43 #include "mbedtls/ssl_internal.h"
47 void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache )
49 memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) );
51 cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
52 cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
54 #if defined(MBEDTLS_THREADING_C)
55 mbedtls_mutex_init( &cache->mutex );
59 int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session )
62 #if defined(MBEDTLS_HAVE_TIME)
63 mbedtls_time_t t = mbedtls_time( NULL );
65 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
66 mbedtls_ssl_cache_entry *cur, *entry;
68 #if defined(MBEDTLS_THREADING_C)
69 if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
81 #if defined(MBEDTLS_HAVE_TIME)
82 if( cache->timeout != 0 &&
83 (int) ( t - entry->timestamp ) > cache->timeout )
87 if( session->ciphersuite != entry->session.ciphersuite ||
88 session->compression != entry->session.compression ||
89 session->id_len != entry->session.id_len )
92 if( memcmp( session->id, entry->session.id,
93 entry->session.id_len ) != 0 )
96 ret = mbedtls_ssl_session_copy( session, &entry->session );
103 #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
104 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
106 * Restore peer certificate (without rest of the original chain)
108 if( entry->peer_cert.p != NULL )
110 /* `session->peer_cert` is NULL after the call to
111 * mbedtls_ssl_session_copy(), because cache entries
112 * have the `peer_cert` field set to NULL. */
114 if( ( session->peer_cert = mbedtls_calloc( 1,
115 sizeof(mbedtls_x509_crt) ) ) == NULL )
121 mbedtls_x509_crt_init( session->peer_cert );
122 if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p,
123 entry->peer_cert.len ) != 0 )
125 mbedtls_free( session->peer_cert );
126 session->peer_cert = NULL;
131 #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
138 #if defined(MBEDTLS_THREADING_C)
139 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
146 int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session )
149 #if defined(MBEDTLS_HAVE_TIME)
150 mbedtls_time_t t = mbedtls_time( NULL ), oldest = 0;
151 mbedtls_ssl_cache_entry *old = NULL;
153 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
154 mbedtls_ssl_cache_entry *cur, *prv;
157 #if defined(MBEDTLS_THREADING_C)
158 if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
169 #if defined(MBEDTLS_HAVE_TIME)
170 if( cache->timeout != 0 &&
171 (int) ( t - cur->timestamp ) > cache->timeout )
174 break; /* expired, reuse this slot, update timestamp */
178 if( memcmp( session->id, cur->session.id, cur->session.id_len ) == 0 )
179 break; /* client reconnected, keep timestamp for session id */
181 #if defined(MBEDTLS_HAVE_TIME)
182 if( oldest == 0 || cur->timestamp < oldest )
184 oldest = cur->timestamp;
195 #if defined(MBEDTLS_HAVE_TIME)
197 * Reuse oldest entry if max_entries reached
199 if( count >= cache->max_entries )
209 #else /* MBEDTLS_HAVE_TIME */
211 * Reuse first entry in chain if max_entries reached,
212 * but move to last place
214 if( count >= cache->max_entries )
216 if( cache->chain == NULL )
223 cache->chain = cur->next;
227 #endif /* MBEDTLS_HAVE_TIME */
231 * max_entries not reached, create new entry
233 cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) );
246 #if defined(MBEDTLS_HAVE_TIME)
251 #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
252 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
254 * If we're reusing an entry, free its certificate first
256 if( cur->peer_cert.p != NULL )
258 mbedtls_free( cur->peer_cert.p );
259 memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) );
261 #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
263 /* Copy the entire session; this temporarily makes a copy of the
264 * X.509 CRT structure even though we only want to store the raw CRT.
265 * This inefficiency will go away as soon as we implement on-demand
266 * parsing of CRTs, in which case there's no need for the `peer_cert`
267 * field anymore in the first place, and we're done after this call. */
268 ret = mbedtls_ssl_session_copy( &cur->session, session );
275 #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
276 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
277 /* If present, free the X.509 structure and only store the raw CRT data. */
278 if( cur->session.peer_cert != NULL )
281 mbedtls_calloc( 1, cur->session.peer_cert->raw.len );
282 if( cur->peer_cert.p == NULL )
288 memcpy( cur->peer_cert.p,
289 cur->session.peer_cert->raw.p,
290 cur->session.peer_cert->raw.len );
291 cur->peer_cert.len = session->peer_cert->raw.len;
293 mbedtls_x509_crt_free( cur->session.peer_cert );
294 mbedtls_free( cur->session.peer_cert );
295 cur->session.peer_cert = NULL;
297 #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
302 #if defined(MBEDTLS_THREADING_C)
303 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
310 #if defined(MBEDTLS_HAVE_TIME)
311 void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
313 if( timeout < 0 ) timeout = 0;
315 cache->timeout = timeout;
317 #endif /* MBEDTLS_HAVE_TIME */
319 void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
321 if( max < 0 ) max = 0;
323 cache->max_entries = max;
326 void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
328 mbedtls_ssl_cache_entry *cur, *prv;
337 mbedtls_ssl_session_free( &prv->session );
339 #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
340 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
341 mbedtls_free( prv->peer_cert.p );
342 #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
347 #if defined(MBEDTLS_THREADING_C)
348 mbedtls_mutex_free( &cache->mutex );
353 #endif /* MBEDTLS_SSL_CACHE_C */