Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 38 | void 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 Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 43 | cache->max_entries = SSL_CACHE_DEFAULT_MAX_ENTRIES; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | int 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 Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 74 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 75 | #if defined(POLARSSL_X509_PARSE_C) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 76 | /* |
| 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 Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 94 | #endif /* POLARSSL_X509_PARSE_C */ |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 95 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 96 | return( 0 ); |
| 97 | } |
| 98 | |
| 99 | return( 1 ); |
| 100 | } |
| 101 | |
| 102 | int ssl_cache_set( void *data, const ssl_session *session ) |
| 103 | { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 104 | time_t t = time( NULL ), oldest = 0; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 105 | ssl_cache_context *cache = (ssl_cache_context *) data; |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 106 | ssl_cache_entry *cur, *prv, *old = NULL; |
| 107 | int count = 0; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 108 | |
| 109 | cur = cache->chain; |
| 110 | prv = NULL; |
| 111 | |
| 112 | while( cur != NULL ) |
| 113 | { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 114 | count++; |
| 115 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 116 | 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 Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 126 | if( oldest == 0 || cur->timestamp < oldest ) |
| 127 | { |
| 128 | oldest = cur->timestamp; |
| 129 | old = cur; |
| 130 | } |
| 131 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 132 | prv = cur; |
| 133 | cur = cur->next; |
| 134 | } |
| 135 | |
| 136 | if( cur == NULL ) |
| 137 | { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 138 | /* |
| 139 | * Reuse oldest entry if max_entries reached |
| 140 | */ |
| 141 | if( old != NULL && count >= cache->max_entries ) |
| 142 | { |
| 143 | cur = old; |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 144 | memset( &cur->session, 0, sizeof(ssl_session) ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 145 | #if defined(POLARSSL_X509_PARSE_C) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 146 | if( cur->peer_cert.p != NULL ) |
| 147 | { |
| 148 | free( cur->peer_cert.p ); |
| 149 | memset( &cur->peer_cert, 0, sizeof(x509_buf) ); |
| 150 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 151 | #endif /* POLARSSL_X509_PARSE_C */ |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 152 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 153 | else |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 154 | { |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 155 | cur = (ssl_cache_entry *) malloc( sizeof(ssl_cache_entry) ); |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 156 | if( cur == NULL ) |
| 157 | return( 1 ); |
| 158 | |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 159 | memset( cur, 0, sizeof(ssl_cache_entry) ); |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 160 | |
| 161 | if( prv == NULL ) |
| 162 | cache->chain = cur; |
| 163 | else |
| 164 | prv->next = cur; |
| 165 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 166 | |
| 167 | cur->timestamp = t; |
| 168 | } |
| 169 | |
| 170 | memcpy( &cur->session, session, sizeof( ssl_session ) ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 171 | |
| 172 | #if defined(POLARSSL_X509_PARSE_C) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 173 | /* |
| 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 Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 188 | #endif /* POLARSSL_X509_PARSE_C */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 189 | |
| 190 | return( 0 ); |
| 191 | } |
| 192 | |
| 193 | void 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 Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 200 | void 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 Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 207 | void 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 Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 219 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 220 | #if defined(POLARSSL_X509_PARSE_C) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 221 | if( prv->peer_cert.p != NULL ) |
| 222 | free( prv->peer_cert.p ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 223 | #endif /* POLARSSL_X509_PARSE_C */ |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 224 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 225 | free( prv ); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | #endif /* POLARSSL_SSL_CACHE_C */ |