blob: 47107b6a024de61a49226890272f2ae7a9f499d9 [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é-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/ssl_cache.h"
Paul Bakker0a597072012-09-25 21:55:46 +000035
Rich Evans00ab4702015-02-06 13:43:58 +000036#include <string.h>
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020040#else
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020042#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020044#endif
45
Ron Eldor5bd27262017-10-17 18:15:41 +030046#include "mbedtls/ssl_cache.h"
47
48#include <string.h>
49
50/* Implementation that should never be optimized out by the compiler */
51static void mbedtls_zeroize( void *v, size_t n ) {
52 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
53}
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache )
Paul Bakker0a597072012-09-25 21:55:46 +000056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) );
Paul Bakker0a597072012-09-25 21:55:46 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
60 cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
Paul Bakkerc5598842013-09-28 15:01:27 +020061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_THREADING_C)
63 mbedtls_mutex_init( &cache->mutex );
Paul Bakkerc5598842013-09-28 15:01:27 +020064#endif
Paul Bakker0a597072012-09-25 21:55:46 +000065}
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session )
Paul Bakker0a597072012-09-25 21:55:46 +000068{
Paul Bakkerc5598842013-09-28 15:01:27 +020069 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000071 time_t t = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +020072#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
74 mbedtls_ssl_cache_entry *cur, *entry;
Paul Bakker0a597072012-09-25 21:55:46 +000075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_THREADING_C)
77 if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +020078 return( 1 );
79#endif
80
Paul Bakker0a597072012-09-25 21:55:46 +000081 cur = cache->chain;
82 entry = NULL;
83
84 while( cur != NULL )
85 {
86 entry = cur;
87 cur = cur->next;
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000090 if( cache->timeout != 0 &&
91 (int) ( t - entry->timestamp ) > cache->timeout )
92 continue;
Paul Bakkerfa9b1002013-07-03 15:31:03 +020093#endif
Paul Bakker0a597072012-09-25 21:55:46 +000094
95 if( session->ciphersuite != entry->session.ciphersuite ||
96 session->compression != entry->session.compression ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +020097 session->id_len != entry->session.id_len )
Paul Bakker0a597072012-09-25 21:55:46 +000098 continue;
99
100 if( memcmp( session->id, entry->session.id,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200101 entry->session.id_len ) != 0 )
Paul Bakker0a597072012-09-25 21:55:46 +0000102 continue;
103
104 memcpy( session->master, entry->session.master, 48 );
Paul Bakkere81beda2013-03-06 17:40:46 +0100105
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +0200106 session->verify_result = entry->session.verify_result;
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +0100109 /*
110 * Restore peer certificate (without rest of the original chain)
111 */
112 if( entry->peer_cert.p != NULL )
113 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200114 if( ( session->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 sizeof(mbedtls_x509_crt) ) ) == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200116 {
117 ret = 1;
118 goto exit;
119 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_x509_crt_init( session->peer_cert );
122 if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200123 entry->peer_cert.len ) != 0 )
Paul Bakkere81beda2013-03-06 17:40:46 +0100124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_free( session->peer_cert );
Paul Bakkere81beda2013-03-06 17:40:46 +0100126 session->peer_cert = NULL;
Paul Bakkerc5598842013-09-28 15:01:27 +0200127 ret = 1;
128 goto exit;
Paul Bakkere81beda2013-03-06 17:40:46 +0100129 }
130 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkere81beda2013-03-06 17:40:46 +0100132
Paul Bakkerc5598842013-09-28 15:01:27 +0200133 ret = 0;
134 goto exit;
Paul Bakker0a597072012-09-25 21:55:46 +0000135 }
136
Paul Bakkerc5598842013-09-28 15:01:27 +0200137exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_THREADING_C)
139 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200140 ret = 1;
141#endif
142
143 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000144}
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session )
Paul Bakker0a597072012-09-25 21:55:46 +0000147{
Paul Bakkerc5598842013-09-28 15:01:27 +0200148 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000150 time_t t = time( NULL ), oldest = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_ssl_cache_entry *old = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200152#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
154 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000155 int count = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#if defined(MBEDTLS_THREADING_C)
158 if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200159 return( ret );
160#endif
161
Paul Bakker0a597072012-09-25 21:55:46 +0000162 cur = cache->chain;
163 prv = NULL;
164
165 while( cur != NULL )
166 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000167 count++;
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000170 if( cache->timeout != 0 &&
171 (int) ( t - cur->timestamp ) > cache->timeout )
172 {
173 cur->timestamp = t;
174 break; /* expired, reuse this slot, update timestamp */
175 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200176#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000177
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200178 if( memcmp( session->id, cur->session.id, cur->session.id_len ) == 0 )
Paul Bakker0a597072012-09-25 21:55:46 +0000179 break; /* client reconnected, keep timestamp for session id */
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000182 if( oldest == 0 || cur->timestamp < oldest )
183 {
184 oldest = cur->timestamp;
185 old = cur;
186 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200187#endif
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000188
Paul Bakker0a597072012-09-25 21:55:46 +0000189 prv = cur;
190 cur = cur->next;
191 }
192
193 if( cur == NULL )
194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000196 /*
197 * Reuse oldest entry if max_entries reached
198 */
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100199 if( count >= cache->max_entries )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000200 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100201 if( old == NULL )
202 {
203 ret = 1;
204 goto exit;
205 }
206
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000207 cur = old;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000208 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#else /* MBEDTLS_HAVE_TIME */
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200210 /*
211 * Reuse first entry in chain if max_entries reached,
212 * but move to last place
213 */
214 if( count >= cache->max_entries )
215 {
216 if( cache->chain == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200217 {
218 ret = 1;
219 goto exit;
220 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200221
222 cur = cache->chain;
223 cache->chain = cur->next;
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100224 cur->next = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200225 prv->next = cur;
226 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000228 else
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000229 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100230 /*
231 * max_entries not reached, create new entry
232 */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200233 cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000234 if( cur == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200235 {
236 ret = 1;
237 goto exit;
238 }
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000239
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000240 if( prv == NULL )
241 cache->chain = cur;
242 else
243 prv->next = cur;
244 }
Paul Bakker0a597072012-09-25 21:55:46 +0000245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000247 cur->timestamp = t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200248#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000249 }
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 memcpy( &cur->session, session, sizeof( mbedtls_ssl_session ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_X509_CRT_PARSE_C)
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 }
262
263 /*
Paul Bakkere81beda2013-03-06 17:40:46 +0100264 * Store peer certificate
265 */
266 if( session->peer_cert != NULL )
267 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200268 cur->peer_cert.p = mbedtls_calloc( 1, session->peer_cert->raw.len );
Paul Bakkere81beda2013-03-06 17:40:46 +0100269 if( cur->peer_cert.p == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200270 {
271 ret = 1;
272 goto exit;
273 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100274
275 memcpy( cur->peer_cert.p, session->peer_cert->raw.p,
276 session->peer_cert->raw.len );
277 cur->peer_cert.len = session->peer_cert->raw.len;
278
279 cur->session.peer_cert = NULL;
280 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker0a597072012-09-25 21:55:46 +0000282
Paul Bakkerc5598842013-09-28 15:01:27 +0200283 ret = 0;
284
285exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#if defined(MBEDTLS_THREADING_C)
287 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200288 ret = 1;
289#endif
290
291 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000292}
293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if defined(MBEDTLS_HAVE_TIME)
295void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
Paul Bakker0a597072012-09-25 21:55:46 +0000296{
297 if( timeout < 0 ) timeout = 0;
298
299 cache->timeout = timeout;
300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000304{
305 if( max < 0 ) max = 0;
306
307 cache->max_entries = max;
308}
309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
Paul Bakker0a597072012-09-25 21:55:46 +0000311{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakker0a597072012-09-25 21:55:46 +0000313
314 cur = cache->chain;
315
316 while( cur != NULL )
317 {
318 prv = cur;
319 cur = cur->next;
320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_ssl_session_free( &prv->session );
Paul Bakkere81beda2013-03-06 17:40:46 +0100322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#if defined(MBEDTLS_X509_CRT_PARSE_C)
324 mbedtls_free( prv->peer_cert.p );
325#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkere81beda2013-03-06 17:40:46 +0100326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_free( prv );
Paul Bakker0a597072012-09-25 21:55:46 +0000328 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#if defined(MBEDTLS_THREADING_C)
331 mbedtls_mutex_free( &cache->mutex );
Paul Bakkerc5598842013-09-28 15:01:27 +0200332#endif
Ron Eldor5bd27262017-10-17 18:15:41 +0300333
334 mbedtls_zeroize( cache, sizeof(mbedtls_ssl_cache_context) );
Paul Bakker0a597072012-09-25 21:55:46 +0000335}
336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337#endif /* MBEDTLS_SSL_CACHE_C */