blob: 203a4b7862fb856a93876623b752f1db026cc1c4 [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;
43}
44
45int ssl_cache_get( void *data, ssl_session *session )
46{
47 time_t t = time( NULL );
48 ssl_cache_context *cache = (ssl_cache_context *) data;
49 ssl_cache_entry *cur, *entry;
50
51 cur = cache->chain;
52 entry = NULL;
53
54 while( cur != NULL )
55 {
56 entry = cur;
57 cur = cur->next;
58
59 if( cache->timeout != 0 &&
60 (int) ( t - entry->timestamp ) > cache->timeout )
61 continue;
62
63 if( session->ciphersuite != entry->session.ciphersuite ||
64 session->compression != entry->session.compression ||
65 session->length != entry->session.length )
66 continue;
67
68 if( memcmp( session->id, entry->session.id,
69 entry->session.length ) != 0 )
70 continue;
71
72 memcpy( session->master, entry->session.master, 48 );
73 return( 0 );
74 }
75
76 return( 1 );
77}
78
79int ssl_cache_set( void *data, const ssl_session *session )
80{
81 time_t t = time( NULL );
82 ssl_cache_context *cache = (ssl_cache_context *) data;
83 ssl_cache_entry *cur, *prv;
84
85 cur = cache->chain;
86 prv = NULL;
87
88 while( cur != NULL )
89 {
90 if( cache->timeout != 0 &&
91 (int) ( t - cur->timestamp ) > cache->timeout )
92 {
93 cur->timestamp = t;
94 break; /* expired, reuse this slot, update timestamp */
95 }
96
97 if( memcmp( session->id, cur->session.id, cur->session.length ) == 0 )
98 break; /* client reconnected, keep timestamp for session id */
99
100 prv = cur;
101 cur = cur->next;
102 }
103
104 if( cur == NULL )
105 {
106 cur = (ssl_cache_entry *) malloc( sizeof( ssl_cache_entry ) );
107 if( cur == NULL )
108 return( 1 );
109
110 memset( cur, 0, sizeof( ssl_cache_entry ) );
111
112 if( prv == NULL )
113 cache->chain = cur;
114 else
115 prv->next = cur;
116
117 cur->timestamp = t;
118 }
119
120 memcpy( &cur->session, session, sizeof( ssl_session ) );
121
122 // Do not include peer_cert in cache entry
123 //
124 cur->session.peer_cert = NULL;
125
126 return( 0 );
127}
128
129void ssl_cache_set_timeout( ssl_cache_context *cache, int timeout )
130{
131 if( timeout < 0 ) timeout = 0;
132
133 cache->timeout = timeout;
134}
135
136void ssl_cache_free( ssl_cache_context *cache )
137{
138 ssl_cache_entry *cur, *prv;
139
140 cur = cache->chain;
141
142 while( cur != NULL )
143 {
144 prv = cur;
145 cur = cur->next;
146
147 ssl_session_free( &prv->session );
148 free( prv );
149 }
150}
151
152#endif /* POLARSSL_SSL_CACHE_C */