blob: 01c66aed14292c21fa78c27ed1c2ae7dbf07be13 [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
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker0a597072012-09-25 21:55:46 +000020 */
21/*
22 * These session callbacks use a simple chained list
23 * to store and retrieve the session information.
24 */
25
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker0a597072012-09-25 21:55:46 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_SSL_CACHE_C)
Paul Bakker0a597072012-09-25 21:55:46 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020036#else
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020038#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010039#define mbedtls_free free
40#define mbedtls_time time
41#define mbedtls_time_t time_t
Paul Bakker6e339b52013-07-03 13:37:05 +020042#endif
43
SimonBd5800b72016-04-26 07:43:27 +010044#include "mbedtls/ssl_cache.h"
45
46#include <string.h>
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache )
Paul Bakker0a597072012-09-25 21:55:46 +000049{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) );
Paul Bakker0a597072012-09-25 21:55:46 +000051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
53 cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
Paul Bakkerc5598842013-09-28 15:01:27 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_THREADING_C)
56 mbedtls_mutex_init( &cache->mutex );
Paul Bakkerc5598842013-09-28 15:01:27 +020057#endif
Paul Bakker0a597072012-09-25 21:55:46 +000058}
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session )
Paul Bakker0a597072012-09-25 21:55:46 +000061{
Paul Bakkerc5598842013-09-28 15:01:27 +020062 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +010064 mbedtls_time_t t = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +020065#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
67 mbedtls_ssl_cache_entry *cur, *entry;
Paul Bakker0a597072012-09-25 21:55:46 +000068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_THREADING_C)
70 if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +020071 return( 1 );
72#endif
73
Paul Bakker0a597072012-09-25 21:55:46 +000074 cur = cache->chain;
75 entry = NULL;
76
77 while( cur != NULL )
78 {
79 entry = cur;
80 cur = cur->next;
81
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000083 if( cache->timeout != 0 &&
84 (int) ( t - entry->timestamp ) > cache->timeout )
85 continue;
Paul Bakkerfa9b1002013-07-03 15:31:03 +020086#endif
Paul Bakker0a597072012-09-25 21:55:46 +000087
88 if( session->ciphersuite != entry->session.ciphersuite ||
89 session->compression != entry->session.compression ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +020090 session->id_len != entry->session.id_len )
Paul Bakker0a597072012-09-25 21:55:46 +000091 continue;
92
93 if( memcmp( session->id, entry->session.id,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +020094 entry->session.id_len ) != 0 )
Paul Bakker0a597072012-09-25 21:55:46 +000095 continue;
96
97 memcpy( session->master, entry->session.master, 48 );
Paul Bakkere81beda2013-03-06 17:40:46 +010098
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +020099 session->verify_result = entry->session.verify_result;
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +0100102 /*
103 * Restore peer certificate (without rest of the original chain)
104 */
105 if( entry->peer_cert.p != NULL )
106 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200107 if( ( session->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 sizeof(mbedtls_x509_crt) ) ) == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200109 {
110 ret = 1;
111 goto exit;
112 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_x509_crt_init( session->peer_cert );
115 if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200116 entry->peer_cert.len ) != 0 )
Paul Bakkere81beda2013-03-06 17:40:46 +0100117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_free( session->peer_cert );
Paul Bakkere81beda2013-03-06 17:40:46 +0100119 session->peer_cert = NULL;
Paul Bakkerc5598842013-09-28 15:01:27 +0200120 ret = 1;
121 goto exit;
Paul Bakkere81beda2013-03-06 17:40:46 +0100122 }
123 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkere81beda2013-03-06 17:40:46 +0100125
Paul Bakkerc5598842013-09-28 15:01:27 +0200126 ret = 0;
127 goto exit;
Paul Bakker0a597072012-09-25 21:55:46 +0000128 }
129
Paul Bakkerc5598842013-09-28 15:01:27 +0200130exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_THREADING_C)
132 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200133 ret = 1;
134#endif
135
136 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000137}
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session )
Paul Bakker0a597072012-09-25 21:55:46 +0000140{
Paul Bakkerc5598842013-09-28 15:01:27 +0200141 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100143 mbedtls_time_t t = time( NULL ), oldest = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_ssl_cache_entry *old = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200145#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
147 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000148 int count = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#if defined(MBEDTLS_THREADING_C)
151 if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200152 return( ret );
153#endif
154
Paul Bakker0a597072012-09-25 21:55:46 +0000155 cur = cache->chain;
156 prv = NULL;
157
158 while( cur != NULL )
159 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000160 count++;
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000163 if( cache->timeout != 0 &&
164 (int) ( t - cur->timestamp ) > cache->timeout )
165 {
166 cur->timestamp = t;
167 break; /* expired, reuse this slot, update timestamp */
168 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200169#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000170
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200171 if( memcmp( session->id, cur->session.id, cur->session.id_len ) == 0 )
Paul Bakker0a597072012-09-25 21:55:46 +0000172 break; /* client reconnected, keep timestamp for session id */
173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000175 if( oldest == 0 || cur->timestamp < oldest )
176 {
177 oldest = cur->timestamp;
178 old = cur;
179 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200180#endif
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000181
Paul Bakker0a597072012-09-25 21:55:46 +0000182 prv = cur;
183 cur = cur->next;
184 }
185
186 if( cur == NULL )
187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000189 /*
190 * Reuse oldest entry if max_entries reached
191 */
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100192 if( count >= cache->max_entries )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000193 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100194 if( old == NULL )
195 {
196 ret = 1;
197 goto exit;
198 }
199
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000200 cur = old;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000201 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#else /* MBEDTLS_HAVE_TIME */
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200203 /*
204 * Reuse first entry in chain if max_entries reached,
205 * but move to last place
206 */
207 if( count >= cache->max_entries )
208 {
209 if( cache->chain == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200210 {
211 ret = 1;
212 goto exit;
213 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200214
215 cur = cache->chain;
216 cache->chain = cur->next;
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100217 cur->next = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200218 prv->next = cur;
219 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000221 else
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000222 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100223 /*
224 * max_entries not reached, create new entry
225 */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200226 cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000227 if( cur == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200228 {
229 ret = 1;
230 goto exit;
231 }
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000232
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000233 if( prv == NULL )
234 cache->chain = cur;
235 else
236 prv->next = cur;
237 }
Paul Bakker0a597072012-09-25 21:55:46 +0000238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000240 cur->timestamp = t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200241#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000242 }
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 memcpy( &cur->session, session, sizeof( mbedtls_ssl_session ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +0100247 /*
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100248 * If we're reusing an entry, free its certificate first
249 */
250 if( cur->peer_cert.p != NULL )
251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_free( cur->peer_cert.p );
253 memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) );
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100254 }
255
256 /*
Paul Bakkere81beda2013-03-06 17:40:46 +0100257 * Store peer certificate
258 */
259 if( session->peer_cert != NULL )
260 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200261 cur->peer_cert.p = mbedtls_calloc( 1, session->peer_cert->raw.len );
Paul Bakkere81beda2013-03-06 17:40:46 +0100262 if( cur->peer_cert.p == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200263 {
264 ret = 1;
265 goto exit;
266 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100267
268 memcpy( cur->peer_cert.p, session->peer_cert->raw.p,
269 session->peer_cert->raw.len );
270 cur->peer_cert.len = session->peer_cert->raw.len;
271
272 cur->session.peer_cert = NULL;
273 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker0a597072012-09-25 21:55:46 +0000275
Paul Bakkerc5598842013-09-28 15:01:27 +0200276 ret = 0;
277
278exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if defined(MBEDTLS_THREADING_C)
280 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200281 ret = 1;
282#endif
283
284 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000285}
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#if defined(MBEDTLS_HAVE_TIME)
288void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
Paul Bakker0a597072012-09-25 21:55:46 +0000289{
290 if( timeout < 0 ) timeout = 0;
291
292 cache->timeout = timeout;
293}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000297{
298 if( max < 0 ) max = 0;
299
300 cache->max_entries = max;
301}
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
Paul Bakker0a597072012-09-25 21:55:46 +0000304{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakker0a597072012-09-25 21:55:46 +0000306
307 cur = cache->chain;
308
309 while( cur != NULL )
310 {
311 prv = cur;
312 cur = cur->next;
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_ssl_session_free( &prv->session );
Paul Bakkere81beda2013-03-06 17:40:46 +0100315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_X509_CRT_PARSE_C)
317 mbedtls_free( prv->peer_cert.p );
318#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkere81beda2013-03-06 17:40:46 +0100319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 mbedtls_free( prv );
Paul Bakker0a597072012-09-25 21:55:46 +0000321 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#if defined(MBEDTLS_THREADING_C)
324 mbedtls_mutex_free( &cache->mutex );
Paul Bakkerc5598842013-09-28 15:01:27 +0200325#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000326}
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#endif /* MBEDTLS_SSL_CACHE_C */