blob: ce85157d2f3e257ccb50ae3a344dd19edb675f3a [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
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
Hanno Becker64ce9742021-04-15 08:19:40 +010081 if( session->id_len != entry->session.id_len ||
82 memcmp( session->id, entry->session.id,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +020083 entry->session.id_len ) != 0 )
Hanno Becker64ce9742021-04-15 08:19:40 +010084 {
Paul Bakker0a597072012-09-25 21:55:46 +000085 continue;
Hanno Becker64ce9742021-04-15 08:19:40 +010086 }
Paul Bakker0a597072012-09-25 21:55:46 +000087
Hanno Beckeraee87172019-02-06 14:53:19 +000088 ret = mbedtls_ssl_session_copy( session, &entry->session );
89 if( ret != 0 )
90 {
91 ret = 1;
92 goto exit;
93 }
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +020094
Hanno Beckera887d1a2019-02-06 15:57:49 +000095#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
96 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkere81beda2013-03-06 17:40:46 +010097 /*
98 * Restore peer certificate (without rest of the original chain)
99 */
100 if( entry->peer_cert.p != NULL )
101 {
Hanno Beckeraee87172019-02-06 14:53:19 +0000102 /* `session->peer_cert` is NULL after the call to
103 * mbedtls_ssl_session_copy(), because cache entries
104 * have the `peer_cert` field set to NULL. */
105
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200106 if( ( session->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 sizeof(mbedtls_x509_crt) ) ) == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200108 {
109 ret = 1;
110 goto exit;
111 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_x509_crt_init( session->peer_cert );
114 if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200115 entry->peer_cert.len ) != 0 )
Paul Bakkere81beda2013-03-06 17:40:46 +0100116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_free( session->peer_cert );
Paul Bakkere81beda2013-03-06 17:40:46 +0100118 session->peer_cert = NULL;
Paul Bakkerc5598842013-09-28 15:01:27 +0200119 ret = 1;
120 goto exit;
Paul Bakkere81beda2013-03-06 17:40:46 +0100121 }
122 }
Hanno Beckera887d1a2019-02-06 15:57:49 +0000123#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkere81beda2013-03-06 17:40:46 +0100124
Paul Bakkerc5598842013-09-28 15:01:27 +0200125 ret = 0;
126 goto exit;
Paul Bakker0a597072012-09-25 21:55:46 +0000127 }
128
Paul Bakkerc5598842013-09-28 15:01:27 +0200129exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if defined(MBEDTLS_THREADING_C)
131 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200132 ret = 1;
133#endif
134
135 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000136}
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session )
Paul Bakker0a597072012-09-25 21:55:46 +0000139{
Paul Bakkerc5598842013-09-28 15:01:27 +0200140 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#if defined(MBEDTLS_HAVE_TIME)
Simon Butchera55e0842017-07-28 23:46:43 +0100142 mbedtls_time_t t = mbedtls_time( NULL ), oldest = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_ssl_cache_entry *old = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200144#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
146 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000147 int count = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if defined(MBEDTLS_THREADING_C)
150 if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200151 return( ret );
152#endif
153
Paul Bakker0a597072012-09-25 21:55:46 +0000154 cur = cache->chain;
155 prv = NULL;
156
157 while( cur != NULL )
158 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000159 count++;
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000162 if( cache->timeout != 0 &&
163 (int) ( t - cur->timestamp ) > cache->timeout )
164 {
165 cur->timestamp = t;
166 break; /* expired, reuse this slot, update timestamp */
167 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200168#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000169
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200170 if( memcmp( session->id, cur->session.id, cur->session.id_len ) == 0 )
Paul Bakker0a597072012-09-25 21:55:46 +0000171 break; /* client reconnected, keep timestamp for session id */
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000174 if( oldest == 0 || cur->timestamp < oldest )
175 {
176 oldest = cur->timestamp;
177 old = cur;
178 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200179#endif
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000180
Paul Bakker0a597072012-09-25 21:55:46 +0000181 prv = cur;
182 cur = cur->next;
183 }
184
185 if( cur == NULL )
186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000188 /*
189 * Reuse oldest entry if max_entries reached
190 */
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100191 if( count >= cache->max_entries )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000192 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100193 if( old == NULL )
194 {
195 ret = 1;
196 goto exit;
197 }
198
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000199 cur = old;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000200 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#else /* MBEDTLS_HAVE_TIME */
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200202 /*
203 * Reuse first entry in chain if max_entries reached,
204 * but move to last place
205 */
206 if( count >= cache->max_entries )
207 {
208 if( cache->chain == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200209 {
210 ret = 1;
211 goto exit;
212 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200213
214 cur = cache->chain;
215 cache->chain = cur->next;
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100216 cur->next = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200217 prv->next = cur;
218 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000220 else
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000221 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100222 /*
223 * max_entries not reached, create new entry
224 */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200225 cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000226 if( cur == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200227 {
228 ret = 1;
229 goto exit;
230 }
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000231
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000232 if( prv == NULL )
233 cache->chain = cur;
234 else
235 prv->next = cur;
236 }
Paul Bakker0a597072012-09-25 21:55:46 +0000237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000239 cur->timestamp = t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200240#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000241 }
242
Hanno Beckera887d1a2019-02-06 15:57:49 +0000243#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
244 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkere81beda2013-03-06 17:40:46 +0100245 /*
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100246 * If we're reusing an entry, free its certificate first
247 */
248 if( cur->peer_cert.p != NULL )
249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_free( cur->peer_cert.p );
251 memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) );
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100252 }
Hanno Beckera887d1a2019-02-06 15:57:49 +0000253#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100254
Hanno Beckeraee87172019-02-06 14:53:19 +0000255 /* Copy the entire session; this temporarily makes a copy of the
256 * X.509 CRT structure even though we only want to store the raw CRT.
257 * This inefficiency will go away as soon as we implement on-demand
258 * parsing of CRTs, in which case there's no need for the `peer_cert`
259 * field anymore in the first place, and we're done after this call. */
260 ret = mbedtls_ssl_session_copy( &cur->session, session );
261 if( ret != 0 )
Paul Bakkere81beda2013-03-06 17:40:46 +0100262 {
Hanno Beckeraee87172019-02-06 14:53:19 +0000263 ret = 1;
264 goto exit;
265 }
266
Hanno Beckera887d1a2019-02-06 15:57:49 +0000267#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
268 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckeraee87172019-02-06 14:53:19 +0000269 /* If present, free the X.509 structure and only store the raw CRT data. */
270 if( cur->session.peer_cert != NULL )
271 {
272 cur->peer_cert.p =
273 mbedtls_calloc( 1, cur->session.peer_cert->raw.len );
Paul Bakkere81beda2013-03-06 17:40:46 +0100274 if( cur->peer_cert.p == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200275 {
276 ret = 1;
277 goto exit;
278 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100279
Hanno Beckeraee87172019-02-06 14:53:19 +0000280 memcpy( cur->peer_cert.p,
281 cur->session.peer_cert->raw.p,
282 cur->session.peer_cert->raw.len );
Paul Bakkere81beda2013-03-06 17:40:46 +0100283 cur->peer_cert.len = session->peer_cert->raw.len;
284
Hanno Beckeraee87172019-02-06 14:53:19 +0000285 mbedtls_x509_crt_free( cur->session.peer_cert );
286 mbedtls_free( cur->session.peer_cert );
Paul Bakkere81beda2013-03-06 17:40:46 +0100287 cur->session.peer_cert = NULL;
288 }
Hanno Beckera887d1a2019-02-06 15:57:49 +0000289#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker0a597072012-09-25 21:55:46 +0000290
Paul Bakkerc5598842013-09-28 15:01:27 +0200291 ret = 0;
292
293exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if defined(MBEDTLS_THREADING_C)
295 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200296 ret = 1;
297#endif
298
299 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000300}
301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#if defined(MBEDTLS_HAVE_TIME)
303void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
Paul Bakker0a597072012-09-25 21:55:46 +0000304{
305 if( timeout < 0 ) timeout = 0;
306
307 cache->timeout = timeout;
308}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000312{
313 if( max < 0 ) max = 0;
314
315 cache->max_entries = max;
316}
317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
Paul Bakker0a597072012-09-25 21:55:46 +0000319{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakker0a597072012-09-25 21:55:46 +0000321
322 cur = cache->chain;
323
324 while( cur != NULL )
325 {
326 prv = cur;
327 cur = cur->next;
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_ssl_session_free( &prv->session );
Paul Bakkere81beda2013-03-06 17:40:46 +0100330
Hanno Beckera887d1a2019-02-06 15:57:49 +0000331#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
332 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 mbedtls_free( prv->peer_cert.p );
Hanno Beckera887d1a2019-02-06 15:57:49 +0000334#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkere81beda2013-03-06 17:40:46 +0100335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_free( prv );
Paul Bakker0a597072012-09-25 21:55:46 +0000337 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#if defined(MBEDTLS_THREADING_C)
340 mbedtls_mutex_free( &cache->mutex );
Paul Bakkerc5598842013-09-28 15:01:27 +0200341#endif
Ron Eldor22360822017-10-29 17:53:52 +0200342 cache->chain = NULL;
Paul Bakker0a597072012-09-25 21:55:46 +0000343}
344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345#endif /* MBEDTLS_SSL_CACHE_C */