blob: f58a524f0ebc1584a7d7d93eac568b63a063cd6d [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.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
Paul Bakker7dc4c442014-02-01 22:50:26 +010037#if defined(POLARSSL_PLATFORM_C)
38#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020039#else
40#define polarssl_malloc malloc
41#define polarssl_free free
42#endif
43
Paul Bakker0a597072012-09-25 21:55:46 +000044#include <stdlib.h>
45
46void ssl_cache_init( ssl_cache_context *cache )
47{
48 memset( cache, 0, sizeof( ssl_cache_context ) );
49
50 cache->timeout = SSL_CACHE_DEFAULT_TIMEOUT;
Paul Bakkerba26e9e2012-10-23 22:18:28 +000051 cache->max_entries = SSL_CACHE_DEFAULT_MAX_ENTRIES;
Paul Bakkerc5598842013-09-28 15:01:27 +020052
53#if defined(POLARSSL_THREADING_C)
54 polarssl_mutex_init( &cache->mutex );
55#endif
Paul Bakker0a597072012-09-25 21:55:46 +000056}
57
58int ssl_cache_get( void *data, ssl_session *session )
59{
Paul Bakkerc5598842013-09-28 15:01:27 +020060 int ret = 1;
Paul Bakkerfa9b1002013-07-03 15:31:03 +020061#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000062 time_t t = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +020063#endif
Paul Bakker0a597072012-09-25 21:55:46 +000064 ssl_cache_context *cache = (ssl_cache_context *) data;
65 ssl_cache_entry *cur, *entry;
66
Paul Bakkerc5598842013-09-28 15:01:27 +020067#if defined(POLARSSL_THREADING_C)
68 if( polarssl_mutex_lock( &cache->mutex ) != 0 )
69 return( 1 );
70#endif
71
Paul Bakker0a597072012-09-25 21:55:46 +000072 cur = cache->chain;
73 entry = NULL;
74
75 while( cur != NULL )
76 {
77 entry = cur;
78 cur = cur->next;
79
Paul Bakkerfa9b1002013-07-03 15:31:03 +020080#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000081 if( cache->timeout != 0 &&
82 (int) ( t - entry->timestamp ) > cache->timeout )
83 continue;
Paul Bakkerfa9b1002013-07-03 15:31:03 +020084#endif
Paul Bakker0a597072012-09-25 21:55:46 +000085
86 if( session->ciphersuite != entry->session.ciphersuite ||
87 session->compression != entry->session.compression ||
88 session->length != entry->session.length )
89 continue;
90
91 if( memcmp( session->id, entry->session.id,
92 entry->session.length ) != 0 )
93 continue;
94
95 memcpy( session->master, entry->session.master, 48 );
Paul Bakkere81beda2013-03-06 17:40:46 +010096
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +020097 session->verify_result = entry->session.verify_result;
98
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +0100100 /*
101 * Restore peer certificate (without rest of the original chain)
102 */
103 if( entry->peer_cert.p != NULL )
104 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100105 if( ( session->peer_cert = (x509_crt *) polarssl_malloc(
106 sizeof(x509_crt) ) ) == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200107 {
108 ret = 1;
109 goto exit;
110 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100111
Paul Bakkerb6b09562013-09-18 14:17:41 +0200112 x509_crt_init( session->peer_cert );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200113 if( x509_crt_parse( session->peer_cert, entry->peer_cert.p,
114 entry->peer_cert.len ) != 0 )
Paul Bakkere81beda2013-03-06 17:40:46 +0100115 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200116 polarssl_free( session->peer_cert );
Paul Bakkere81beda2013-03-06 17:40:46 +0100117 session->peer_cert = NULL;
Paul Bakkerc5598842013-09-28 15:01:27 +0200118 ret = 1;
119 goto exit;
Paul Bakkere81beda2013-03-06 17:40:46 +0100120 }
121 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200122#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkere81beda2013-03-06 17:40:46 +0100123
Paul Bakkerc5598842013-09-28 15:01:27 +0200124 ret = 0;
125 goto exit;
Paul Bakker0a597072012-09-25 21:55:46 +0000126 }
127
Paul Bakkerc5598842013-09-28 15:01:27 +0200128exit:
129#if defined(POLARSSL_THREADING_C)
130 if( polarssl_mutex_unlock( &cache->mutex ) != 0 )
131 ret = 1;
132#endif
133
134 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000135}
136
137int ssl_cache_set( void *data, const ssl_session *session )
138{
Paul Bakkerc5598842013-09-28 15:01:27 +0200139 int ret = 1;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200140#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000141 time_t t = time( NULL ), oldest = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200142 ssl_cache_entry *old = NULL;
143#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000144 ssl_cache_context *cache = (ssl_cache_context *) data;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200145 ssl_cache_entry *cur, *prv;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000146 int count = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000147
Paul Bakkerc5598842013-09-28 15:01:27 +0200148#if defined(POLARSSL_THREADING_C)
149 if( ( ret = polarssl_mutex_lock( &cache->mutex ) ) != 0 )
150 return( ret );
151#endif
152
Paul Bakker0a597072012-09-25 21:55:46 +0000153 cur = cache->chain;
154 prv = NULL;
155
156 while( cur != NULL )
157 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000158 count++;
159
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200160#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000161 if( cache->timeout != 0 &&
162 (int) ( t - cur->timestamp ) > cache->timeout )
163 {
164 cur->timestamp = t;
165 break; /* expired, reuse this slot, update timestamp */
166 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200167#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000168
169 if( memcmp( session->id, cur->session.id, cur->session.length ) == 0 )
170 break; /* client reconnected, keep timestamp for session id */
171
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200172#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000173 if( oldest == 0 || cur->timestamp < oldest )
174 {
175 oldest = cur->timestamp;
176 old = cur;
177 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200178#endif
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000179
Paul Bakker0a597072012-09-25 21:55:46 +0000180 prv = cur;
181 cur = cur->next;
182 }
183
184 if( cur == NULL )
185 {
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200186#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000187 /*
188 * Reuse oldest entry if max_entries reached
189 */
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100190 if( count >= cache->max_entries )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000191 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100192 if( old == NULL )
193 {
194 ret = 1;
195 goto exit;
196 }
197
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000198 cur = old;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000199 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200200#else /* POLARSSL_HAVE_TIME */
201 /*
202 * Reuse first entry in chain if max_entries reached,
203 * but move to last place
204 */
205 if( count >= cache->max_entries )
206 {
207 if( cache->chain == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200208 {
209 ret = 1;
210 goto exit;
211 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200212
213 cur = cache->chain;
214 cache->chain = cur->next;
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100215 cur->next = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200216 prv->next = cur;
217 }
218#endif /* POLARSSL_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000219 else
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000220 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100221 /*
222 * max_entries not reached, create new entry
223 */
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100224 cur = (ssl_cache_entry *) polarssl_malloc( sizeof(ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000225 if( cur == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200226 {
227 ret = 1;
228 goto exit;
229 }
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000230
Paul Bakkere81beda2013-03-06 17:40:46 +0100231 memset( cur, 0, sizeof(ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000232
233 if( prv == NULL )
234 cache->chain = cur;
235 else
236 prv->next = cur;
237 }
Paul Bakker0a597072012-09-25 21:55:46 +0000238
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200239#if defined(POLARSSL_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
244 memcpy( &cur->session, session, sizeof( ssl_session ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200245
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200246#if defined(POLARSSL_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 {
252 polarssl_free( cur->peer_cert.p );
253 memset( &cur->peer_cert, 0, sizeof(x509_buf) );
254 }
255
256 /*
Paul Bakkere81beda2013-03-06 17:40:46 +0100257 * Store peer certificate
258 */
259 if( session->peer_cert != NULL )
260 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100261 cur->peer_cert.p = (unsigned char *) polarssl_malloc(
262 session->peer_cert->raw.len );
Paul Bakkere81beda2013-03-06 17:40:46 +0100263 if( cur->peer_cert.p == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200264 {
265 ret = 1;
266 goto exit;
267 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100268
269 memcpy( cur->peer_cert.p, session->peer_cert->raw.p,
270 session->peer_cert->raw.len );
271 cur->peer_cert.len = session->peer_cert->raw.len;
272
273 cur->session.peer_cert = NULL;
274 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200275#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker0a597072012-09-25 21:55:46 +0000276
Paul Bakkerc5598842013-09-28 15:01:27 +0200277 ret = 0;
278
279exit:
280#if defined(POLARSSL_THREADING_C)
281 if( polarssl_mutex_unlock( &cache->mutex ) != 0 )
282 ret = 1;
283#endif
284
285 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000286}
287
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200288#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000289void ssl_cache_set_timeout( ssl_cache_context *cache, int timeout )
290{
291 if( timeout < 0 ) timeout = 0;
292
293 cache->timeout = timeout;
294}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200295#endif /* POLARSSL_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000296
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000297void ssl_cache_set_max_entries( ssl_cache_context *cache, int max )
298{
299 if( max < 0 ) max = 0;
300
301 cache->max_entries = max;
302}
303
Paul Bakker0a597072012-09-25 21:55:46 +0000304void ssl_cache_free( ssl_cache_context *cache )
305{
306 ssl_cache_entry *cur, *prv;
307
308 cur = cache->chain;
309
310 while( cur != NULL )
311 {
312 prv = cur;
313 cur = cur->next;
314
315 ssl_session_free( &prv->session );
Paul Bakkere81beda2013-03-06 17:40:46 +0100316
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200317#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker14b16c62014-05-28 11:33:54 +0200318 polarssl_free( prv->peer_cert.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200319#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkere81beda2013-03-06 17:40:46 +0100320
Paul Bakker6e339b52013-07-03 13:37:05 +0200321 polarssl_free( prv );
Paul Bakker0a597072012-09-25 21:55:46 +0000322 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200323
324#if defined(POLARSSL_THREADING_C)
325 polarssl_mutex_free( &cache->mutex );
326#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000327}
328
329#endif /* POLARSSL_SSL_CACHE_C */