blob: 1cb71bf575aa859e996bedae0121d398176c38c0 [file] [log] [blame]
Paul Bakker0a597072012-09-25 21:55:46 +00001/*
2 * SSL session cache implementation
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, 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é-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker0a597072012-09-25 21:55:46 +000028#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakker0a597072012-09-25 21:55:46 +000032
33#if defined(POLARSSL_SSL_CACHE_C)
34
35#include "polarssl/ssl_cache.h"
36
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <string.h>
38
Paul Bakker7dc4c442014-02-01 22:50:26 +010039#if defined(POLARSSL_PLATFORM_C)
40#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020041#else
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <stdlib.h>
Paul Bakker6e339b52013-07-03 13:37:05 +020043#define polarssl_malloc malloc
44#define polarssl_free free
45#endif
46
Ron Eldor1f311ed2017-10-17 18:15:41 +030047
48/* Implementation that should never be optimized out by the compiler */
49static void polarssl_zeroize( void *v, size_t n ) {
50 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
51}
52
Paul Bakker0a597072012-09-25 21:55:46 +000053void ssl_cache_init( ssl_cache_context *cache )
54{
55 memset( cache, 0, sizeof( ssl_cache_context ) );
56
57 cache->timeout = SSL_CACHE_DEFAULT_TIMEOUT;
Paul Bakkerba26e9e2012-10-23 22:18:28 +000058 cache->max_entries = SSL_CACHE_DEFAULT_MAX_ENTRIES;
Paul Bakkerc5598842013-09-28 15:01:27 +020059
60#if defined(POLARSSL_THREADING_C)
61 polarssl_mutex_init( &cache->mutex );
62#endif
Paul Bakker0a597072012-09-25 21:55:46 +000063}
64
65int ssl_cache_get( void *data, ssl_session *session )
66{
Paul Bakkerc5598842013-09-28 15:01:27 +020067 int ret = 1;
Paul Bakkerfa9b1002013-07-03 15:31:03 +020068#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000069 time_t t = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +020070#endif
Paul Bakker0a597072012-09-25 21:55:46 +000071 ssl_cache_context *cache = (ssl_cache_context *) data;
72 ssl_cache_entry *cur, *entry;
73
Paul Bakkerc5598842013-09-28 15:01:27 +020074#if defined(POLARSSL_THREADING_C)
75 if( polarssl_mutex_lock( &cache->mutex ) != 0 )
76 return( 1 );
77#endif
78
Paul Bakker0a597072012-09-25 21:55:46 +000079 cur = cache->chain;
80 entry = NULL;
81
82 while( cur != NULL )
83 {
84 entry = cur;
85 cur = cur->next;
86
Paul Bakkerfa9b1002013-07-03 15:31:03 +020087#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000088 if( cache->timeout != 0 &&
89 (int) ( t - entry->timestamp ) > cache->timeout )
90 continue;
Paul Bakkerfa9b1002013-07-03 15:31:03 +020091#endif
Paul Bakker0a597072012-09-25 21:55:46 +000092
93 if( session->ciphersuite != entry->session.ciphersuite ||
94 session->compression != entry->session.compression ||
95 session->length != entry->session.length )
96 continue;
97
98 if( memcmp( session->id, entry->session.id,
99 entry->session.length ) != 0 )
100 continue;
101
102 memcpy( session->master, entry->session.master, 48 );
Paul Bakkere81beda2013-03-06 17:40:46 +0100103
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +0200104 session->verify_result = entry->session.verify_result;
105
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200106#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +0100107 /*
108 * Restore peer certificate (without rest of the original chain)
109 */
110 if( entry->peer_cert.p != NULL )
111 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500112 if( ( session->peer_cert = polarssl_malloc(
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100113 sizeof(x509_crt) ) ) == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200114 {
115 ret = 1;
116 goto exit;
117 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100118
Paul Bakkerb6b09562013-09-18 14:17:41 +0200119 x509_crt_init( session->peer_cert );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200120 if( x509_crt_parse( session->peer_cert, entry->peer_cert.p,
121 entry->peer_cert.len ) != 0 )
Paul Bakkere81beda2013-03-06 17:40:46 +0100122 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200123 polarssl_free( session->peer_cert );
Paul Bakkere81beda2013-03-06 17:40:46 +0100124 session->peer_cert = NULL;
Paul Bakkerc5598842013-09-28 15:01:27 +0200125 ret = 1;
126 goto exit;
Paul Bakkere81beda2013-03-06 17:40:46 +0100127 }
128 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200129#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkere81beda2013-03-06 17:40:46 +0100130
Paul Bakkerc5598842013-09-28 15:01:27 +0200131 ret = 0;
132 goto exit;
Paul Bakker0a597072012-09-25 21:55:46 +0000133 }
134
Paul Bakkerc5598842013-09-28 15:01:27 +0200135exit:
136#if defined(POLARSSL_THREADING_C)
137 if( polarssl_mutex_unlock( &cache->mutex ) != 0 )
138 ret = 1;
139#endif
140
141 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000142}
143
144int ssl_cache_set( void *data, const ssl_session *session )
145{
Paul Bakkerc5598842013-09-28 15:01:27 +0200146 int ret = 1;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200147#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000148 time_t t = time( NULL ), oldest = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200149 ssl_cache_entry *old = NULL;
150#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000151 ssl_cache_context *cache = (ssl_cache_context *) data;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200152 ssl_cache_entry *cur, *prv;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000153 int count = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000154
Paul Bakkerc5598842013-09-28 15:01:27 +0200155#if defined(POLARSSL_THREADING_C)
156 if( ( ret = polarssl_mutex_lock( &cache->mutex ) ) != 0 )
157 return( ret );
158#endif
159
Paul Bakker0a597072012-09-25 21:55:46 +0000160 cur = cache->chain;
161 prv = NULL;
162
163 while( cur != NULL )
164 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000165 count++;
166
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200167#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000168 if( cache->timeout != 0 &&
169 (int) ( t - cur->timestamp ) > cache->timeout )
170 {
171 cur->timestamp = t;
172 break; /* expired, reuse this slot, update timestamp */
173 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200174#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000175
176 if( memcmp( session->id, cur->session.id, cur->session.length ) == 0 )
177 break; /* client reconnected, keep timestamp for session id */
178
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200179#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000180 if( oldest == 0 || cur->timestamp < oldest )
181 {
182 oldest = cur->timestamp;
183 old = cur;
184 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200185#endif
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000186
Paul Bakker0a597072012-09-25 21:55:46 +0000187 prv = cur;
188 cur = cur->next;
189 }
190
191 if( cur == NULL )
192 {
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200193#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000194 /*
195 * Reuse oldest entry if max_entries reached
196 */
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100197 if( count >= cache->max_entries )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000198 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100199 if( old == NULL )
200 {
201 ret = 1;
202 goto exit;
203 }
204
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000205 cur = old;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000206 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200207#else /* POLARSSL_HAVE_TIME */
208 /*
209 * Reuse first entry in chain if max_entries reached,
210 * but move to last place
211 */
212 if( count >= cache->max_entries )
213 {
214 if( cache->chain == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200215 {
216 ret = 1;
217 goto exit;
218 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200219
220 cur = cache->chain;
221 cache->chain = cur->next;
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100222 cur->next = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200223 prv->next = cur;
224 }
225#endif /* POLARSSL_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000226 else
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000227 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100228 /*
229 * max_entries not reached, create new entry
230 */
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500231 cur = polarssl_malloc( sizeof(ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000232 if( cur == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200233 {
234 ret = 1;
235 goto exit;
236 }
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000237
Paul Bakkere81beda2013-03-06 17:40:46 +0100238 memset( cur, 0, sizeof(ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000239
240 if( prv == NULL )
241 cache->chain = cur;
242 else
243 prv->next = cur;
244 }
Paul Bakker0a597072012-09-25 21:55:46 +0000245
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200246#if defined(POLARSSL_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
251 memcpy( &cur->session, session, sizeof( ssl_session ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200252
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200253#if defined(POLARSSL_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 {
259 polarssl_free( cur->peer_cert.p );
260 memset( &cur->peer_cert, 0, sizeof(x509_buf) );
261 }
262
263 /*
Paul Bakkere81beda2013-03-06 17:40:46 +0100264 * Store peer certificate
265 */
266 if( session->peer_cert != NULL )
267 {
Mansour Moufid99b92592015-02-15 17:46:32 -0500268 cur->peer_cert.p = polarssl_malloc( 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 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200281#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker0a597072012-09-25 21:55:46 +0000282
Paul Bakkerc5598842013-09-28 15:01:27 +0200283 ret = 0;
284
285exit:
286#if defined(POLARSSL_THREADING_C)
287 if( polarssl_mutex_unlock( &cache->mutex ) != 0 )
288 ret = 1;
289#endif
290
291 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000292}
293
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200294#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000295void ssl_cache_set_timeout( ssl_cache_context *cache, int timeout )
296{
297 if( timeout < 0 ) timeout = 0;
298
299 cache->timeout = timeout;
300}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200301#endif /* POLARSSL_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000302
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000303void ssl_cache_set_max_entries( ssl_cache_context *cache, int max )
304{
305 if( max < 0 ) max = 0;
306
307 cache->max_entries = max;
308}
309
Paul Bakker0a597072012-09-25 21:55:46 +0000310void ssl_cache_free( ssl_cache_context *cache )
311{
312 ssl_cache_entry *cur, *prv;
313
314 cur = cache->chain;
315
316 while( cur != NULL )
317 {
318 prv = cur;
319 cur = cur->next;
320
321 ssl_session_free( &prv->session );
Paul Bakkere81beda2013-03-06 17:40:46 +0100322
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200323#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker14b16c62014-05-28 11:33:54 +0200324 polarssl_free( prv->peer_cert.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkere81beda2013-03-06 17:40:46 +0100326
Paul Bakker6e339b52013-07-03 13:37:05 +0200327 polarssl_free( prv );
Paul Bakker0a597072012-09-25 21:55:46 +0000328 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200329
330#if defined(POLARSSL_THREADING_C)
331 polarssl_mutex_free( &cache->mutex );
332#endif
Ron Eldor1f311ed2017-10-17 18:15:41 +0300333
334 polarssl_zeroize( cache, sizeof(ssl_cache_context) );
Paul Bakker0a597072012-09-25 21:55:46 +0000335}
336
337#endif /* POLARSSL_SSL_CACHE_C */