blob: e0e21779641ee242357d693763f1672563056052 [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 Beckerccdaf6e2021-04-15 09:26:17 +010053int mbedtls_ssl_cache_get( void *data,
54 unsigned char const *session_id,
55 size_t session_id_len,
56 mbedtls_ssl_session *session )
Paul Bakker0a597072012-09-25 21:55:46 +000057{
Paul Bakkerc5598842013-09-28 15:01:27 +020058 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +010060 mbedtls_time_t t = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +020061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
63 mbedtls_ssl_cache_entry *cur, *entry;
Paul Bakker0a597072012-09-25 21:55:46 +000064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_THREADING_C)
66 if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +020067 return( 1 );
68#endif
69
Paul Bakker0a597072012-09-25 21:55:46 +000070 cur = cache->chain;
71 entry = NULL;
72
73 while( cur != NULL )
74 {
75 entry = cur;
76 cur = cur->next;
77
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000079 if( cache->timeout != 0 &&
80 (int) ( t - entry->timestamp ) > cache->timeout )
81 continue;
Paul Bakkerfa9b1002013-07-03 15:31:03 +020082#endif
Paul Bakker0a597072012-09-25 21:55:46 +000083
Hanno Beckerccdaf6e2021-04-15 09:26:17 +010084 if( session_id_len != entry->session.id_len ||
85 memcmp( session_id, entry->session.id,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +020086 entry->session.id_len ) != 0 )
Hanno Becker64ce9742021-04-15 08:19:40 +010087 {
Paul Bakker0a597072012-09-25 21:55:46 +000088 continue;
Hanno Becker64ce9742021-04-15 08:19:40 +010089 }
Paul Bakker0a597072012-09-25 21:55:46 +000090
Hanno Beckeraee87172019-02-06 14:53:19 +000091 ret = mbedtls_ssl_session_copy( session, &entry->session );
92 if( ret != 0 )
93 {
94 ret = 1;
95 goto exit;
96 }
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +020097
Hanno Beckera887d1a2019-02-06 15:57:49 +000098#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
99 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkere81beda2013-03-06 17:40:46 +0100100 /*
101 * Restore peer certificate (without rest of the original chain)
102 */
103 if( entry->peer_cert.p != NULL )
104 {
Hanno Beckeraee87172019-02-06 14:53:19 +0000105 /* `session->peer_cert` is NULL after the call to
106 * mbedtls_ssl_session_copy(), because cache entries
107 * have the `peer_cert` field set to NULL. */
108
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200109 if( ( session->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 sizeof(mbedtls_x509_crt) ) ) == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200111 {
112 ret = 1;
113 goto exit;
114 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_x509_crt_init( session->peer_cert );
117 if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200118 entry->peer_cert.len ) != 0 )
Paul Bakkere81beda2013-03-06 17:40:46 +0100119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_free( session->peer_cert );
Paul Bakkere81beda2013-03-06 17:40:46 +0100121 session->peer_cert = NULL;
Paul Bakkerc5598842013-09-28 15:01:27 +0200122 ret = 1;
123 goto exit;
Paul Bakkere81beda2013-03-06 17:40:46 +0100124 }
125 }
Hanno Beckera887d1a2019-02-06 15:57:49 +0000126#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkere81beda2013-03-06 17:40:46 +0100127
Paul Bakkerc5598842013-09-28 15:01:27 +0200128 ret = 0;
129 goto exit;
Paul Bakker0a597072012-09-25 21:55:46 +0000130 }
131
Paul Bakkerc5598842013-09-28 15:01:27 +0200132exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#if defined(MBEDTLS_THREADING_C)
134 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200135 ret = 1;
136#endif
137
138 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000139}
140
Hanno Beckerccdaf6e2021-04-15 09:26:17 +0100141int mbedtls_ssl_cache_set( void *data,
142 unsigned char const *session_id,
143 size_t session_id_len,
144 const mbedtls_ssl_session *session )
Paul Bakker0a597072012-09-25 21:55:46 +0000145{
Paul Bakkerc5598842013-09-28 15:01:27 +0200146 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#if defined(MBEDTLS_HAVE_TIME)
Simon Butchera55e0842017-07-28 23:46:43 +0100148 mbedtls_time_t t = mbedtls_time( NULL ), oldest = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_ssl_cache_entry *old = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200150#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
152 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000153 int count = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_THREADING_C)
156 if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200157 return( ret );
158#endif
159
Paul Bakker0a597072012-09-25 21:55:46 +0000160 cur = cache->chain;
161 prv = NULL;
162
163 while( cur != NULL )
164 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000165 count++;
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000168 if( cache->timeout != 0 &&
169 (int) ( t - cur->timestamp ) > cache->timeout )
170 {
171 cur->timestamp = t;
172 break; /* expired, reuse this slot, update timestamp */
173 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200174#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000175
Hanno Beckerccdaf6e2021-04-15 09:26:17 +0100176 if( session_id_len == cur->session.id_len &&
177 memcmp( session_id, cur->session.id, cur->session.id_len ) == 0 )
178 {
Paul Bakker0a597072012-09-25 21:55:46 +0000179 break; /* client reconnected, keep timestamp for session id */
Hanno Beckerccdaf6e2021-04-15 09:26:17 +0100180 }
Paul Bakker0a597072012-09-25 21:55:46 +0000181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000183 if( oldest == 0 || cur->timestamp < oldest )
184 {
185 oldest = cur->timestamp;
186 old = cur;
187 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200188#endif
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000189
Paul Bakker0a597072012-09-25 21:55:46 +0000190 prv = cur;
191 cur = cur->next;
192 }
193
194 if( cur == NULL )
195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000197 /*
198 * Reuse oldest entry if max_entries reached
199 */
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100200 if( count >= cache->max_entries )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000201 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100202 if( old == NULL )
203 {
204 ret = 1;
205 goto exit;
206 }
207
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000208 cur = old;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000209 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#else /* MBEDTLS_HAVE_TIME */
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200211 /*
212 * Reuse first entry in chain if max_entries reached,
213 * but move to last place
214 */
215 if( count >= cache->max_entries )
216 {
217 if( cache->chain == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200218 {
219 ret = 1;
220 goto exit;
221 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200222
223 cur = cache->chain;
224 cache->chain = cur->next;
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100225 cur->next = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200226 prv->next = cur;
227 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000229 else
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000230 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100231 /*
232 * max_entries not reached, create new entry
233 */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200234 cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000235 if( cur == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200236 {
237 ret = 1;
238 goto exit;
239 }
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000240
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000241 if( prv == NULL )
242 cache->chain = cur;
243 else
244 prv->next = cur;
245 }
Paul Bakker0a597072012-09-25 21:55:46 +0000246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000248 cur->timestamp = t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200249#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000250 }
251
Hanno Beckera887d1a2019-02-06 15:57:49 +0000252#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
253 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkere81beda2013-03-06 17:40:46 +0100254 /*
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100255 * If we're reusing an entry, free its certificate first
256 */
257 if( cur->peer_cert.p != NULL )
258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_free( cur->peer_cert.p );
260 memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) );
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100261 }
Hanno Beckera887d1a2019-02-06 15:57:49 +0000262#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100263
Hanno Beckeraee87172019-02-06 14:53:19 +0000264 /* Copy the entire session; this temporarily makes a copy of the
265 * X.509 CRT structure even though we only want to store the raw CRT.
266 * This inefficiency will go away as soon as we implement on-demand
267 * parsing of CRTs, in which case there's no need for the `peer_cert`
268 * field anymore in the first place, and we're done after this call. */
269 ret = mbedtls_ssl_session_copy( &cur->session, session );
270 if( ret != 0 )
Paul Bakkere81beda2013-03-06 17:40:46 +0100271 {
Hanno Beckeraee87172019-02-06 14:53:19 +0000272 ret = 1;
273 goto exit;
274 }
275
Hanno Beckera887d1a2019-02-06 15:57:49 +0000276#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
277 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckeraee87172019-02-06 14:53:19 +0000278 /* If present, free the X.509 structure and only store the raw CRT data. */
279 if( cur->session.peer_cert != NULL )
280 {
281 cur->peer_cert.p =
282 mbedtls_calloc( 1, cur->session.peer_cert->raw.len );
Paul Bakkere81beda2013-03-06 17:40:46 +0100283 if( cur->peer_cert.p == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200284 {
285 ret = 1;
286 goto exit;
287 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100288
Hanno Beckeraee87172019-02-06 14:53:19 +0000289 memcpy( cur->peer_cert.p,
290 cur->session.peer_cert->raw.p,
291 cur->session.peer_cert->raw.len );
Paul Bakkere81beda2013-03-06 17:40:46 +0100292 cur->peer_cert.len = session->peer_cert->raw.len;
293
Hanno Beckeraee87172019-02-06 14:53:19 +0000294 mbedtls_x509_crt_free( cur->session.peer_cert );
295 mbedtls_free( cur->session.peer_cert );
Paul Bakkere81beda2013-03-06 17:40:46 +0100296 cur->session.peer_cert = NULL;
297 }
Hanno Beckera887d1a2019-02-06 15:57:49 +0000298#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker0a597072012-09-25 21:55:46 +0000299
Paul Bakkerc5598842013-09-28 15:01:27 +0200300 ret = 0;
301
302exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#if defined(MBEDTLS_THREADING_C)
304 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200305 ret = 1;
306#endif
307
308 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000309}
310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311#if defined(MBEDTLS_HAVE_TIME)
312void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
Paul Bakker0a597072012-09-25 21:55:46 +0000313{
314 if( timeout < 0 ) timeout = 0;
315
316 cache->timeout = timeout;
317}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000321{
322 if( max < 0 ) max = 0;
323
324 cache->max_entries = max;
325}
326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
Paul Bakker0a597072012-09-25 21:55:46 +0000328{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakker0a597072012-09-25 21:55:46 +0000330
331 cur = cache->chain;
332
333 while( cur != NULL )
334 {
335 prv = cur;
336 cur = cur->next;
337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_ssl_session_free( &prv->session );
Paul Bakkere81beda2013-03-06 17:40:46 +0100339
Hanno Beckera887d1a2019-02-06 15:57:49 +0000340#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
341 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_free( prv->peer_cert.p );
Hanno Beckera887d1a2019-02-06 15:57:49 +0000343#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkere81beda2013-03-06 17:40:46 +0100344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_free( prv );
Paul Bakker0a597072012-09-25 21:55:46 +0000346 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#if defined(MBEDTLS_THREADING_C)
349 mbedtls_mutex_free( &cache->mutex );
Paul Bakkerc5598842013-09-28 15:01:27 +0200350#endif
Ron Eldor22360822017-10-29 17:53:52 +0200351 cache->chain = NULL;
Paul Bakker0a597072012-09-25 21:55:46 +0000352}
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#endif /* MBEDTLS_SSL_CACHE_C */