blob: ab947bab6e762483a91205e6c095c638f5bfd551 [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
Paul Bakker0a597072012-09-25 21:55:46 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker0a597072012-09-25 21:55:46 +00007 *
Paul Bakker0a597072012-09-25 21:55:46 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * These session callbacks use a simple chained list
24 * to store and retrieve the session information.
25 */
26
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker0a597072012-09-25 21:55:46 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_SSL_CACHE_C)
Paul Bakker0a597072012-09-25 21:55:46 +000034
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/ssl_cache.h"
Paul Bakker0a597072012-09-25 21:55:46 +000036
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <string.h>
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020041#else
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020043#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020045#endif
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache )
Paul Bakker0a597072012-09-25 21:55:46 +000048{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) );
Paul Bakker0a597072012-09-25 21:55:46 +000050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
52 cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
Paul Bakkerc5598842013-09-28 15:01:27 +020053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_THREADING_C)
55 mbedtls_mutex_init( &cache->mutex );
Paul Bakkerc5598842013-09-28 15:01:27 +020056#endif
Paul Bakker0a597072012-09-25 21:55:46 +000057}
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session )
Paul Bakker0a597072012-09-25 21:55:46 +000060{
Paul Bakkerc5598842013-09-28 15:01:27 +020061 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000063 time_t t = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +020064#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
66 mbedtls_ssl_cache_entry *cur, *entry;
Paul Bakker0a597072012-09-25 21:55:46 +000067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_THREADING_C)
69 if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +020070 return( 1 );
71#endif
72
Paul Bakker0a597072012-09-25 21:55:46 +000073 cur = cache->chain;
74 entry = NULL;
75
76 while( cur != NULL )
77 {
78 entry = cur;
79 cur = cur->next;
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000082 if( cache->timeout != 0 &&
83 (int) ( t - entry->timestamp ) > cache->timeout )
84 continue;
Paul Bakkerfa9b1002013-07-03 15:31:03 +020085#endif
Paul Bakker0a597072012-09-25 21:55:46 +000086
87 if( session->ciphersuite != entry->session.ciphersuite ||
88 session->compression != entry->session.compression ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +020089 session->id_len != entry->session.id_len )
Paul Bakker0a597072012-09-25 21:55:46 +000090 continue;
91
92 if( memcmp( session->id, entry->session.id,
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +020093 entry->session.id_len ) != 0 )
Paul Bakker0a597072012-09-25 21:55:46 +000094 continue;
95
96 memcpy( session->master, entry->session.master, 48 );
Paul Bakkere81beda2013-03-06 17:40:46 +010097
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +020098 session->verify_result = entry->session.verify_result;
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +0100101 /*
102 * Restore peer certificate (without rest of the original chain)
103 */
104 if( entry->peer_cert.p != NULL )
105 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200106 if( ( session->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 sizeof(mbedtls_x509_crt) ) ) == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200108 {
109 ret = 1;
110 goto exit;
111 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_x509_crt_init( session->peer_cert );
114 if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200115 entry->peer_cert.len ) != 0 )
Paul Bakkere81beda2013-03-06 17:40:46 +0100116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_free( session->peer_cert );
Paul Bakkere81beda2013-03-06 17:40:46 +0100118 session->peer_cert = NULL;
Paul Bakkerc5598842013-09-28 15:01:27 +0200119 ret = 1;
120 goto exit;
Paul Bakkere81beda2013-03-06 17:40:46 +0100121 }
122 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkere81beda2013-03-06 17:40:46 +0100124
Paul Bakkerc5598842013-09-28 15:01:27 +0200125 ret = 0;
126 goto exit;
Paul Bakker0a597072012-09-25 21:55:46 +0000127 }
128
Paul Bakkerc5598842013-09-28 15:01:27 +0200129exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if defined(MBEDTLS_THREADING_C)
131 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200132 ret = 1;
133#endif
134
135 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000136}
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session )
Paul Bakker0a597072012-09-25 21:55:46 +0000139{
Paul Bakkerc5598842013-09-28 15:01:27 +0200140 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000142 time_t t = time( NULL ), oldest = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_ssl_cache_entry *old = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200144#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
146 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000147 int count = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if defined(MBEDTLS_THREADING_C)
150 if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200151 return( ret );
152#endif
153
Paul Bakker0a597072012-09-25 21:55:46 +0000154 cur = cache->chain;
155 prv = NULL;
156
157 while( cur != NULL )
158 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000159 count++;
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000162 if( cache->timeout != 0 &&
163 (int) ( t - cur->timestamp ) > cache->timeout )
164 {
165 cur->timestamp = t;
166 break; /* expired, reuse this slot, update timestamp */
167 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200168#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000169
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200170 if( memcmp( session->id, cur->session.id, cur->session.id_len ) == 0 )
Paul Bakker0a597072012-09-25 21:55:46 +0000171 break; /* client reconnected, keep timestamp for session id */
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000174 if( oldest == 0 || cur->timestamp < oldest )
175 {
176 oldest = cur->timestamp;
177 old = cur;
178 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200179#endif
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000180
Paul Bakker0a597072012-09-25 21:55:46 +0000181 prv = cur;
182 cur = cur->next;
183 }
184
185 if( cur == NULL )
186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000188 /*
189 * Reuse oldest entry if max_entries reached
190 */
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100191 if( count >= cache->max_entries )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000192 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100193 if( old == NULL )
194 {
195 ret = 1;
196 goto exit;
197 }
198
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000199 cur = old;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000200 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#else /* MBEDTLS_HAVE_TIME */
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200202 /*
203 * Reuse first entry in chain if max_entries reached,
204 * but move to last place
205 */
206 if( count >= cache->max_entries )
207 {
208 if( cache->chain == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200209 {
210 ret = 1;
211 goto exit;
212 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200213
214 cur = cache->chain;
215 cache->chain = cur->next;
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100216 cur->next = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200217 prv->next = cur;
218 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000220 else
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000221 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100222 /*
223 * max_entries not reached, create new entry
224 */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200225 cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000226 if( cur == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200227 {
228 ret = 1;
229 goto exit;
230 }
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000231
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000232 if( prv == NULL )
233 cache->chain = cur;
234 else
235 prv->next = cur;
236 }
Paul Bakker0a597072012-09-25 21:55:46 +0000237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000239 cur->timestamp = t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200240#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000241 }
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 memcpy( &cur->session, session, sizeof( mbedtls_ssl_session ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +0100246 /*
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100247 * If we're reusing an entry, free its certificate first
248 */
249 if( cur->peer_cert.p != NULL )
250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_free( cur->peer_cert.p );
252 memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) );
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100253 }
254
255 /*
Paul Bakkere81beda2013-03-06 17:40:46 +0100256 * Store peer certificate
257 */
258 if( session->peer_cert != NULL )
259 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200260 cur->peer_cert.p = mbedtls_calloc( 1, session->peer_cert->raw.len );
Paul Bakkere81beda2013-03-06 17:40:46 +0100261 if( cur->peer_cert.p == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200262 {
263 ret = 1;
264 goto exit;
265 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100266
267 memcpy( cur->peer_cert.p, session->peer_cert->raw.p,
268 session->peer_cert->raw.len );
269 cur->peer_cert.len = session->peer_cert->raw.len;
270
271 cur->session.peer_cert = NULL;
272 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker0a597072012-09-25 21:55:46 +0000274
Paul Bakkerc5598842013-09-28 15:01:27 +0200275 ret = 0;
276
277exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278#if defined(MBEDTLS_THREADING_C)
279 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
Paul Bakkerc5598842013-09-28 15:01:27 +0200280 ret = 1;
281#endif
282
283 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000284}
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#if defined(MBEDTLS_HAVE_TIME)
287void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
Paul Bakker0a597072012-09-25 21:55:46 +0000288{
289 if( timeout < 0 ) timeout = 0;
290
291 cache->timeout = timeout;
292}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000296{
297 if( max < 0 ) max = 0;
298
299 cache->max_entries = max;
300}
301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
Paul Bakker0a597072012-09-25 21:55:46 +0000303{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakker0a597072012-09-25 21:55:46 +0000305
306 cur = cache->chain;
307
308 while( cur != NULL )
309 {
310 prv = cur;
311 cur = cur->next;
312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_ssl_session_free( &prv->session );
Paul Bakkere81beda2013-03-06 17:40:46 +0100314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#if defined(MBEDTLS_X509_CRT_PARSE_C)
316 mbedtls_free( prv->peer_cert.p );
317#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkere81beda2013-03-06 17:40:46 +0100318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_free( prv );
Paul Bakker0a597072012-09-25 21:55:46 +0000320 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#if defined(MBEDTLS_THREADING_C)
323 mbedtls_mutex_free( &cache->mutex );
Paul Bakkerc5598842013-09-28 15:01:27 +0200324#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000325}
326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#endif /* MBEDTLS_SSL_CACHE_C */