blob: 3cbfeb740a8a4b6573a8cfabd695f73f09aabf45 [file] [log] [blame]
Paul Bakker0a597072012-09-25 21:55:46 +00001/*
2 * SSL session cache implementation
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker0a597072012-09-25 21:55:46 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker0a597072012-09-25 21:55:46 +000047 */
48/*
49 * These session callbacks use a simple chained list
50 * to store and retrieve the session information.
51 */
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020055#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#endif
Paul Bakker0a597072012-09-25 21:55:46 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_SSL_CACHE_C)
Paul Bakker0a597072012-09-25 21:55:46 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020063#else
Rich Evans00ab4702015-02-06 13:43:58 +000064#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020065#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010066#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020067#endif
68
SimonBd5800b72016-04-26 07:43:27 +010069#include "mbedtls/ssl_cache.h"
70
71#include <string.h>
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache )
Paul Bakker0a597072012-09-25 21:55:46 +000074{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) );
Paul Bakker0a597072012-09-25 21:55:46 +000076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
78 cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
Paul Bakkerc5598842013-09-28 15:01:27 +020079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if defined(MBEDTLS_THREADING_C)
81 mbedtls_mutex_init( &cache->mutex );
Paul Bakkerc5598842013-09-28 15:01:27 +020082#endif
Paul Bakker0a597072012-09-25 21:55:46 +000083}
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session )
Paul Bakker0a597072012-09-25 21:55:46 +000086{
Paul Bakkerc5598842013-09-28 15:01:27 +020087 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +010089 mbedtls_time_t t = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +020090#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
92 mbedtls_ssl_cache_entry *cur, *entry;
Paul Bakker0a597072012-09-25 21:55:46 +000093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if defined(MBEDTLS_THREADING_C)
95 if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +020096 return( 1 );
97#endif
98
Paul Bakker0a597072012-09-25 21:55:46 +000099 cur = cache->chain;
100 entry = NULL;
101
102 while( cur != NULL )
103 {
104 entry = cur;
105 cur = cur->next;
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000108 if( cache->timeout != 0 &&
109 (int) ( t - entry->timestamp ) > cache->timeout )
110 continue;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200111#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000112
113 if( session->ciphersuite != entry->session.ciphersuite ||
114 session->compression != entry->session.compression ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200115 session->id_len != entry->session.id_len )
Paul Bakker0a597072012-09-25 21:55:46 +0000116 continue;
117
118 if( memcmp( session->id, entry->session.id,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200119 entry->session.id_len ) != 0 )
Paul Bakker0a597072012-09-25 21:55:46 +0000120 continue;
121
122 memcpy( session->master, entry->session.master, 48 );
Paul Bakkere81beda2013-03-06 17:40:46 +0100123
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +0200124 session->verify_result = entry->session.verify_result;
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +0100127 /*
128 * Restore peer certificate (without rest of the original chain)
129 */
130 if( entry->peer_cert.p != NULL )
131 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200132 if( ( session->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 sizeof(mbedtls_x509_crt) ) ) == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200134 {
135 ret = 1;
136 goto exit;
137 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_x509_crt_init( session->peer_cert );
140 if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200141 entry->peer_cert.len ) != 0 )
Paul Bakkere81beda2013-03-06 17:40:46 +0100142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_free( session->peer_cert );
Paul Bakkere81beda2013-03-06 17:40:46 +0100144 session->peer_cert = NULL;
Paul Bakkerc5598842013-09-28 15:01:27 +0200145 ret = 1;
146 goto exit;
Paul Bakkere81beda2013-03-06 17:40:46 +0100147 }
148 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkere81beda2013-03-06 17:40:46 +0100150
Paul Bakkerc5598842013-09-28 15:01:27 +0200151 ret = 0;
152 goto exit;
Paul Bakker0a597072012-09-25 21:55:46 +0000153 }
154
Paul Bakkerc5598842013-09-28 15:01:27 +0200155exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_THREADING_C)
157 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200158 ret = 1;
159#endif
160
161 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000162}
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session )
Paul Bakker0a597072012-09-25 21:55:46 +0000165{
Paul Bakkerc5598842013-09-28 15:01:27 +0200166 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_HAVE_TIME)
Simon Butchera55e0842017-07-28 23:46:43 +0100168 mbedtls_time_t t = mbedtls_time( NULL ), oldest = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_ssl_cache_entry *old = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200170#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
172 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000173 int count = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#if defined(MBEDTLS_THREADING_C)
176 if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200177 return( ret );
178#endif
179
Paul Bakker0a597072012-09-25 21:55:46 +0000180 cur = cache->chain;
181 prv = NULL;
182
183 while( cur != NULL )
184 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000185 count++;
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000188 if( cache->timeout != 0 &&
189 (int) ( t - cur->timestamp ) > cache->timeout )
190 {
191 cur->timestamp = t;
192 break; /* expired, reuse this slot, update timestamp */
193 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200194#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000195
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200196 if( memcmp( session->id, cur->session.id, cur->session.id_len ) == 0 )
Paul Bakker0a597072012-09-25 21:55:46 +0000197 break; /* client reconnected, keep timestamp for session id */
198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000200 if( oldest == 0 || cur->timestamp < oldest )
201 {
202 oldest = cur->timestamp;
203 old = cur;
204 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200205#endif
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000206
Paul Bakker0a597072012-09-25 21:55:46 +0000207 prv = cur;
208 cur = cur->next;
209 }
210
211 if( cur == NULL )
212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000214 /*
215 * Reuse oldest entry if max_entries reached
216 */
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100217 if( count >= cache->max_entries )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000218 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100219 if( old == NULL )
220 {
221 ret = 1;
222 goto exit;
223 }
224
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000225 cur = old;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000226 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#else /* MBEDTLS_HAVE_TIME */
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200228 /*
229 * Reuse first entry in chain if max_entries reached,
230 * but move to last place
231 */
232 if( count >= cache->max_entries )
233 {
234 if( cache->chain == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200235 {
236 ret = 1;
237 goto exit;
238 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200239
240 cur = cache->chain;
241 cache->chain = cur->next;
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100242 cur->next = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200243 prv->next = cur;
244 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000246 else
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000247 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100248 /*
249 * max_entries not reached, create new entry
250 */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200251 cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000252 if( cur == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200253 {
254 ret = 1;
255 goto exit;
256 }
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000257
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000258 if( prv == NULL )
259 cache->chain = cur;
260 else
261 prv->next = cur;
262 }
Paul Bakker0a597072012-09-25 21:55:46 +0000263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000265 cur->timestamp = t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200266#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000267 }
268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 memcpy( &cur->session, session, sizeof( mbedtls_ssl_session ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +0100272 /*
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100273 * If we're reusing an entry, free its certificate first
274 */
275 if( cur->peer_cert.p != NULL )
276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_free( cur->peer_cert.p );
278 memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) );
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100279 }
280
281 /*
Paul Bakkere81beda2013-03-06 17:40:46 +0100282 * Store peer certificate
283 */
284 if( session->peer_cert != NULL )
285 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200286 cur->peer_cert.p = mbedtls_calloc( 1, session->peer_cert->raw.len );
Paul Bakkere81beda2013-03-06 17:40:46 +0100287 if( cur->peer_cert.p == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200288 {
289 ret = 1;
290 goto exit;
291 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100292
293 memcpy( cur->peer_cert.p, session->peer_cert->raw.p,
294 session->peer_cert->raw.len );
295 cur->peer_cert.len = session->peer_cert->raw.len;
296
297 cur->session.peer_cert = NULL;
298 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker0a597072012-09-25 21:55:46 +0000300
Paul Bakkerc5598842013-09-28 15:01:27 +0200301 ret = 0;
302
303exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_THREADING_C)
305 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200306 ret = 1;
307#endif
308
309 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000310}
311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312#if defined(MBEDTLS_HAVE_TIME)
313void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
Paul Bakker0a597072012-09-25 21:55:46 +0000314{
315 if( timeout < 0 ) timeout = 0;
316
317 cache->timeout = timeout;
318}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000322{
323 if( max < 0 ) max = 0;
324
325 cache->max_entries = max;
326}
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
Paul Bakker0a597072012-09-25 21:55:46 +0000329{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakker0a597072012-09-25 21:55:46 +0000331
332 cur = cache->chain;
333
334 while( cur != NULL )
335 {
336 prv = cur;
337 cur = cur->next;
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 mbedtls_ssl_session_free( &prv->session );
Paul Bakkere81beda2013-03-06 17:40:46 +0100340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341#if defined(MBEDTLS_X509_CRT_PARSE_C)
342 mbedtls_free( prv->peer_cert.p );
343#endif /* MBEDTLS_X509_CRT_PARSE_C */
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 */