blob: 1bbcc957b971ac4c5d1f8ae8b9562dd14d2e9215 [file] [log] [blame]
Paul Bakker0a597072012-09-25 21:55:46 +00001/*
2 * SSL session cache implementation
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
Paul Bakker0a597072012-09-25 21:55:46 +000018 */
19/*
20 * These session callbacks use a simple chained list
21 * to store and retrieve the session information.
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker0a597072012-09-25 21:55:46 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_CACHE_C)
Paul Bakker0a597072012-09-25 21:55:46 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020030#else
Rich Evans00ab4702015-02-06 13:43:58 +000031#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020032#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010033#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020034#endif
35
SimonBd5800b72016-04-26 07:43:27 +010036#include "mbedtls/ssl_cache.h"
Chris Jones84a773f2021-03-05 18:38:47 +000037#include "ssl_misc.h"
SimonBd5800b72016-04-26 07:43:27 +010038
39#include <string.h>
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache )
Paul Bakker0a597072012-09-25 21:55:46 +000042{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) );
Paul Bakker0a597072012-09-25 21:55:46 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045 cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
46 cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
Paul Bakkerc5598842013-09-28 15:01:27 +020047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_THREADING_C)
49 mbedtls_mutex_init( &cache->mutex );
Paul Bakkerc5598842013-09-28 15:01:27 +020050#endif
Paul Bakker0a597072012-09-25 21:55:46 +000051}
52
Hanno Beckerf938c432021-04-15 10:17:53 +010053static int ssl_cache_find_entry( mbedtls_ssl_cache_context *cache,
54 unsigned char const *session_id,
55 size_t session_id_len,
56 mbedtls_ssl_cache_entry **dst )
57{
58 int ret = 1;
59#if defined(MBEDTLS_HAVE_TIME)
60 mbedtls_time_t t = mbedtls_time( NULL );
61#endif
62 mbedtls_ssl_cache_entry *cur;
63
64 for( cur = cache->chain; cur != NULL; cur = cur->next )
65 {
66#if defined(MBEDTLS_HAVE_TIME)
67 if( cache->timeout != 0 &&
68 (int) ( t - cur->timestamp ) > cache->timeout )
69 continue;
70#endif
71
Hanno Becker7e6eb9f2021-04-15 10:26:06 +010072 if( session_id_len != cur->session_id_len ||
73 memcmp( session_id, cur->session_id,
74 cur->session_id_len ) != 0 )
Hanno Beckerf938c432021-04-15 10:17:53 +010075 {
76 continue;
77 }
78
79 break;
80 }
81
82 if( cur != NULL )
83 {
84 *dst = cur;
85 ret = 0;
86 }
87
88 return( ret );
89}
90
91
Hanno Beckerccdaf6e2021-04-15 09:26:17 +010092int mbedtls_ssl_cache_get( void *data,
93 unsigned char const *session_id,
94 size_t session_id_len,
95 mbedtls_ssl_session *session )
Paul Bakker0a597072012-09-25 21:55:46 +000096{
Paul Bakkerc5598842013-09-28 15:01:27 +020097 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
Hanno Beckerf938c432021-04-15 10:17:53 +010099 mbedtls_ssl_cache_entry *entry;
Paul Bakker0a597072012-09-25 21:55:46 +0000100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if defined(MBEDTLS_THREADING_C)
102 if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200103 return( 1 );
104#endif
105
Hanno Beckerf938c432021-04-15 10:17:53 +0100106 ret = ssl_cache_find_entry( cache, session_id, session_id_len, &entry );
107 if( ret != 0 )
108 goto exit;
Paul Bakker0a597072012-09-25 21:55:46 +0000109
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100110 ret = mbedtls_ssl_session_load( session,
111 entry->session,
112 entry->session_len );
Hanno Beckerf938c432021-04-15 10:17:53 +0100113 if( ret != 0 )
114 goto exit;
115
Hanno Beckerf938c432021-04-15 10:17:53 +0100116 ret = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000117
Paul Bakkerc5598842013-09-28 15:01:27 +0200118exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119#if defined(MBEDTLS_THREADING_C)
120 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200121 ret = 1;
122#endif
123
124 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000125}
126
Hanno Beckerf47199d2021-05-13 06:29:13 +0100127static int ssl_cache_pick_writing_slot( mbedtls_ssl_cache_context *cache,
128 unsigned char const *session_id,
129 size_t session_id_len,
130 mbedtls_ssl_cache_entry **dst )
Paul Bakker0a597072012-09-25 21:55:46 +0000131{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if defined(MBEDTLS_HAVE_TIME)
Simon Butchera55e0842017-07-28 23:46:43 +0100133 mbedtls_time_t t = mbedtls_time( NULL ), oldest = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_ssl_cache_entry *old = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200135#endif
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000136 int count = 0;
Hanno Becker02a68eb2021-04-15 09:57:17 +0100137 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakkerc5598842013-09-28 15:01:27 +0200138
Paul Bakker0a597072012-09-25 21:55:46 +0000139 cur = cache->chain;
140 prv = NULL;
141
Hanno Becker845ceb72021-05-13 07:05:07 +0100142 /* Check 1: Is there already an entry with the given session ID?
143 *
144 * If yes, overwrite it.
145 *
146 * If not, `count` will hold the size of the session cache
147 * at the end of this loop, and `prv` will point to the last
148 * entry, both of which will be used later. */
149
Paul Bakker0a597072012-09-25 21:55:46 +0000150 while( cur != NULL )
151 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000152 count++;
153
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100154 if( session_id_len == cur->session_id_len &&
155 memcmp( session_id, cur->session_id, cur->session_id_len ) == 0 )
Hanno Beckerccdaf6e2021-04-15 09:26:17 +0100156 {
Hanno Becker845ceb72021-05-13 07:05:07 +0100157 goto found;
Hanno Beckerccdaf6e2021-04-15 09:26:17 +0100158 }
Paul Bakker0a597072012-09-25 21:55:46 +0000159
160 prv = cur;
161 cur = cur->next;
162 }
163
Hanno Becker845ceb72021-05-13 07:05:07 +0100164 /* Check 2: Is there an outdated entry in the cache?
165 *
166 * If so, overwrite it.
167 *
168 * If not, remember the oldest entry in `old` for later.
169 */
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker845ceb72021-05-13 07:05:07 +0100172 while( cur != NULL )
173 {
174 if( cache->timeout != 0 &&
175 (int) ( t - cur->timestamp ) > cache->timeout )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000176 {
Hanno Becker845ceb72021-05-13 07:05:07 +0100177 goto found;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000178 }
Hanno Becker845ceb72021-05-13 07:05:07 +0100179
180 if( oldest == 0 || cur->timestamp < oldest )
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200181 {
Hanno Becker845ceb72021-05-13 07:05:07 +0100182 oldest = cur->timestamp;
183 old = cur;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200184 }
Hanno Becker845ceb72021-05-13 07:05:07 +0100185
186 prv = cur;
187 cur = cur->next;
188 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#endif /* MBEDTLS_HAVE_TIME */
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000190
Hanno Becker845ceb72021-05-13 07:05:07 +0100191 /* Check 3: Is there free space in the cache? */
192
193 if( count < cache->max_entries )
194 {
195 /* Create new entry */
196 cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) );
197 if( cur == NULL )
198 return( 1 );
199
200 /* Append to the end of the linked list. */
201 if( prv == NULL )
202 cache->chain = cur;
203 else
204 prv->next = cur;
205
206 goto found;
207 }
208
209 /* Last resort: The cache is full and doesn't contain any outdated
210 * elements. In this case, we evict the oldest one, judged by timestamp
211 * (if present) or cache-order. */
Paul Bakker0a597072012-09-25 21:55:46 +0000212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker845ceb72021-05-13 07:05:07 +0100214 if( old == NULL )
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100215 {
Hanno Becker845ceb72021-05-13 07:05:07 +0100216 /* This should only happen on an ill-configured cache
217 * with max_entries == 0. */
218 return( 1 );
219 }
220#else /* MBEDTLS_HAVE_TIME */
221 /* Reuse first entry in chain, but move to last place. */
222 if( cache->chain == NULL )
223 return( 1 );
Hanno Becker02a68eb2021-04-15 09:57:17 +0100224
Hanno Becker845ceb72021-05-13 07:05:07 +0100225 old = cache->chain;
226 cache->chain = old->next;
227 cur->next = NULL;
228 prv->next = old;
229#endif /* MBEDTLS_HAVE_TIME */
Hanno Becker02a68eb2021-04-15 09:57:17 +0100230
Hanno Becker845ceb72021-05-13 07:05:07 +0100231 /* Now `old` points to the oldest entry to be overwritten. */
232 cur = old;
233
234found:
235
236#if defined(MBEDTLS_HAVE_TIME)
237 cur->timestamp = t;
238#endif
239
240 /* If we're reusing an entry, free it first. */
241 if( cur->session != NULL )
242 {
243 mbedtls_free( cur->session );
244 cur->session = NULL;
245 cur->session_len = 0;
246 memset( cur->session_id, 0, sizeof( cur->session_id ) );
247 cur->session_id_len = 0;
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100248 }
Hanno Becker02a68eb2021-04-15 09:57:17 +0100249
Hanno Becker845ceb72021-05-13 07:05:07 +0100250 *dst = cur;
251 return( 0 );
Hanno Becker02a68eb2021-04-15 09:57:17 +0100252}
253
254int mbedtls_ssl_cache_set( void *data,
255 unsigned char const *session_id,
256 size_t session_id_len,
257 const mbedtls_ssl_session *session )
258{
259 int ret = 1;
260 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
261 mbedtls_ssl_cache_entry *cur;
262
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100263 size_t session_serialized_len;
264 unsigned char *session_serialized = NULL;
265
Hanno Becker02a68eb2021-04-15 09:57:17 +0100266#if defined(MBEDTLS_THREADING_C)
267 if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
268 return( ret );
269#endif
270
Hanno Beckerf47199d2021-05-13 06:29:13 +0100271 ret = ssl_cache_pick_writing_slot( cache,
272 session_id, session_id_len,
273 &cur );
Hanno Becker02a68eb2021-04-15 09:57:17 +0100274 if( ret != 0 )
275 goto exit;
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100276
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100277 /* Check how much space we need to serialize the session
278 * and allocate a sufficiently large buffer. */
279 ret = mbedtls_ssl_session_save( session, NULL, 0, &session_serialized_len );
280 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Paul Bakkere81beda2013-03-06 17:40:46 +0100281 {
Hanno Beckeraee87172019-02-06 14:53:19 +0000282 ret = 1;
283 goto exit;
284 }
285
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100286 session_serialized = mbedtls_calloc( 1, session_serialized_len );
287 if( session_serialized == NULL )
Hanno Beckeraee87172019-02-06 14:53:19 +0000288 {
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100289 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
290 goto exit;
Paul Bakkere81beda2013-03-06 17:40:46 +0100291 }
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100292
293 /* Now serialize the session into the allocated buffer. */
294 ret = mbedtls_ssl_session_save( session,
295 session_serialized,
296 session_serialized_len,
297 &session_serialized_len );
298 if( ret != 0 )
299 goto exit;
300
Hanno Beckeraee4cc42021-04-15 16:49:32 +0100301 if( session_id_len > sizeof( cur->session_id ) )
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100302 {
303 ret = 1;
304 goto exit;
305 }
306 cur->session_id_len = session_id_len;
307 memcpy( cur->session_id, session_id, session_id_len );
308
309 cur->session = session_serialized;
310 cur->session_len = session_serialized_len;
311 session_serialized = NULL;
Paul Bakker0a597072012-09-25 21:55:46 +0000312
Paul Bakkerc5598842013-09-28 15:01:27 +0200313 ret = 0;
314
315exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_THREADING_C)
317 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200318 ret = 1;
319#endif
320
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100321 if( session_serialized != NULL )
322 mbedtls_platform_zeroize( session_serialized, session_serialized_len );
323
Paul Bakkerc5598842013-09-28 15:01:27 +0200324 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000325}
326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#if defined(MBEDTLS_HAVE_TIME)
328void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
Paul Bakker0a597072012-09-25 21:55:46 +0000329{
330 if( timeout < 0 ) timeout = 0;
331
332 cache->timeout = timeout;
333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000337{
338 if( max < 0 ) max = 0;
339
340 cache->max_entries = max;
341}
342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
Paul Bakker0a597072012-09-25 21:55:46 +0000344{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakker0a597072012-09-25 21:55:46 +0000346
347 cur = cache->chain;
348
349 while( cur != NULL )
350 {
351 prv = cur;
352 cur = cur->next;
353
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100354 mbedtls_free( prv->session );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_free( prv );
Paul Bakker0a597072012-09-25 21:55:46 +0000356 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_THREADING_C)
359 mbedtls_mutex_free( &cache->mutex );
Paul Bakkerc5598842013-09-28 15:01:27 +0200360#endif
Ron Eldor22360822017-10-29 17:53:52 +0200361 cache->chain = NULL;
Paul Bakker0a597072012-09-25 21:55:46 +0000362}
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#endif /* MBEDTLS_SSL_CACHE_C */