blob: d94a7d9f4319f8faf54ba57d9917a340461749a1 [file] [log] [blame]
Paul Bakker0a597072012-09-25 21:55:46 +00001/*
2 * SSL session cache implementation
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker0a597072012-09-25 21:55:46 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * These session callbacks use a simple chained list
27 * to store and retrieve the session information.
28 */
29
30#include "polarssl/config.h"
31
32#if defined(POLARSSL_SSL_CACHE_C)
33
34#include "polarssl/ssl_cache.h"
35
Paul Bakker7dc4c442014-02-01 22:50:26 +010036#if defined(POLARSSL_PLATFORM_C)
37#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020038#else
39#define polarssl_malloc malloc
40#define polarssl_free free
41#endif
42
Paul Bakker0a597072012-09-25 21:55:46 +000043#include <stdlib.h>
44
45void ssl_cache_init( ssl_cache_context *cache )
46{
47 memset( cache, 0, sizeof( ssl_cache_context ) );
48
49 cache->timeout = SSL_CACHE_DEFAULT_TIMEOUT;
Paul Bakkerba26e9e2012-10-23 22:18:28 +000050 cache->max_entries = SSL_CACHE_DEFAULT_MAX_ENTRIES;
Paul Bakkerc5598842013-09-28 15:01:27 +020051
52#if defined(POLARSSL_THREADING_C)
53 polarssl_mutex_init( &cache->mutex );
54#endif
Paul Bakker0a597072012-09-25 21:55:46 +000055}
56
57int ssl_cache_get( void *data, ssl_session *session )
58{
Paul Bakkerc5598842013-09-28 15:01:27 +020059 int ret = 1;
Paul Bakkerfa9b1002013-07-03 15:31:03 +020060#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000061 time_t t = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +020062#endif
Paul Bakker0a597072012-09-25 21:55:46 +000063 ssl_cache_context *cache = (ssl_cache_context *) data;
64 ssl_cache_entry *cur, *entry;
65
Paul Bakkerc5598842013-09-28 15:01:27 +020066#if defined(POLARSSL_THREADING_C)
67 if( polarssl_mutex_lock( &cache->mutex ) != 0 )
68 return( 1 );
69#endif
70
Paul Bakker0a597072012-09-25 21:55:46 +000071 cur = cache->chain;
72 entry = NULL;
73
74 while( cur != NULL )
75 {
76 entry = cur;
77 cur = cur->next;
78
Paul Bakkerfa9b1002013-07-03 15:31:03 +020079#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000080 if( cache->timeout != 0 &&
81 (int) ( t - entry->timestamp ) > cache->timeout )
82 continue;
Paul Bakkerfa9b1002013-07-03 15:31:03 +020083#endif
Paul Bakker0a597072012-09-25 21:55:46 +000084
85 if( session->ciphersuite != entry->session.ciphersuite ||
86 session->compression != entry->session.compression ||
87 session->length != entry->session.length )
88 continue;
89
90 if( memcmp( session->id, entry->session.id,
91 entry->session.length ) != 0 )
92 continue;
93
94 memcpy( session->master, entry->session.master, 48 );
Paul Bakkere81beda2013-03-06 17:40:46 +010095
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +020096 session->verify_result = entry->session.verify_result;
97
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +010099 /*
100 * Restore peer certificate (without rest of the original chain)
101 */
102 if( entry->peer_cert.p != NULL )
103 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200104 session->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
Paul Bakkere81beda2013-03-06 17:40:46 +0100105 if( session->peer_cert == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200106 {
107 ret = 1;
108 goto exit;
109 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100110
Paul Bakkerb6b09562013-09-18 14:17:41 +0200111 x509_crt_init( session->peer_cert );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200112 if( x509_crt_parse( session->peer_cert, entry->peer_cert.p,
113 entry->peer_cert.len ) != 0 )
Paul Bakkere81beda2013-03-06 17:40:46 +0100114 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200115 polarssl_free( session->peer_cert );
Paul Bakkere81beda2013-03-06 17:40:46 +0100116 session->peer_cert = NULL;
Paul Bakkerc5598842013-09-28 15:01:27 +0200117 ret = 1;
118 goto exit;
Paul Bakkere81beda2013-03-06 17:40:46 +0100119 }
120 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200121#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkere81beda2013-03-06 17:40:46 +0100122
Paul Bakkerc5598842013-09-28 15:01:27 +0200123 ret = 0;
124 goto exit;
Paul Bakker0a597072012-09-25 21:55:46 +0000125 }
126
Paul Bakkerc5598842013-09-28 15:01:27 +0200127exit:
128#if defined(POLARSSL_THREADING_C)
129 if( polarssl_mutex_unlock( &cache->mutex ) != 0 )
130 ret = 1;
131#endif
132
133 return( ret );
Paul Bakker0a597072012-09-25 21:55:46 +0000134}
135
136int ssl_cache_set( void *data, const ssl_session *session )
137{
Paul Bakkerc5598842013-09-28 15:01:27 +0200138 int ret = 1;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200139#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000140 time_t t = time( NULL ), oldest = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200141 ssl_cache_entry *old = NULL;
142#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000143 ssl_cache_context *cache = (ssl_cache_context *) data;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200144 ssl_cache_entry *cur, *prv;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000145 int count = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000146
Paul Bakkerc5598842013-09-28 15:01:27 +0200147#if defined(POLARSSL_THREADING_C)
148 if( ( ret = polarssl_mutex_lock( &cache->mutex ) ) != 0 )
149 return( ret );
150#endif
151
Paul Bakker0a597072012-09-25 21:55:46 +0000152 cur = cache->chain;
153 prv = NULL;
154
155 while( cur != NULL )
156 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000157 count++;
158
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200159#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000160 if( cache->timeout != 0 &&
161 (int) ( t - cur->timestamp ) > cache->timeout )
162 {
163 cur->timestamp = t;
164 break; /* expired, reuse this slot, update timestamp */
165 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200166#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000167
168 if( memcmp( session->id, cur->session.id, cur->session.length ) == 0 )
169 break; /* client reconnected, keep timestamp for session id */
170
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200171#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000172 if( oldest == 0 || cur->timestamp < oldest )
173 {
174 oldest = cur->timestamp;
175 old = cur;
176 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200177#endif
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000178
Paul Bakker0a597072012-09-25 21:55:46 +0000179 prv = cur;
180 cur = cur->next;
181 }
182
183 if( cur == NULL )
184 {
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200185#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000186 /*
187 * Reuse oldest entry if max_entries reached
188 */
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100189 if( count >= cache->max_entries )
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000190 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100191 if( old == NULL )
192 {
193 ret = 1;
194 goto exit;
195 }
196
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000197 cur = old;
Paul Bakkere81beda2013-03-06 17:40:46 +0100198 memset( &cur->session, 0, sizeof(ssl_session) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200199#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +0100200 if( cur->peer_cert.p != NULL )
201 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200202 polarssl_free( cur->peer_cert.p );
Paul Bakkere81beda2013-03-06 17:40:46 +0100203 memset( &cur->peer_cert, 0, sizeof(x509_buf) );
204 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200205#endif /* POLARSSL_X509_CRT_PARSE_C */
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;
222
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200223#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200224 if( cur->peer_cert.p != NULL )
225 {
226 polarssl_free( cur->peer_cert.p );
227 memset( &cur->peer_cert, 0, sizeof(x509_buf) );
228 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200229#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200230
231 memset( cur, 0, sizeof(ssl_cache_entry) );
232 prv->next = cur;
233 }
234#endif /* POLARSSL_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000235 else
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000236 {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100237 /*
238 * max_entries not reached, create new entry
239 */
Paul Bakker6e339b52013-07-03 13:37:05 +0200240 cur = (ssl_cache_entry *) polarssl_malloc( sizeof(ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000241 if( cur == NULL )
Paul Bakkerc5598842013-09-28 15:01:27 +0200242 {
243 ret = 1;
244 goto exit;
245 }
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000246
Paul Bakkere81beda2013-03-06 17:40:46 +0100247 memset( cur, 0, sizeof(ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000248
249 if( prv == NULL )
250 cache->chain = cur;
251 else
252 prv->next = cur;
253 }
Paul Bakker0a597072012-09-25 21:55:46 +0000254
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200255#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000256 cur->timestamp = t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200257#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000258 }
259
260 memcpy( &cur->session, session, sizeof( ssl_session ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200261
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200262#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +0100263 /*
264 * Store peer certificate
265 */
266 if( session->peer_cert != NULL )
267 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200268 cur->peer_cert.p = (unsigned char *) 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 Bakkere81beda2013-03-06 17:40:46 +0100324 if( prv->peer_cert.p != NULL )
Paul Bakker6e339b52013-07-03 13:37:05 +0200325 polarssl_free( prv->peer_cert.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200326#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkere81beda2013-03-06 17:40:46 +0100327
Paul Bakker6e339b52013-07-03 13:37:05 +0200328 polarssl_free( prv );
Paul Bakker0a597072012-09-25 21:55:46 +0000329 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200330
331#if defined(POLARSSL_THREADING_C)
332 polarssl_mutex_free( &cache->mutex );
333#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000334}
335
336#endif /* POLARSSL_SSL_CACHE_C */