blob: 3fdab5b48e545120e06c00f1685a282a973c5cb8 [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
72 if( session_id_len != cur->session.id_len ||
73 memcmp( session_id, cur->session.id,
74 cur->session.id_len ) != 0 )
75 {
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 Beckerf938c432021-04-15 10:17:53 +0100110 ret = mbedtls_ssl_session_copy( session, &entry->session );
111 if( ret != 0 )
112 goto exit;
113
114#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
115 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
116 /*
117 * Restore peer certificate (without rest of the original chain)
118 */
119 if( entry->peer_cert.p != NULL )
Paul Bakker0a597072012-09-25 21:55:46 +0000120 {
Hanno Beckerf938c432021-04-15 10:17:53 +0100121 /* `session->peer_cert` is NULL after the call to
122 * mbedtls_ssl_session_copy(), because cache entries
123 * have the `peer_cert` field set to NULL. */
Paul Bakker0a597072012-09-25 21:55:46 +0000124
Hanno Beckerf938c432021-04-15 10:17:53 +0100125 if( ( session->peer_cert = mbedtls_calloc( 1,
126 sizeof(mbedtls_x509_crt) ) ) == NULL )
Hanno Beckeraee87172019-02-06 14:53:19 +0000127 {
128 ret = 1;
129 goto exit;
130 }
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +0200131
Hanno Beckerf938c432021-04-15 10:17:53 +0100132 mbedtls_x509_crt_init( session->peer_cert );
133 if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p,
134 entry->peer_cert.len ) != 0 )
Paul Bakkere81beda2013-03-06 17:40:46 +0100135 {
Hanno Beckerf938c432021-04-15 10:17:53 +0100136 mbedtls_free( session->peer_cert );
137 session->peer_cert = NULL;
138 ret = 1;
139 goto exit;
Paul Bakkere81beda2013-03-06 17:40:46 +0100140 }
Hanno Beckerf938c432021-04-15 10:17:53 +0100141 }
Hanno Beckera887d1a2019-02-06 15:57:49 +0000142#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkere81beda2013-03-06 17:40:46 +0100143
Hanno Beckerf938c432021-04-15 10:17:53 +0100144 ret = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000145
Paul Bakkerc5598842013-09-28 15:01:27 +0200146exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#if defined(MBEDTLS_THREADING_C)
148 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200149 ret = 1;
150#endif
151
152 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000153}
154
Hanno Becker02a68eb2021-04-15 09:57:17 +0100155static int ssl_cache_find_fresh_entry( mbedtls_ssl_cache_context *cache,
156 unsigned char const *session_id,
157 size_t session_id_len,
158 mbedtls_ssl_cache_entry **dst )
Paul Bakker0a597072012-09-25 21:55:46 +0000159{
Paul Bakkerc5598842013-09-28 15:01:27 +0200160 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_HAVE_TIME)
Simon Butchera55e0842017-07-28 23:46:43 +0100162 mbedtls_time_t t = mbedtls_time( NULL ), oldest = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_ssl_cache_entry *old = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200164#endif
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000165 int count = 0;
Hanno Becker02a68eb2021-04-15 09:57:17 +0100166 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakkerc5598842013-09-28 15:01:27 +0200167
Paul Bakker0a597072012-09-25 21:55:46 +0000168 cur = cache->chain;
169 prv = NULL;
170
171 while( cur != NULL )
172 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000173 count++;
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000176 if( cache->timeout != 0 &&
177 (int) ( t - cur->timestamp ) > cache->timeout )
178 {
179 cur->timestamp = t;
180 break; /* expired, reuse this slot, update timestamp */
181 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200182#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000183
Hanno Beckerccdaf6e2021-04-15 09:26:17 +0100184 if( session_id_len == cur->session.id_len &&
185 memcmp( session_id, cur->session.id, cur->session.id_len ) == 0 )
186 {
Paul Bakker0a597072012-09-25 21:55:46 +0000187 break; /* client reconnected, keep timestamp for session id */
Hanno Beckerccdaf6e2021-04-15 09:26:17 +0100188 }
Paul Bakker0a597072012-09-25 21:55:46 +0000189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000191 if( oldest == 0 || cur->timestamp < oldest )
192 {
193 oldest = cur->timestamp;
194 old = cur;
195 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200196#endif
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000197
Paul Bakker0a597072012-09-25 21:55:46 +0000198 prv = cur;
199 cur = cur->next;
200 }
201
202 if( cur == NULL )
203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000205 /*
206 * Reuse oldest entry if max_entries reached
207 */
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100208 if( count >= cache->max_entries )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000209 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100210 if( old == NULL )
211 {
212 ret = 1;
213 goto exit;
214 }
215
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000216 cur = old;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000217 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#else /* MBEDTLS_HAVE_TIME */
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200219 /*
220 * Reuse first entry in chain if max_entries reached,
221 * but move to last place
222 */
223 if( count >= cache->max_entries )
224 {
225 if( cache->chain == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200226 {
227 ret = 1;
228 goto exit;
229 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200230
231 cur = cache->chain;
232 cache->chain = cur->next;
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100233 cur->next = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200234 prv->next = cur;
235 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000237 else
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000238 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100239 /*
240 * max_entries not reached, create new entry
241 */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200242 cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000243 if( cur == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200244 {
245 ret = 1;
246 goto exit;
247 }
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000248
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000249 if( prv == NULL )
250 cache->chain = cur;
251 else
252 prv->next = cur;
253 }
Paul Bakker0a597072012-09-25 21:55:46 +0000254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000256 cur->timestamp = t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200257#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000258 }
259
Hanno Becker02a68eb2021-04-15 09:57:17 +0100260 if( cur != NULL )
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100261 {
Hanno Becker02a68eb2021-04-15 09:57:17 +0100262 *dst = cur;
263
264 /*
265 * If we're reusing an entry, free its certificate first
266 */
267 if( cur->peer_cert.p != NULL )
268 {
269 mbedtls_free( cur->peer_cert.p );
270 memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) );
271 }
272
273 ret = 0;
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100274 }
Hanno Becker02a68eb2021-04-15 09:57:17 +0100275
276exit:
277
278 return( ret );
279}
280
281int mbedtls_ssl_cache_set( void *data,
282 unsigned char const *session_id,
283 size_t session_id_len,
284 const mbedtls_ssl_session *session )
285{
286 int ret = 1;
287 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
288 mbedtls_ssl_cache_entry *cur;
289
290#if defined(MBEDTLS_THREADING_C)
291 if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
292 return( ret );
293#endif
294
295 ret = ssl_cache_find_fresh_entry( cache,
296 session_id, session_id_len,
297 &cur );
298 if( ret != 0 )
299 goto exit;
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100300
Hanno Beckeraee87172019-02-06 14:53:19 +0000301 /* Copy the entire session; this temporarily makes a copy of the
302 * X.509 CRT structure even though we only want to store the raw CRT.
303 * This inefficiency will go away as soon as we implement on-demand
304 * parsing of CRTs, in which case there's no need for the `peer_cert`
305 * field anymore in the first place, and we're done after this call. */
306 ret = mbedtls_ssl_session_copy( &cur->session, session );
307 if( ret != 0 )
Paul Bakkere81beda2013-03-06 17:40:46 +0100308 {
Hanno Beckeraee87172019-02-06 14:53:19 +0000309 ret = 1;
310 goto exit;
311 }
312
Hanno Beckera887d1a2019-02-06 15:57:49 +0000313#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
314 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckeraee87172019-02-06 14:53:19 +0000315 /* If present, free the X.509 structure and only store the raw CRT data. */
316 if( cur->session.peer_cert != NULL )
317 {
318 cur->peer_cert.p =
319 mbedtls_calloc( 1, cur->session.peer_cert->raw.len );
Paul Bakkere81beda2013-03-06 17:40:46 +0100320 if( cur->peer_cert.p == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200321 {
322 ret = 1;
323 goto exit;
324 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100325
Hanno Beckeraee87172019-02-06 14:53:19 +0000326 memcpy( cur->peer_cert.p,
327 cur->session.peer_cert->raw.p,
328 cur->session.peer_cert->raw.len );
Paul Bakkere81beda2013-03-06 17:40:46 +0100329 cur->peer_cert.len = session->peer_cert->raw.len;
330
Hanno Beckeraee87172019-02-06 14:53:19 +0000331 mbedtls_x509_crt_free( cur->session.peer_cert );
332 mbedtls_free( cur->session.peer_cert );
Paul Bakkere81beda2013-03-06 17:40:46 +0100333 cur->session.peer_cert = NULL;
334 }
Hanno Beckera887d1a2019-02-06 15:57:49 +0000335#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker0a597072012-09-25 21:55:46 +0000336
Paul Bakkerc5598842013-09-28 15:01:27 +0200337 ret = 0;
338
339exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#if defined(MBEDTLS_THREADING_C)
341 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200342 ret = 1;
343#endif
344
345 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000346}
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#if defined(MBEDTLS_HAVE_TIME)
349void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
Paul Bakker0a597072012-09-25 21:55:46 +0000350{
351 if( timeout < 0 ) timeout = 0;
352
353 cache->timeout = timeout;
354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000358{
359 if( max < 0 ) max = 0;
360
361 cache->max_entries = max;
362}
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
Paul Bakker0a597072012-09-25 21:55:46 +0000365{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakker0a597072012-09-25 21:55:46 +0000367
368 cur = cache->chain;
369
370 while( cur != NULL )
371 {
372 prv = cur;
373 cur = cur->next;
374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_ssl_session_free( &prv->session );
Paul Bakkere81beda2013-03-06 17:40:46 +0100376
Hanno Beckera887d1a2019-02-06 15:57:49 +0000377#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
378 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_free( prv->peer_cert.p );
Hanno Beckera887d1a2019-02-06 15:57:49 +0000380#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkere81beda2013-03-06 17:40:46 +0100381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_free( prv );
Paul Bakker0a597072012-09-25 21:55:46 +0000383 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#if defined(MBEDTLS_THREADING_C)
386 mbedtls_mutex_free( &cache->mutex );
Paul Bakkerc5598842013-09-28 15:01:27 +0200387#endif
Ron Eldor22360822017-10-29 17:53:52 +0200388 cache->chain = NULL;
Paul Bakker0a597072012-09-25 21:55:46 +0000389}
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#endif /* MBEDTLS_SSL_CACHE_C */