Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL session cache implementation |
| 3 | * |
Paul Bakker | 530927b | 2015-02-13 14:24:10 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | e12abf9 | 2015-01-28 17:13:45 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | /* |
| 23 | * These session callbacks use a simple chained list |
| 24 | * to store and retrieve the session information. |
| 25 | */ |
| 26 | |
| 27 | #include "polarssl/config.h" |
| 28 | |
| 29 | #if defined(POLARSSL_SSL_CACHE_C) |
| 30 | |
| 31 | #include "polarssl/ssl_cache.h" |
| 32 | |
| 33 | #include <stdlib.h> |
| 34 | |
| 35 | void ssl_cache_init( ssl_cache_context *cache ) |
| 36 | { |
| 37 | memset( cache, 0, sizeof( ssl_cache_context ) ); |
| 38 | |
| 39 | cache->timeout = SSL_CACHE_DEFAULT_TIMEOUT; |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 40 | cache->max_entries = SSL_CACHE_DEFAULT_MAX_ENTRIES; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | int ssl_cache_get( void *data, ssl_session *session ) |
| 44 | { |
| 45 | time_t t = time( NULL ); |
| 46 | ssl_cache_context *cache = (ssl_cache_context *) data; |
| 47 | ssl_cache_entry *cur, *entry; |
| 48 | |
| 49 | cur = cache->chain; |
| 50 | entry = NULL; |
| 51 | |
| 52 | while( cur != NULL ) |
| 53 | { |
| 54 | entry = cur; |
| 55 | cur = cur->next; |
| 56 | |
| 57 | if( cache->timeout != 0 && |
| 58 | (int) ( t - entry->timestamp ) > cache->timeout ) |
| 59 | continue; |
| 60 | |
| 61 | if( session->ciphersuite != entry->session.ciphersuite || |
| 62 | session->compression != entry->session.compression || |
| 63 | session->length != entry->session.length ) |
| 64 | continue; |
| 65 | |
| 66 | if( memcmp( session->id, entry->session.id, |
| 67 | entry->session.length ) != 0 ) |
| 68 | continue; |
| 69 | |
| 70 | memcpy( session->master, entry->session.master, 48 ); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * Restore peer certificate (without rest of the original chain) |
| 74 | */ |
| 75 | if( entry->peer_cert.p != NULL ) |
| 76 | { |
| 77 | session->peer_cert = (x509_cert *) malloc( sizeof(x509_cert) ); |
| 78 | if( session->peer_cert == NULL ) |
| 79 | return( 1 ); |
| 80 | |
| 81 | memset( session->peer_cert, 0, sizeof(x509_cert) ); |
| 82 | if( x509parse_crt( session->peer_cert, entry->peer_cert.p, |
| 83 | entry->peer_cert.len ) != 0 ) |
| 84 | { |
| 85 | free( session->peer_cert ); |
| 86 | session->peer_cert = NULL; |
| 87 | return( 1 ); |
| 88 | } |
| 89 | } |
| 90 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 91 | return( 0 ); |
| 92 | } |
| 93 | |
| 94 | return( 1 ); |
| 95 | } |
| 96 | |
| 97 | int ssl_cache_set( void *data, const ssl_session *session ) |
| 98 | { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 99 | time_t t = time( NULL ), oldest = 0; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 100 | ssl_cache_context *cache = (ssl_cache_context *) data; |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 101 | ssl_cache_entry *cur, *prv, *old = NULL; |
| 102 | int count = 0; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 103 | |
| 104 | cur = cache->chain; |
| 105 | prv = NULL; |
| 106 | |
| 107 | while( cur != NULL ) |
| 108 | { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 109 | count++; |
| 110 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 111 | if( cache->timeout != 0 && |
| 112 | (int) ( t - cur->timestamp ) > cache->timeout ) |
| 113 | { |
| 114 | cur->timestamp = t; |
| 115 | break; /* expired, reuse this slot, update timestamp */ |
| 116 | } |
| 117 | |
| 118 | if( memcmp( session->id, cur->session.id, cur->session.length ) == 0 ) |
| 119 | break; /* client reconnected, keep timestamp for session id */ |
| 120 | |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 121 | if( oldest == 0 || cur->timestamp < oldest ) |
| 122 | { |
| 123 | oldest = cur->timestamp; |
| 124 | old = cur; |
| 125 | } |
| 126 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 127 | prv = cur; |
| 128 | cur = cur->next; |
| 129 | } |
| 130 | |
| 131 | if( cur == NULL ) |
| 132 | { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 133 | /* |
| 134 | * Reuse oldest entry if max_entries reached |
| 135 | */ |
| 136 | if( old != NULL && count >= cache->max_entries ) |
| 137 | { |
| 138 | cur = old; |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 139 | memset( &cur->session, 0, sizeof(ssl_session) ); |
| 140 | if( cur->peer_cert.p != NULL ) |
| 141 | { |
| 142 | free( cur->peer_cert.p ); |
| 143 | memset( &cur->peer_cert, 0, sizeof(x509_buf) ); |
| 144 | } |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 145 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 146 | else |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 147 | { |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 148 | cur = (ssl_cache_entry *) malloc( sizeof(ssl_cache_entry) ); |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 149 | if( cur == NULL ) |
| 150 | return( 1 ); |
| 151 | |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 152 | memset( cur, 0, sizeof(ssl_cache_entry) ); |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 153 | |
| 154 | if( prv == NULL ) |
| 155 | cache->chain = cur; |
| 156 | else |
| 157 | prv->next = cur; |
| 158 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 159 | |
| 160 | cur->timestamp = t; |
| 161 | } |
| 162 | |
| 163 | memcpy( &cur->session, session, sizeof( ssl_session ) ); |
| 164 | |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 165 | /* |
| 166 | * Store peer certificate |
| 167 | */ |
| 168 | if( session->peer_cert != NULL ) |
| 169 | { |
| 170 | cur->peer_cert.p = (unsigned char *) malloc( session->peer_cert->raw.len ); |
| 171 | if( cur->peer_cert.p == NULL ) |
| 172 | return( 1 ); |
| 173 | |
| 174 | memcpy( cur->peer_cert.p, session->peer_cert->raw.p, |
| 175 | session->peer_cert->raw.len ); |
| 176 | cur->peer_cert.len = session->peer_cert->raw.len; |
| 177 | |
| 178 | cur->session.peer_cert = NULL; |
| 179 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 180 | |
| 181 | return( 0 ); |
| 182 | } |
| 183 | |
| 184 | void ssl_cache_set_timeout( ssl_cache_context *cache, int timeout ) |
| 185 | { |
| 186 | if( timeout < 0 ) timeout = 0; |
| 187 | |
| 188 | cache->timeout = timeout; |
| 189 | } |
| 190 | |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 191 | void ssl_cache_set_max_entries( ssl_cache_context *cache, int max ) |
| 192 | { |
| 193 | if( max < 0 ) max = 0; |
| 194 | |
| 195 | cache->max_entries = max; |
| 196 | } |
| 197 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 198 | void ssl_cache_free( ssl_cache_context *cache ) |
| 199 | { |
| 200 | ssl_cache_entry *cur, *prv; |
| 201 | |
| 202 | cur = cache->chain; |
| 203 | |
| 204 | while( cur != NULL ) |
| 205 | { |
| 206 | prv = cur; |
| 207 | cur = cur->next; |
| 208 | |
| 209 | ssl_session_free( &prv->session ); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 210 | |
| 211 | if( prv->peer_cert.p != NULL ) |
| 212 | free( prv->peer_cert.p ); |
| 213 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 214 | free( prv ); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | #endif /* POLARSSL_SSL_CACHE_C */ |