blob: 93d5d8b3322c33a033d77e495f79254d22b0e054 [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
36#include <stdlib.h>
37
38void ssl_cache_init( ssl_cache_context *cache )
39{
40 memset( cache, 0, sizeof( ssl_cache_context ) );
41
42 cache->timeout = SSL_CACHE_DEFAULT_TIMEOUT;
Paul Bakkerba26e9e2012-10-23 22:18:28 +000043 cache->max_entries = SSL_CACHE_DEFAULT_MAX_ENTRIES;
Paul Bakker0a597072012-09-25 21:55:46 +000044}
45
46int ssl_cache_get( void *data, ssl_session *session )
47{
48 time_t t = time( NULL );
49 ssl_cache_context *cache = (ssl_cache_context *) data;
50 ssl_cache_entry *cur, *entry;
51
52 cur = cache->chain;
53 entry = NULL;
54
55 while( cur != NULL )
56 {
57 entry = cur;
58 cur = cur->next;
59
60 if( cache->timeout != 0 &&
61 (int) ( t - entry->timestamp ) > cache->timeout )
62 continue;
63
64 if( session->ciphersuite != entry->session.ciphersuite ||
65 session->compression != entry->session.compression ||
66 session->length != entry->session.length )
67 continue;
68
69 if( memcmp( session->id, entry->session.id,
70 entry->session.length ) != 0 )
71 continue;
72
73 memcpy( session->master, entry->session.master, 48 );
Paul Bakkere81beda2013-03-06 17:40:46 +010074
Paul Bakkered27a042013-04-18 22:46:23 +020075#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +010076 /*
77 * Restore peer certificate (without rest of the original chain)
78 */
79 if( entry->peer_cert.p != NULL )
80 {
81 session->peer_cert = (x509_cert *) malloc( sizeof(x509_cert) );
82 if( session->peer_cert == NULL )
83 return( 1 );
84
85 memset( session->peer_cert, 0, sizeof(x509_cert) );
86 if( x509parse_crt( session->peer_cert, entry->peer_cert.p,
87 entry->peer_cert.len ) != 0 )
88 {
89 free( session->peer_cert );
90 session->peer_cert = NULL;
91 return( 1 );
92 }
93 }
Paul Bakkered27a042013-04-18 22:46:23 +020094#endif /* POLARSSL_X509_PARSE_C */
Paul Bakkere81beda2013-03-06 17:40:46 +010095
Paul Bakker0a597072012-09-25 21:55:46 +000096 return( 0 );
97 }
98
99 return( 1 );
100}
101
102int ssl_cache_set( void *data, const ssl_session *session )
103{
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000104 time_t t = time( NULL ), oldest = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000105 ssl_cache_context *cache = (ssl_cache_context *) data;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000106 ssl_cache_entry *cur, *prv, *old = NULL;
107 int count = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000108
109 cur = cache->chain;
110 prv = NULL;
111
112 while( cur != NULL )
113 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000114 count++;
115
Paul Bakker0a597072012-09-25 21:55:46 +0000116 if( cache->timeout != 0 &&
117 (int) ( t - cur->timestamp ) > cache->timeout )
118 {
119 cur->timestamp = t;
120 break; /* expired, reuse this slot, update timestamp */
121 }
122
123 if( memcmp( session->id, cur->session.id, cur->session.length ) == 0 )
124 break; /* client reconnected, keep timestamp for session id */
125
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000126 if( oldest == 0 || cur->timestamp < oldest )
127 {
128 oldest = cur->timestamp;
129 old = cur;
130 }
131
Paul Bakker0a597072012-09-25 21:55:46 +0000132 prv = cur;
133 cur = cur->next;
134 }
135
136 if( cur == NULL )
137 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000138 /*
139 * Reuse oldest entry if max_entries reached
140 */
141 if( old != NULL && count >= cache->max_entries )
142 {
143 cur = old;
Paul Bakkere81beda2013-03-06 17:40:46 +0100144 memset( &cur->session, 0, sizeof(ssl_session) );
Paul Bakkered27a042013-04-18 22:46:23 +0200145#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +0100146 if( cur->peer_cert.p != NULL )
147 {
148 free( cur->peer_cert.p );
149 memset( &cur->peer_cert, 0, sizeof(x509_buf) );
150 }
Paul Bakkered27a042013-04-18 22:46:23 +0200151#endif /* POLARSSL_X509_PARSE_C */
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000152 }
Paul Bakker0a597072012-09-25 21:55:46 +0000153 else
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000154 {
Paul Bakkere81beda2013-03-06 17:40:46 +0100155 cur = (ssl_cache_entry *) malloc( sizeof(ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000156 if( cur == NULL )
157 return( 1 );
158
Paul Bakkere81beda2013-03-06 17:40:46 +0100159 memset( cur, 0, sizeof(ssl_cache_entry) );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000160
161 if( prv == NULL )
162 cache->chain = cur;
163 else
164 prv->next = cur;
165 }
Paul Bakker0a597072012-09-25 21:55:46 +0000166
167 cur->timestamp = t;
168 }
169
170 memcpy( &cur->session, session, sizeof( ssl_session ) );
Paul Bakkered27a042013-04-18 22:46:23 +0200171
172#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +0100173 /*
174 * Store peer certificate
175 */
176 if( session->peer_cert != NULL )
177 {
178 cur->peer_cert.p = (unsigned char *) malloc( session->peer_cert->raw.len );
179 if( cur->peer_cert.p == NULL )
180 return( 1 );
181
182 memcpy( cur->peer_cert.p, session->peer_cert->raw.p,
183 session->peer_cert->raw.len );
184 cur->peer_cert.len = session->peer_cert->raw.len;
185
186 cur->session.peer_cert = NULL;
187 }
Paul Bakkered27a042013-04-18 22:46:23 +0200188#endif /* POLARSSL_X509_PARSE_C */
Paul Bakker0a597072012-09-25 21:55:46 +0000189
190 return( 0 );
191}
192
193void ssl_cache_set_timeout( ssl_cache_context *cache, int timeout )
194{
195 if( timeout < 0 ) timeout = 0;
196
197 cache->timeout = timeout;
198}
199
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000200void ssl_cache_set_max_entries( ssl_cache_context *cache, int max )
201{
202 if( max < 0 ) max = 0;
203
204 cache->max_entries = max;
205}
206
Paul Bakker0a597072012-09-25 21:55:46 +0000207void ssl_cache_free( ssl_cache_context *cache )
208{
209 ssl_cache_entry *cur, *prv;
210
211 cur = cache->chain;
212
213 while( cur != NULL )
214 {
215 prv = cur;
216 cur = cur->next;
217
218 ssl_session_free( &prv->session );
Paul Bakkere81beda2013-03-06 17:40:46 +0100219
Paul Bakkered27a042013-04-18 22:46:23 +0200220#if defined(POLARSSL_X509_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +0100221 if( prv->peer_cert.p != NULL )
222 free( prv->peer_cert.p );
Paul Bakkered27a042013-04-18 22:46:23 +0200223#endif /* POLARSSL_X509_PARSE_C */
Paul Bakkere81beda2013-03-06 17:40:46 +0100224
Paul Bakker0a597072012-09-25 21:55:46 +0000225 free( prv );
226 }
227}
228
229#endif /* POLARSSL_SSL_CACHE_C */