blob: 3090dc062166bf723f2f610a59035818c43b3569 [file] [log] [blame]
Paul Bakker0a597072012-09-25 21:55:46 +00001/*
2 * SSL session cache implementation
3 *
4 * Copyright (C) 2006-2012, Brainspark B.V.
5 *
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 Bakker6e339b52013-07-03 13:37:05 +020036#if defined(POLARSSL_MEMORY_C)
37#include "polarssl/memory.h"
38#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 Bakker0a597072012-09-25 21:55:46 +000051}
52
53int ssl_cache_get( void *data, ssl_session *session )
54{
Paul Bakkerfa9b1002013-07-03 15:31:03 +020055#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000056 time_t t = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +020057#endif
Paul Bakker0a597072012-09-25 21:55:46 +000058 ssl_cache_context *cache = (ssl_cache_context *) data;
59 ssl_cache_entry *cur, *entry;
60
61 cur = cache->chain;
62 entry = NULL;
63
64 while( cur != NULL )
65 {
66 entry = cur;
67 cur = cur->next;
68
Paul Bakkerfa9b1002013-07-03 15:31:03 +020069#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000070 if( cache->timeout != 0 &&
71 (int) ( t - entry->timestamp ) > cache->timeout )
72 continue;
Paul Bakkerfa9b1002013-07-03 15:31:03 +020073#endif
Paul Bakker0a597072012-09-25 21:55:46 +000074
75 if( session->ciphersuite != entry->session.ciphersuite ||
76 session->compression != entry->session.compression ||
77 session->length != entry->session.length )
78 continue;
79
80 if( memcmp( session->id, entry->session.id,
81 entry->session.length ) != 0 )
82 continue;
83
84 memcpy( session->master, entry->session.master, 48 );
Paul Bakkere81beda2013-03-06 17:40:46 +010085
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +020086 session->verify_result = entry->session.verify_result;
87
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +010089 /*
90 * Restore peer certificate (without rest of the original chain)
91 */
92 if( entry->peer_cert.p != NULL )
93 {
Paul Bakker6e339b52013-07-03 13:37:05 +020094 session->peer_cert = (x509_cert *) polarssl_malloc( sizeof(x509_cert) );
Paul Bakkere81beda2013-03-06 17:40:46 +010095 if( session->peer_cert == NULL )
96 return( 1 );
97
98 memset( session->peer_cert, 0, sizeof(x509_cert) );
99 if( x509parse_crt( session->peer_cert, entry->peer_cert.p,
100 entry->peer_cert.len ) != 0 )
101 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200102 polarssl_free( session->peer_cert );
Paul Bakkere81beda2013-03-06 17:40:46 +0100103 session->peer_cert = NULL;
104 return( 1 );
105 }
106 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkere81beda2013-03-06 17:40:46 +0100108
Paul Bakker0a597072012-09-25 21:55:46 +0000109 return( 0 );
110 }
111
112 return( 1 );
113}
114
115int ssl_cache_set( void *data, const ssl_session *session )
116{
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200117#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000118 time_t t = time( NULL ), oldest = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200119 ssl_cache_entry *old = NULL;
120#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000121 ssl_cache_context *cache = (ssl_cache_context *) data;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200122 ssl_cache_entry *cur, *prv;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000123 int count = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000124
125 cur = cache->chain;
126 prv = NULL;
127
128 while( cur != NULL )
129 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000130 count++;
131
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200132#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000133 if( cache->timeout != 0 &&
134 (int) ( t - cur->timestamp ) > cache->timeout )
135 {
136 cur->timestamp = t;
137 break; /* expired, reuse this slot, update timestamp */
138 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200139#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000140
141 if( memcmp( session->id, cur->session.id, cur->session.length ) == 0 )
142 break; /* client reconnected, keep timestamp for session id */
143
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200144#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000145 if( oldest == 0 || cur->timestamp < oldest )
146 {
147 oldest = cur->timestamp;
148 old = cur;
149 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200150#endif
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000151
Paul Bakker0a597072012-09-25 21:55:46 +0000152 prv = cur;
153 cur = cur->next;
154 }
155
156 if( cur == NULL )
157 {
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200158#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000159 /*
160 * Reuse oldest entry if max_entries reached
161 */
162 if( old != NULL && count >= cache->max_entries )
163 {
164 cur = old;
Paul Bakkere81beda2013-03-06 17:40:46 +0100165 memset( &cur->session, 0, sizeof(ssl_session) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200166#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +0100167 if( cur->peer_cert.p != NULL )
168 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200169 polarssl_free( cur->peer_cert.p );
Paul Bakkere81beda2013-03-06 17:40:46 +0100170 memset( &cur->peer_cert, 0, sizeof(x509_buf) );
171 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200172#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000173 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200174#else /* POLARSSL_HAVE_TIME */
175 /*
176 * Reuse first entry in chain if max_entries reached,
177 * but move to last place
178 */
179 if( count >= cache->max_entries )
180 {
181 if( cache->chain == NULL )
182 return( 1 );
183
184 cur = cache->chain;
185 cache->chain = cur->next;
186
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200187#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200188 if( cur->peer_cert.p != NULL )
189 {
190 polarssl_free( cur->peer_cert.p );
191 memset( &cur->peer_cert, 0, sizeof(x509_buf) );
192 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200193#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200194
195 memset( cur, 0, sizeof(ssl_cache_entry) );
196 prv->next = cur;
197 }
198#endif /* POLARSSL_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000199 else
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000200 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200201 cur = (ssl_cache_entry *) polarssl_malloc( sizeof(ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000202 if( cur == NULL )
203 return( 1 );
204
Paul Bakkere81beda2013-03-06 17:40:46 +0100205 memset( cur, 0, sizeof(ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000206
207 if( prv == NULL )
208 cache->chain = cur;
209 else
210 prv->next = cur;
211 }
Paul Bakker0a597072012-09-25 21:55:46 +0000212
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200213#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000214 cur->timestamp = t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200215#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000216 }
217
218 memcpy( &cur->session, session, sizeof( ssl_session ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200219
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200220#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +0100221 /*
222 * Store peer certificate
223 */
224 if( session->peer_cert != NULL )
225 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200226 cur->peer_cert.p = (unsigned char *) polarssl_malloc( session->peer_cert->raw.len );
Paul Bakkere81beda2013-03-06 17:40:46 +0100227 if( cur->peer_cert.p == NULL )
228 return( 1 );
229
230 memcpy( cur->peer_cert.p, session->peer_cert->raw.p,
231 session->peer_cert->raw.len );
232 cur->peer_cert.len = session->peer_cert->raw.len;
233
234 cur->session.peer_cert = NULL;
235 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200236#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker0a597072012-09-25 21:55:46 +0000237
238 return( 0 );
239}
240
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200241#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000242void ssl_cache_set_timeout( ssl_cache_context *cache, int timeout )
243{
244 if( timeout < 0 ) timeout = 0;
245
246 cache->timeout = timeout;
247}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200248#endif /* POLARSSL_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000249
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000250void ssl_cache_set_max_entries( ssl_cache_context *cache, int max )
251{
252 if( max < 0 ) max = 0;
253
254 cache->max_entries = max;
255}
256
Paul Bakker0a597072012-09-25 21:55:46 +0000257void ssl_cache_free( ssl_cache_context *cache )
258{
259 ssl_cache_entry *cur, *prv;
260
261 cur = cache->chain;
262
263 while( cur != NULL )
264 {
265 prv = cur;
266 cur = cur->next;
267
268 ssl_session_free( &prv->session );
Paul Bakkere81beda2013-03-06 17:40:46 +0100269
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200270#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +0100271 if( prv->peer_cert.p != NULL )
Paul Bakker6e339b52013-07-03 13:37:05 +0200272 polarssl_free( prv->peer_cert.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200273#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkere81beda2013-03-06 17:40:46 +0100274
Paul Bakker6e339b52013-07-03 13:37:05 +0200275 polarssl_free( prv );
Paul Bakker0a597072012-09-25 21:55:46 +0000276 }
277}
278
279#endif /* POLARSSL_SSL_CACHE_C */