blob: 7e9d4da05aea87d849812d0f4230a58508f93d3a [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"
Hanno Beckeraee87172019-02-06 14:53:19 +000037#include "mbedtls/ssl_internal.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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session )
Paul Bakker0a597072012-09-25 21:55:46 +000054{
Paul Bakkerc5598842013-09-28 15:01:27 +020055 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +010057 mbedtls_time_t t = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +020058#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
60 mbedtls_ssl_cache_entry *cur, *entry;
Paul Bakker0a597072012-09-25 21:55:46 +000061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_THREADING_C)
63 if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +020064 return( 1 );
65#endif
66
Paul Bakker0a597072012-09-25 21:55:46 +000067 cur = cache->chain;
68 entry = NULL;
69
70 while( cur != NULL )
71 {
72 entry = cur;
73 cur = cur->next;
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000076 if( cache->timeout != 0 &&
77 (int) ( t - entry->timestamp ) > cache->timeout )
78 continue;
Paul Bakkerfa9b1002013-07-03 15:31:03 +020079#endif
Paul Bakker0a597072012-09-25 21:55:46 +000080
81 if( session->ciphersuite != entry->session.ciphersuite ||
82 session->compression != entry->session.compression ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +020083 session->id_len != entry->session.id_len )
Paul Bakker0a597072012-09-25 21:55:46 +000084 continue;
85
86 if( memcmp( session->id, entry->session.id,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +020087 entry->session.id_len ) != 0 )
Paul Bakker0a597072012-09-25 21:55:46 +000088 continue;
89
Hanno Beckeraee87172019-02-06 14:53:19 +000090 ret = mbedtls_ssl_session_copy( session, &entry->session );
91 if( ret != 0 )
92 {
93 ret = 1;
94 goto exit;
95 }
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +020096
Hanno Beckera887d1a2019-02-06 15:57:49 +000097#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
98 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkere81beda2013-03-06 17:40:46 +010099 /*
100 * Restore peer certificate (without rest of the original chain)
101 */
102 if( entry->peer_cert.p != NULL )
103 {
Hanno Beckeraee87172019-02-06 14:53:19 +0000104 /* `session->peer_cert` is NULL after the call to
105 * mbedtls_ssl_session_copy(), because cache entries
106 * have the `peer_cert` field set to NULL. */
107
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200108 if( ( session->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 sizeof(mbedtls_x509_crt) ) ) == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200110 {
111 ret = 1;
112 goto exit;
113 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_x509_crt_init( session->peer_cert );
116 if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200117 entry->peer_cert.len ) != 0 )
Paul Bakkere81beda2013-03-06 17:40:46 +0100118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_free( session->peer_cert );
Paul Bakkere81beda2013-03-06 17:40:46 +0100120 session->peer_cert = NULL;
Paul Bakkerc5598842013-09-28 15:01:27 +0200121 ret = 1;
122 goto exit;
Paul Bakkere81beda2013-03-06 17:40:46 +0100123 }
124 }
Hanno Beckera887d1a2019-02-06 15:57:49 +0000125#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkere81beda2013-03-06 17:40:46 +0100126
Paul Bakkerc5598842013-09-28 15:01:27 +0200127 ret = 0;
128 goto exit;
Paul Bakker0a597072012-09-25 21:55:46 +0000129 }
130
Paul Bakkerc5598842013-09-28 15:01:27 +0200131exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if defined(MBEDTLS_THREADING_C)
133 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200134 ret = 1;
135#endif
136
137 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000138}
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session )
Paul Bakker0a597072012-09-25 21:55:46 +0000141{
Paul Bakkerc5598842013-09-28 15:01:27 +0200142 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_HAVE_TIME)
Simon Butchera55e0842017-07-28 23:46:43 +0100144 mbedtls_time_t t = mbedtls_time( NULL ), oldest = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_ssl_cache_entry *old = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200146#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
148 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000149 int count = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#if defined(MBEDTLS_THREADING_C)
152 if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200153 return( ret );
154#endif
155
Paul Bakker0a597072012-09-25 21:55:46 +0000156 cur = cache->chain;
157 prv = NULL;
158
159 while( cur != NULL )
160 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000161 count++;
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000164 if( cache->timeout != 0 &&
165 (int) ( t - cur->timestamp ) > cache->timeout )
166 {
167 cur->timestamp = t;
168 break; /* expired, reuse this slot, update timestamp */
169 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200170#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000171
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200172 if( memcmp( session->id, cur->session.id, cur->session.id_len ) == 0 )
Paul Bakker0a597072012-09-25 21:55:46 +0000173 break; /* client reconnected, keep timestamp for session id */
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000176 if( oldest == 0 || cur->timestamp < oldest )
177 {
178 oldest = cur->timestamp;
179 old = cur;
180 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200181#endif
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000182
Paul Bakker0a597072012-09-25 21:55:46 +0000183 prv = cur;
184 cur = cur->next;
185 }
186
187 if( cur == NULL )
188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000190 /*
191 * Reuse oldest entry if max_entries reached
192 */
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100193 if( count >= cache->max_entries )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000194 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100195 if( old == NULL )
196 {
197 ret = 1;
198 goto exit;
199 }
200
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000201 cur = old;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000202 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#else /* MBEDTLS_HAVE_TIME */
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200204 /*
205 * Reuse first entry in chain if max_entries reached,
206 * but move to last place
207 */
208 if( count >= cache->max_entries )
209 {
210 if( cache->chain == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200211 {
212 ret = 1;
213 goto exit;
214 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200215
216 cur = cache->chain;
217 cache->chain = cur->next;
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100218 cur->next = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200219 prv->next = cur;
220 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000222 else
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000223 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100224 /*
225 * max_entries not reached, create new entry
226 */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200227 cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000228 if( cur == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200229 {
230 ret = 1;
231 goto exit;
232 }
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000233
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000234 if( prv == NULL )
235 cache->chain = cur;
236 else
237 prv->next = cur;
238 }
Paul Bakker0a597072012-09-25 21:55:46 +0000239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000241 cur->timestamp = t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200242#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000243 }
244
Hanno Beckera887d1a2019-02-06 15:57:49 +0000245#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
246 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkere81beda2013-03-06 17:40:46 +0100247 /*
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100248 * If we're reusing an entry, free its certificate first
249 */
250 if( cur->peer_cert.p != NULL )
251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_free( cur->peer_cert.p );
253 memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) );
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100254 }
Hanno Beckera887d1a2019-02-06 15:57:49 +0000255#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100256
Hanno Beckeraee87172019-02-06 14:53:19 +0000257 /* Copy the entire session; this temporarily makes a copy of the
258 * X.509 CRT structure even though we only want to store the raw CRT.
259 * This inefficiency will go away as soon as we implement on-demand
260 * parsing of CRTs, in which case there's no need for the `peer_cert`
261 * field anymore in the first place, and we're done after this call. */
262 ret = mbedtls_ssl_session_copy( &cur->session, session );
263 if( ret != 0 )
Paul Bakkere81beda2013-03-06 17:40:46 +0100264 {
Hanno Beckeraee87172019-02-06 14:53:19 +0000265 ret = 1;
266 goto exit;
267 }
268
Hanno Beckera887d1a2019-02-06 15:57:49 +0000269#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
270 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckeraee87172019-02-06 14:53:19 +0000271 /* If present, free the X.509 structure and only store the raw CRT data. */
272 if( cur->session.peer_cert != NULL )
273 {
274 cur->peer_cert.p =
275 mbedtls_calloc( 1, cur->session.peer_cert->raw.len );
Paul Bakkere81beda2013-03-06 17:40:46 +0100276 if( cur->peer_cert.p == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200277 {
278 ret = 1;
279 goto exit;
280 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100281
Hanno Beckeraee87172019-02-06 14:53:19 +0000282 memcpy( cur->peer_cert.p,
283 cur->session.peer_cert->raw.p,
284 cur->session.peer_cert->raw.len );
Paul Bakkere81beda2013-03-06 17:40:46 +0100285 cur->peer_cert.len = session->peer_cert->raw.len;
286
Hanno Beckeraee87172019-02-06 14:53:19 +0000287 mbedtls_x509_crt_free( cur->session.peer_cert );
288 mbedtls_free( cur->session.peer_cert );
Paul Bakkere81beda2013-03-06 17:40:46 +0100289 cur->session.peer_cert = NULL;
290 }
Hanno Beckera887d1a2019-02-06 15:57:49 +0000291#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker0a597072012-09-25 21:55:46 +0000292
Paul Bakkerc5598842013-09-28 15:01:27 +0200293 ret = 0;
294
295exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#if defined(MBEDTLS_THREADING_C)
297 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200298 ret = 1;
299#endif
300
301 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000302}
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_HAVE_TIME)
305void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
Paul Bakker0a597072012-09-25 21:55:46 +0000306{
307 if( timeout < 0 ) timeout = 0;
308
309 cache->timeout = timeout;
310}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000314{
315 if( max < 0 ) max = 0;
316
317 cache->max_entries = max;
318}
319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
Paul Bakker0a597072012-09-25 21:55:46 +0000321{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakker0a597072012-09-25 21:55:46 +0000323
324 cur = cache->chain;
325
326 while( cur != NULL )
327 {
328 prv = cur;
329 cur = cur->next;
330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_ssl_session_free( &prv->session );
Paul Bakkere81beda2013-03-06 17:40:46 +0100332
Hanno Beckera887d1a2019-02-06 15:57:49 +0000333#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
334 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_free( prv->peer_cert.p );
Hanno Beckera887d1a2019-02-06 15:57:49 +0000336#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkere81beda2013-03-06 17:40:46 +0100337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_free( prv );
Paul Bakker0a597072012-09-25 21:55:46 +0000339 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341#if defined(MBEDTLS_THREADING_C)
342 mbedtls_mutex_free( &cache->mutex );
Paul Bakkerc5598842013-09-28 15:01:27 +0200343#endif
Ron Eldor22360822017-10-29 17:53:52 +0200344 cache->chain = NULL;
Paul Bakker0a597072012-09-25 21:55:46 +0000345}
346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#endif /* MBEDTLS_SSL_CACHE_C */