blob: ab948d6b085ff984dc8b20cce9f762e4f1ccc3bb [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 );
74 return( 0 );
75 }
76
77 return( 1 );
78}
79
80int ssl_cache_set( void *data, const ssl_session *session )
81{
Paul Bakkerba26e9e2012-10-23 22:18:28 +000082 time_t t = time( NULL ), oldest = 0;
Paul Bakker0a597072012-09-25 21:55:46 +000083 ssl_cache_context *cache = (ssl_cache_context *) data;
Paul Bakkerba26e9e2012-10-23 22:18:28 +000084 ssl_cache_entry *cur, *prv, *old = NULL;
85 int count = 0;
Paul Bakker0a597072012-09-25 21:55:46 +000086
87 cur = cache->chain;
88 prv = NULL;
89
90 while( cur != NULL )
91 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +000092 count++;
93
Paul Bakker0a597072012-09-25 21:55:46 +000094 if( cache->timeout != 0 &&
95 (int) ( t - cur->timestamp ) > cache->timeout )
96 {
97 cur->timestamp = t;
98 break; /* expired, reuse this slot, update timestamp */
99 }
100
101 if( memcmp( session->id, cur->session.id, cur->session.length ) == 0 )
102 break; /* client reconnected, keep timestamp for session id */
103
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000104 if( oldest == 0 || cur->timestamp < oldest )
105 {
106 oldest = cur->timestamp;
107 old = cur;
108 }
109
Paul Bakker0a597072012-09-25 21:55:46 +0000110 prv = cur;
111 cur = cur->next;
112 }
113
114 if( cur == NULL )
115 {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000116 /*
117 * Reuse oldest entry if max_entries reached
118 */
119 if( old != NULL && count >= cache->max_entries )
120 {
121 cur = old;
122 memset( &cur->session, 0, sizeof( ssl_session ) );
123 }
Paul Bakker0a597072012-09-25 21:55:46 +0000124 else
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000125 {
126 cur = (ssl_cache_entry *) malloc( sizeof( ssl_cache_entry ) );
127 if( cur == NULL )
128 return( 1 );
129
130 memset( cur, 0, sizeof( ssl_cache_entry ) );
131
132 if( prv == NULL )
133 cache->chain = cur;
134 else
135 prv->next = cur;
136 }
Paul Bakker0a597072012-09-25 21:55:46 +0000137
138 cur->timestamp = t;
139 }
140
141 memcpy( &cur->session, session, sizeof( ssl_session ) );
142
143 // Do not include peer_cert in cache entry
144 //
145 cur->session.peer_cert = NULL;
146
147 return( 0 );
148}
149
150void ssl_cache_set_timeout( ssl_cache_context *cache, int timeout )
151{
152 if( timeout < 0 ) timeout = 0;
153
154 cache->timeout = timeout;
155}
156
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000157void ssl_cache_set_max_entries( ssl_cache_context *cache, int max )
158{
159 if( max < 0 ) max = 0;
160
161 cache->max_entries = max;
162}
163
Paul Bakker0a597072012-09-25 21:55:46 +0000164void ssl_cache_free( ssl_cache_context *cache )
165{
166 ssl_cache_entry *cur, *prv;
167
168 cur = cache->chain;
169
170 while( cur != NULL )
171 {
172 prv = cur;
173 cur = cur->next;
174
175 ssl_session_free( &prv->session );
176 free( prv );
177 }
178}
179
180#endif /* POLARSSL_SSL_CACHE_C */