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 | |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 36 | #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 Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 43 | #include <stdlib.h> |
| 44 | |
| 45 | void 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 Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 50 | cache->max_entries = SSL_CACHE_DEFAULT_MAX_ENTRIES; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | int ssl_cache_get( void *data, ssl_session *session ) |
| 54 | { |
| 55 | time_t t = time( NULL ); |
| 56 | ssl_cache_context *cache = (ssl_cache_context *) data; |
| 57 | ssl_cache_entry *cur, *entry; |
| 58 | |
| 59 | cur = cache->chain; |
| 60 | entry = NULL; |
| 61 | |
| 62 | while( cur != NULL ) |
| 63 | { |
| 64 | entry = cur; |
| 65 | cur = cur->next; |
| 66 | |
| 67 | if( cache->timeout != 0 && |
| 68 | (int) ( t - entry->timestamp ) > cache->timeout ) |
| 69 | continue; |
| 70 | |
| 71 | if( session->ciphersuite != entry->session.ciphersuite || |
| 72 | session->compression != entry->session.compression || |
| 73 | session->length != entry->session.length ) |
| 74 | continue; |
| 75 | |
| 76 | if( memcmp( session->id, entry->session.id, |
| 77 | entry->session.length ) != 0 ) |
| 78 | continue; |
| 79 | |
| 80 | memcpy( session->master, entry->session.master, 48 ); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 81 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 82 | #if defined(POLARSSL_X509_PARSE_C) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 83 | /* |
| 84 | * Restore peer certificate (without rest of the original chain) |
| 85 | */ |
| 86 | if( entry->peer_cert.p != NULL ) |
| 87 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 88 | session->peer_cert = (x509_cert *) polarssl_malloc( sizeof(x509_cert) ); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 89 | if( session->peer_cert == NULL ) |
| 90 | return( 1 ); |
| 91 | |
| 92 | memset( session->peer_cert, 0, sizeof(x509_cert) ); |
| 93 | if( x509parse_crt( session->peer_cert, entry->peer_cert.p, |
| 94 | entry->peer_cert.len ) != 0 ) |
| 95 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 96 | polarssl_free( session->peer_cert ); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 97 | session->peer_cert = NULL; |
| 98 | return( 1 ); |
| 99 | } |
| 100 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 101 | #endif /* POLARSSL_X509_PARSE_C */ |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 102 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 103 | return( 0 ); |
| 104 | } |
| 105 | |
| 106 | return( 1 ); |
| 107 | } |
| 108 | |
| 109 | int ssl_cache_set( void *data, const ssl_session *session ) |
| 110 | { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 111 | time_t t = time( NULL ), oldest = 0; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 112 | ssl_cache_context *cache = (ssl_cache_context *) data; |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 113 | ssl_cache_entry *cur, *prv, *old = NULL; |
| 114 | int count = 0; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 115 | |
| 116 | cur = cache->chain; |
| 117 | prv = NULL; |
| 118 | |
| 119 | while( cur != NULL ) |
| 120 | { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 121 | count++; |
| 122 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 123 | if( cache->timeout != 0 && |
| 124 | (int) ( t - cur->timestamp ) > cache->timeout ) |
| 125 | { |
| 126 | cur->timestamp = t; |
| 127 | break; /* expired, reuse this slot, update timestamp */ |
| 128 | } |
| 129 | |
| 130 | if( memcmp( session->id, cur->session.id, cur->session.length ) == 0 ) |
| 131 | break; /* client reconnected, keep timestamp for session id */ |
| 132 | |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 133 | if( oldest == 0 || cur->timestamp < oldest ) |
| 134 | { |
| 135 | oldest = cur->timestamp; |
| 136 | old = cur; |
| 137 | } |
| 138 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 139 | prv = cur; |
| 140 | cur = cur->next; |
| 141 | } |
| 142 | |
| 143 | if( cur == NULL ) |
| 144 | { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 145 | /* |
| 146 | * Reuse oldest entry if max_entries reached |
| 147 | */ |
| 148 | if( old != NULL && count >= cache->max_entries ) |
| 149 | { |
| 150 | cur = old; |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 151 | memset( &cur->session, 0, sizeof(ssl_session) ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 152 | #if defined(POLARSSL_X509_PARSE_C) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 153 | if( cur->peer_cert.p != NULL ) |
| 154 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 155 | polarssl_free( cur->peer_cert.p ); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 156 | memset( &cur->peer_cert, 0, sizeof(x509_buf) ); |
| 157 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 158 | #endif /* POLARSSL_X509_PARSE_C */ |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 159 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 160 | else |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 161 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 162 | cur = (ssl_cache_entry *) polarssl_malloc( sizeof(ssl_cache_entry) ); |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 163 | if( cur == NULL ) |
| 164 | return( 1 ); |
| 165 | |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 166 | memset( cur, 0, sizeof(ssl_cache_entry) ); |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 167 | |
| 168 | if( prv == NULL ) |
| 169 | cache->chain = cur; |
| 170 | else |
| 171 | prv->next = cur; |
| 172 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 173 | |
| 174 | cur->timestamp = t; |
| 175 | } |
| 176 | |
| 177 | memcpy( &cur->session, session, sizeof( ssl_session ) ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 178 | |
| 179 | #if defined(POLARSSL_X509_PARSE_C) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 180 | /* |
| 181 | * Store peer certificate |
| 182 | */ |
| 183 | if( session->peer_cert != NULL ) |
| 184 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 185 | cur->peer_cert.p = (unsigned char *) polarssl_malloc( session->peer_cert->raw.len ); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 186 | if( cur->peer_cert.p == NULL ) |
| 187 | return( 1 ); |
| 188 | |
| 189 | memcpy( cur->peer_cert.p, session->peer_cert->raw.p, |
| 190 | session->peer_cert->raw.len ); |
| 191 | cur->peer_cert.len = session->peer_cert->raw.len; |
| 192 | |
| 193 | cur->session.peer_cert = NULL; |
| 194 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 195 | #endif /* POLARSSL_X509_PARSE_C */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 196 | |
| 197 | return( 0 ); |
| 198 | } |
| 199 | |
| 200 | void ssl_cache_set_timeout( ssl_cache_context *cache, int timeout ) |
| 201 | { |
| 202 | if( timeout < 0 ) timeout = 0; |
| 203 | |
| 204 | cache->timeout = timeout; |
| 205 | } |
| 206 | |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 207 | void ssl_cache_set_max_entries( ssl_cache_context *cache, int max ) |
| 208 | { |
| 209 | if( max < 0 ) max = 0; |
| 210 | |
| 211 | cache->max_entries = max; |
| 212 | } |
| 213 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 214 | void ssl_cache_free( ssl_cache_context *cache ) |
| 215 | { |
| 216 | ssl_cache_entry *cur, *prv; |
| 217 | |
| 218 | cur = cache->chain; |
| 219 | |
| 220 | while( cur != NULL ) |
| 221 | { |
| 222 | prv = cur; |
| 223 | cur = cur->next; |
| 224 | |
| 225 | ssl_session_free( &prv->session ); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 226 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 227 | #if defined(POLARSSL_X509_PARSE_C) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 228 | if( prv->peer_cert.p != NULL ) |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 229 | polarssl_free( prv->peer_cert.p ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 230 | #endif /* POLARSSL_X509_PARSE_C */ |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 231 | |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 232 | polarssl_free( prv ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| 236 | #endif /* POLARSSL_SSL_CACHE_C */ |