blob: 7a600cad1808385bab9d88fff5849af01337423f [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é-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020029
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/ssl_cache.h"
Hanno Beckeraee87172019-02-06 14:53:19 +000031#include "mbedtls/ssl_internal.h"
SimonBd5800b72016-04-26 07:43:27 +010032
33#include <string.h>
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache )
Paul Bakker0a597072012-09-25 21:55:46 +000036{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037 memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) );
Paul Bakker0a597072012-09-25 21:55:46 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
40 cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
Paul Bakkerc5598842013-09-28 15:01:27 +020041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_THREADING_C)
43 mbedtls_mutex_init( &cache->mutex );
Paul Bakkerc5598842013-09-28 15:01:27 +020044#endif
Paul Bakker0a597072012-09-25 21:55:46 +000045}
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session )
Paul Bakker0a597072012-09-25 21:55:46 +000048{
Paul Bakkerc5598842013-09-28 15:01:27 +020049 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +010051 mbedtls_time_t t = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +020052#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
54 mbedtls_ssl_cache_entry *cur, *entry;
Paul Bakker0a597072012-09-25 21:55:46 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_THREADING_C)
57 if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +020058 return( 1 );
59#endif
60
Paul Bakker0a597072012-09-25 21:55:46 +000061 cur = cache->chain;
62 entry = NULL;
63
64 while( cur != NULL )
65 {
66 entry = cur;
67 cur = cur->next;
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000070 if( cache->timeout != 0 &&
71 (int) ( t - entry->timestamp ) > cache->timeout )
72 continue;
Paul Bakkerfa9b1002013-07-03 15:31:03 +020073#endif
Paul Bakker0a597072012-09-25 21:55:46 +000074
Hanno Becker83e36712021-04-15 08:19:40 +010075 if( session->id_len != entry->session.id_len ||
76 memcmp( session->id, entry->session.id,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +020077 entry->session.id_len ) != 0 )
Hanno Becker83e36712021-04-15 08:19:40 +010078 {
Paul Bakker0a597072012-09-25 21:55:46 +000079 continue;
Hanno Becker83e36712021-04-15 08:19:40 +010080 }
Paul Bakker0a597072012-09-25 21:55:46 +000081
Hanno Beckeraee87172019-02-06 14:53:19 +000082 ret = mbedtls_ssl_session_copy( session, &entry->session );
83 if( ret != 0 )
84 {
85 ret = 1;
86 goto exit;
87 }
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +020088
Hanno Beckera887d1a2019-02-06 15:57:49 +000089#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
90 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkere81beda2013-03-06 17:40:46 +010091 /*
92 * Restore peer certificate (without rest of the original chain)
93 */
94 if( entry->peer_cert.p != NULL )
95 {
Hanno Beckeraee87172019-02-06 14:53:19 +000096 /* `session->peer_cert` is NULL after the call to
97 * mbedtls_ssl_session_copy(), because cache entries
98 * have the `peer_cert` field set to NULL. */
99
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200100 if( ( session->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 sizeof(mbedtls_x509_crt) ) ) == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200102 {
103 ret = 1;
104 goto exit;
105 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_x509_crt_init( session->peer_cert );
108 if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200109 entry->peer_cert.len ) != 0 )
Paul Bakkere81beda2013-03-06 17:40:46 +0100110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_free( session->peer_cert );
Paul Bakkere81beda2013-03-06 17:40:46 +0100112 session->peer_cert = NULL;
Paul Bakkerc5598842013-09-28 15:01:27 +0200113 ret = 1;
114 goto exit;
Paul Bakkere81beda2013-03-06 17:40:46 +0100115 }
116 }
Hanno Beckera887d1a2019-02-06 15:57:49 +0000117#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkere81beda2013-03-06 17:40:46 +0100118
Paul Bakkerc5598842013-09-28 15:01:27 +0200119 ret = 0;
120 goto exit;
Paul Bakker0a597072012-09-25 21:55:46 +0000121 }
122
Paul Bakkerc5598842013-09-28 15:01:27 +0200123exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#if defined(MBEDTLS_THREADING_C)
125 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200126 ret = 1;
127#endif
128
129 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000130}
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session )
Paul Bakker0a597072012-09-25 21:55:46 +0000133{
Paul Bakkerc5598842013-09-28 15:01:27 +0200134 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#if defined(MBEDTLS_HAVE_TIME)
Simon Butchera55e0842017-07-28 23:46:43 +0100136 mbedtls_time_t t = mbedtls_time( NULL ), oldest = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_ssl_cache_entry *old = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200138#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
140 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000141 int count = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_THREADING_C)
144 if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200145 return( ret );
146#endif
147
Paul Bakker0a597072012-09-25 21:55:46 +0000148 cur = cache->chain;
149 prv = NULL;
150
151 while( cur != NULL )
152 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000153 count++;
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000156 if( cache->timeout != 0 &&
157 (int) ( t - cur->timestamp ) > cache->timeout )
158 {
159 cur->timestamp = t;
160 break; /* expired, reuse this slot, update timestamp */
161 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200162#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000163
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200164 if( memcmp( session->id, cur->session.id, cur->session.id_len ) == 0 )
Paul Bakker0a597072012-09-25 21:55:46 +0000165 break; /* client reconnected, keep timestamp for session id */
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000168 if( oldest == 0 || cur->timestamp < oldest )
169 {
170 oldest = cur->timestamp;
171 old = cur;
172 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200173#endif
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000174
Paul Bakker0a597072012-09-25 21:55:46 +0000175 prv = cur;
176 cur = cur->next;
177 }
178
179 if( cur == NULL )
180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000182 /*
183 * Reuse oldest entry if max_entries reached
184 */
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100185 if( count >= cache->max_entries )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000186 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100187 if( old == NULL )
188 {
189 ret = 1;
190 goto exit;
191 }
192
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000193 cur = old;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000194 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#else /* MBEDTLS_HAVE_TIME */
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200196 /*
197 * Reuse first entry in chain if max_entries reached,
198 * but move to last place
199 */
200 if( count >= cache->max_entries )
201 {
202 if( cache->chain == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200203 {
204 ret = 1;
205 goto exit;
206 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200207
208 cur = cache->chain;
209 cache->chain = cur->next;
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100210 cur->next = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200211 prv->next = cur;
212 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000214 else
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000215 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100216 /*
217 * max_entries not reached, create new entry
218 */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200219 cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000220 if( cur == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200221 {
222 ret = 1;
223 goto exit;
224 }
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000225
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000226 if( prv == NULL )
227 cache->chain = cur;
228 else
229 prv->next = cur;
230 }
Paul Bakker0a597072012-09-25 21:55:46 +0000231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000233 cur->timestamp = t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200234#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000235 }
236
Hanno Beckera887d1a2019-02-06 15:57:49 +0000237#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
238 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkere81beda2013-03-06 17:40:46 +0100239 /*
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100240 * If we're reusing an entry, free its certificate first
241 */
242 if( cur->peer_cert.p != NULL )
243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_free( cur->peer_cert.p );
245 memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) );
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100246 }
Hanno Beckera887d1a2019-02-06 15:57:49 +0000247#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100248
Hanno Beckeraee87172019-02-06 14:53:19 +0000249 /* Copy the entire session; this temporarily makes a copy of the
250 * X.509 CRT structure even though we only want to store the raw CRT.
251 * This inefficiency will go away as soon as we implement on-demand
252 * parsing of CRTs, in which case there's no need for the `peer_cert`
253 * field anymore in the first place, and we're done after this call. */
254 ret = mbedtls_ssl_session_copy( &cur->session, session );
255 if( ret != 0 )
Paul Bakkere81beda2013-03-06 17:40:46 +0100256 {
Hanno Beckeraee87172019-02-06 14:53:19 +0000257 ret = 1;
258 goto exit;
259 }
260
Hanno Beckera887d1a2019-02-06 15:57:49 +0000261#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
262 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckeraee87172019-02-06 14:53:19 +0000263 /* If present, free the X.509 structure and only store the raw CRT data. */
264 if( cur->session.peer_cert != NULL )
265 {
266 cur->peer_cert.p =
267 mbedtls_calloc( 1, cur->session.peer_cert->raw.len );
Paul Bakkere81beda2013-03-06 17:40:46 +0100268 if( cur->peer_cert.p == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200269 {
270 ret = 1;
271 goto exit;
272 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100273
Hanno Beckeraee87172019-02-06 14:53:19 +0000274 memcpy( cur->peer_cert.p,
275 cur->session.peer_cert->raw.p,
276 cur->session.peer_cert->raw.len );
Paul Bakkere81beda2013-03-06 17:40:46 +0100277 cur->peer_cert.len = session->peer_cert->raw.len;
278
Hanno Beckeraee87172019-02-06 14:53:19 +0000279 mbedtls_x509_crt_free( cur->session.peer_cert );
280 mbedtls_free( cur->session.peer_cert );
Paul Bakkere81beda2013-03-06 17:40:46 +0100281 cur->session.peer_cert = NULL;
282 }
Hanno Beckera887d1a2019-02-06 15:57:49 +0000283#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker0a597072012-09-25 21:55:46 +0000284
Paul Bakkerc5598842013-09-28 15:01:27 +0200285 ret = 0;
286
287exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#if defined(MBEDTLS_THREADING_C)
289 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200290 ret = 1;
291#endif
292
293 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000294}
295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#if defined(MBEDTLS_HAVE_TIME)
297void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
Paul Bakker0a597072012-09-25 21:55:46 +0000298{
299 if( timeout < 0 ) timeout = 0;
300
301 cache->timeout = timeout;
302}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000306{
307 if( max < 0 ) max = 0;
308
309 cache->max_entries = max;
310}
311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
Paul Bakker0a597072012-09-25 21:55:46 +0000313{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakker0a597072012-09-25 21:55:46 +0000315
316 cur = cache->chain;
317
318 while( cur != NULL )
319 {
320 prv = cur;
321 cur = cur->next;
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_ssl_session_free( &prv->session );
Paul Bakkere81beda2013-03-06 17:40:46 +0100324
Hanno Beckera887d1a2019-02-06 15:57:49 +0000325#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
326 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_free( prv->peer_cert.p );
Hanno Beckera887d1a2019-02-06 15:57:49 +0000328#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkere81beda2013-03-06 17:40:46 +0100329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_free( prv );
Paul Bakker0a597072012-09-25 21:55:46 +0000331 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#if defined(MBEDTLS_THREADING_C)
334 mbedtls_mutex_free( &cache->mutex );
Paul Bakkerc5598842013-09-28 15:01:27 +0200335#endif
Ron Eldor22360822017-10-29 17:53:52 +0200336 cache->chain = NULL;
Paul Bakker0a597072012-09-25 21:55:46 +0000337}
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#endif /* MBEDTLS_SSL_CACHE_C */