| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  SSL session cache implementation | 
|  | 3 | * | 
| Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 5 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7 | * | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 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 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 28 | #include "mbedtls/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 31 | #endif | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 32 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #if defined(MBEDTLS_SSL_CACHE_C) | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 34 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 35 | #include "mbedtls/ssl_cache.h" | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 36 |  | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 37 | #include <string.h> | 
|  | 38 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | #if defined(MBEDTLS_PLATFORM_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 40 | #include "mbedtls/platform.h" | 
| Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 41 | #else | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 42 | #include <stdlib.h> | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | #define mbedtls_malloc     malloc | 
|  | 44 | #define mbedtls_free       free | 
| Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 45 | #endif | 
|  | 46 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache ) | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 48 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) ); | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 50 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT; | 
|  | 52 | cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES; | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 53 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | #if defined(MBEDTLS_THREADING_C) | 
|  | 55 | mbedtls_mutex_init( &cache->mutex ); | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 56 | #endif | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session ) | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 60 | { | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 61 | int ret = 1; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | #if defined(MBEDTLS_HAVE_TIME) | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 63 | time_t t = time( NULL ); | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 64 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data; | 
|  | 66 | mbedtls_ssl_cache_entry *cur, *entry; | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 67 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 68 | #if defined(MBEDTLS_THREADING_C) | 
|  | 69 | if( mbedtls_mutex_lock( &cache->mutex ) != 0 ) | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 70 | return( 1 ); | 
|  | 71 | #endif | 
|  | 72 |  | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 73 | cur = cache->chain; | 
|  | 74 | entry = NULL; | 
|  | 75 |  | 
|  | 76 | while( cur != NULL ) | 
|  | 77 | { | 
|  | 78 | entry = cur; | 
|  | 79 | cur = cur->next; | 
|  | 80 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 81 | #if defined(MBEDTLS_HAVE_TIME) | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 82 | if( cache->timeout != 0 && | 
|  | 83 | (int) ( t - entry->timestamp ) > cache->timeout ) | 
|  | 84 | continue; | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 85 | #endif | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 86 |  | 
|  | 87 | if( session->ciphersuite != entry->session.ciphersuite || | 
|  | 88 | session->compression != entry->session.compression || | 
|  | 89 | session->length != entry->session.length ) | 
|  | 90 | continue; | 
|  | 91 |  | 
|  | 92 | if( memcmp( session->id, entry->session.id, | 
|  | 93 | entry->session.length ) != 0 ) | 
|  | 94 | continue; | 
|  | 95 |  | 
|  | 96 | memcpy( session->master, entry->session.master, 48 ); | 
| Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 97 |  | 
| Manuel Pégourié-Gonnard | 38d1eba | 2013-08-23 10:44:29 +0200 | [diff] [blame] | 98 | session->verify_result = entry->session.verify_result; | 
|  | 99 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 100 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 101 | /* | 
|  | 102 | * Restore peer certificate (without rest of the original chain) | 
|  | 103 | */ | 
|  | 104 | if( entry->peer_cert.p != NULL ) | 
|  | 105 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | if( ( session->peer_cert = mbedtls_malloc( | 
|  | 107 | sizeof(mbedtls_x509_crt) ) ) == NULL ) | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 108 | { | 
|  | 109 | ret = 1; | 
|  | 110 | goto exit; | 
|  | 111 | } | 
| Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 112 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | mbedtls_x509_crt_init( session->peer_cert ); | 
|  | 114 | if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p, | 
| Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 115 | entry->peer_cert.len ) != 0 ) | 
| Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 116 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | mbedtls_free( session->peer_cert ); | 
| Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 118 | session->peer_cert = NULL; | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 119 | ret = 1; | 
|  | 120 | goto exit; | 
| Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 121 | } | 
|  | 122 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 124 |  | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 125 | ret = 0; | 
|  | 126 | goto exit; | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 127 | } | 
|  | 128 |  | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 129 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | #if defined(MBEDTLS_THREADING_C) | 
|  | 131 | if( mbedtls_mutex_unlock( &cache->mutex ) != 0 ) | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 132 | ret = 1; | 
|  | 133 | #endif | 
|  | 134 |  | 
|  | 135 | return( ret ); | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 136 | } | 
|  | 137 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 138 | int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session ) | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 139 | { | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 140 | int ret = 1; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 141 | #if defined(MBEDTLS_HAVE_TIME) | 
| Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 142 | time_t t = time( NULL ), oldest = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | mbedtls_ssl_cache_entry *old = NULL; | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 144 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data; | 
|  | 146 | mbedtls_ssl_cache_entry *cur, *prv; | 
| Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 147 | int count = 0; | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 148 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 149 | #if defined(MBEDTLS_THREADING_C) | 
|  | 150 | if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 ) | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 151 | return( ret ); | 
|  | 152 | #endif | 
|  | 153 |  | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 154 | cur = cache->chain; | 
|  | 155 | prv = NULL; | 
|  | 156 |  | 
|  | 157 | while( cur != NULL ) | 
|  | 158 | { | 
| Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 159 | count++; | 
|  | 160 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | #if defined(MBEDTLS_HAVE_TIME) | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 162 | if( cache->timeout != 0 && | 
|  | 163 | (int) ( t - cur->timestamp ) > cache->timeout ) | 
|  | 164 | { | 
|  | 165 | cur->timestamp = t; | 
|  | 166 | break; /* expired, reuse this slot, update timestamp */ | 
|  | 167 | } | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 168 | #endif | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 169 |  | 
|  | 170 | if( memcmp( session->id, cur->session.id, cur->session.length ) == 0 ) | 
|  | 171 | break; /* client reconnected, keep timestamp for session id */ | 
|  | 172 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 173 | #if defined(MBEDTLS_HAVE_TIME) | 
| Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 174 | if( oldest == 0 || cur->timestamp < oldest ) | 
|  | 175 | { | 
|  | 176 | oldest = cur->timestamp; | 
|  | 177 | old = cur; | 
|  | 178 | } | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 179 | #endif | 
| Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 180 |  | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 181 | prv = cur; | 
|  | 182 | cur = cur->next; | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | if( cur == NULL ) | 
|  | 186 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 187 | #if defined(MBEDTLS_HAVE_TIME) | 
| Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 188 | /* | 
|  | 189 | * Reuse oldest entry if max_entries reached | 
|  | 190 | */ | 
| Manuel Pégourié-Gonnard | 274a12e | 2014-02-20 21:32:08 +0100 | [diff] [blame] | 191 | if( count >= cache->max_entries ) | 
| Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 192 | { | 
| Manuel Pégourié-Gonnard | 274a12e | 2014-02-20 21:32:08 +0100 | [diff] [blame] | 193 | if( old == NULL ) | 
|  | 194 | { | 
|  | 195 | ret = 1; | 
|  | 196 | goto exit; | 
|  | 197 | } | 
|  | 198 |  | 
| Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 199 | cur = old; | 
| Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 200 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | #else /* MBEDTLS_HAVE_TIME */ | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 202 | /* | 
|  | 203 | * Reuse first entry in chain if max_entries reached, | 
|  | 204 | * but move to last place | 
|  | 205 | */ | 
|  | 206 | if( count >= cache->max_entries ) | 
|  | 207 | { | 
|  | 208 | if( cache->chain == NULL ) | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 209 | { | 
|  | 210 | ret = 1; | 
|  | 211 | goto exit; | 
|  | 212 | } | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 213 |  | 
|  | 214 | cur = cache->chain; | 
|  | 215 | cache->chain = cur->next; | 
| Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 216 | cur->next = NULL; | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 217 | prv->next = cur; | 
|  | 218 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 219 | #endif /* MBEDTLS_HAVE_TIME */ | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 220 | else | 
| Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 221 | { | 
| Manuel Pégourié-Gonnard | 274a12e | 2014-02-20 21:32:08 +0100 | [diff] [blame] | 222 | /* | 
|  | 223 | * max_entries not reached, create new entry | 
|  | 224 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | cur = mbedtls_malloc( sizeof(mbedtls_ssl_cache_entry) ); | 
| Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 226 | if( cur == NULL ) | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 227 | { | 
|  | 228 | ret = 1; | 
|  | 229 | goto exit; | 
|  | 230 | } | 
| Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 231 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 232 | memset( cur, 0, sizeof(mbedtls_ssl_cache_entry) ); | 
| Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 233 |  | 
|  | 234 | if( prv == NULL ) | 
|  | 235 | cache->chain = cur; | 
|  | 236 | else | 
|  | 237 | prv->next = cur; | 
|  | 238 | } | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 239 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 240 | #if defined(MBEDTLS_HAVE_TIME) | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 241 | cur->timestamp = t; | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 242 | #endif | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 243 | } | 
|  | 244 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 245 | memcpy( &cur->session, session, sizeof( mbedtls_ssl_session ) ); | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 246 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 247 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 248 | /* | 
| Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 249 | * If we're reusing an entry, free its certificate first | 
|  | 250 | */ | 
|  | 251 | if( cur->peer_cert.p != NULL ) | 
|  | 252 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 253 | mbedtls_free( cur->peer_cert.p ); | 
|  | 254 | memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) ); | 
| Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 255 | } | 
|  | 256 |  | 
|  | 257 | /* | 
| Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 258 | * Store peer certificate | 
|  | 259 | */ | 
|  | 260 | if( session->peer_cert != NULL ) | 
|  | 261 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | cur->peer_cert.p = mbedtls_malloc( session->peer_cert->raw.len ); | 
| Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 263 | if( cur->peer_cert.p == NULL ) | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 264 | { | 
|  | 265 | ret = 1; | 
|  | 266 | goto exit; | 
|  | 267 | } | 
| Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 268 |  | 
|  | 269 | memcpy( cur->peer_cert.p, session->peer_cert->raw.p, | 
|  | 270 | session->peer_cert->raw.len ); | 
|  | 271 | cur->peer_cert.len = session->peer_cert->raw.len; | 
|  | 272 |  | 
|  | 273 | cur->session.peer_cert = NULL; | 
|  | 274 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 275 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 276 |  | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 277 | ret = 0; | 
|  | 278 |  | 
|  | 279 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | #if defined(MBEDTLS_THREADING_C) | 
|  | 281 | if( mbedtls_mutex_unlock( &cache->mutex ) != 0 ) | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 282 | ret = 1; | 
|  | 283 | #endif | 
|  | 284 |  | 
|  | 285 | return( ret ); | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 286 | } | 
|  | 287 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 288 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 289 | void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout ) | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 290 | { | 
|  | 291 | if( timeout < 0 ) timeout = 0; | 
|  | 292 |  | 
|  | 293 | cache->timeout = timeout; | 
|  | 294 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 295 | #endif /* MBEDTLS_HAVE_TIME */ | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 296 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 297 | void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max ) | 
| Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 298 | { | 
|  | 299 | if( max < 0 ) max = 0; | 
|  | 300 |  | 
|  | 301 | cache->max_entries = max; | 
|  | 302 | } | 
|  | 303 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 304 | void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache ) | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 305 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 306 | mbedtls_ssl_cache_entry *cur, *prv; | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 307 |  | 
|  | 308 | cur = cache->chain; | 
|  | 309 |  | 
|  | 310 | while( cur != NULL ) | 
|  | 311 | { | 
|  | 312 | prv = cur; | 
|  | 313 | cur = cur->next; | 
|  | 314 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 315 | mbedtls_ssl_session_free( &prv->session ); | 
| Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 316 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 317 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 318 | mbedtls_free( prv->peer_cert.p ); | 
|  | 319 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 320 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 321 | mbedtls_free( prv ); | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 322 | } | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 323 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 324 | #if defined(MBEDTLS_THREADING_C) | 
|  | 325 | mbedtls_mutex_free( &cache->mutex ); | 
| Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 326 | #endif | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 327 | } | 
|  | 328 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 329 | #endif /* MBEDTLS_SSL_CACHE_C */ |