Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL session cache implementation |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 18 | */ |
| 19 | /* |
| 20 | * These session callbacks use a simple chained list |
| 21 | * to store and retrieve the session information. |
| 22 | */ |
| 23 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 24 | #include "common.h" |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_SSL_CACHE_C) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/platform.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 30 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 32 | #define mbedtls_calloc calloc |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 33 | #define mbedtls_free free |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 34 | #endif |
| 35 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 36 | #include "mbedtls/ssl_cache.h" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 37 | #include "ssl_misc.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 38 | |
| 39 | #include <string.h> |
| 40 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache ) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 42 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 44 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT; |
| 46 | cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES; |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 47 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | #if defined(MBEDTLS_THREADING_C) |
| 49 | mbedtls_mutex_init( &cache->mutex ); |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 50 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame^] | 53 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 54 | static int ssl_cache_find_entry( mbedtls_ssl_cache_context *cache, |
| 55 | unsigned char const *session_id, |
| 56 | size_t session_id_len, |
| 57 | mbedtls_ssl_cache_entry **dst ) |
| 58 | { |
| 59 | int ret = 1; |
| 60 | #if defined(MBEDTLS_HAVE_TIME) |
| 61 | mbedtls_time_t t = mbedtls_time( NULL ); |
| 62 | #endif |
| 63 | mbedtls_ssl_cache_entry *cur; |
| 64 | |
| 65 | for( cur = cache->chain; cur != NULL; cur = cur->next ) |
| 66 | { |
| 67 | #if defined(MBEDTLS_HAVE_TIME) |
| 68 | if( cache->timeout != 0 && |
| 69 | (int) ( t - cur->timestamp ) > cache->timeout ) |
| 70 | continue; |
| 71 | #endif |
| 72 | |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 73 | if( session_id_len != cur->session_id_len || |
| 74 | memcmp( session_id, cur->session_id, |
| 75 | cur->session_id_len ) != 0 ) |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 76 | { |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | if( cur != NULL ) |
| 84 | { |
| 85 | *dst = cur; |
| 86 | ret = 0; |
| 87 | } |
| 88 | |
| 89 | return( ret ); |
| 90 | } |
| 91 | |
| 92 | |
Hanno Becker | ccdaf6e | 2021-04-15 09:26:17 +0100 | [diff] [blame] | 93 | int mbedtls_ssl_cache_get( void *data, |
| 94 | unsigned char const *session_id, |
| 95 | size_t session_id_len, |
| 96 | mbedtls_ssl_session *session ) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 97 | { |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 98 | int ret = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data; |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 100 | mbedtls_ssl_cache_entry *entry; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 101 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 102 | #if defined(MBEDTLS_THREADING_C) |
| 103 | if( mbedtls_mutex_lock( &cache->mutex ) != 0 ) |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 104 | return( 1 ); |
| 105 | #endif |
| 106 | |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 107 | ret = ssl_cache_find_entry( cache, session_id, session_id_len, &entry ); |
| 108 | if( ret != 0 ) |
| 109 | goto exit; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 110 | |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 111 | ret = mbedtls_ssl_session_load( session, |
| 112 | entry->session, |
| 113 | entry->session_len ); |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 114 | if( ret != 0 ) |
| 115 | goto exit; |
| 116 | |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 117 | ret = 0; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 118 | |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 119 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 120 | #if defined(MBEDTLS_THREADING_C) |
| 121 | if( mbedtls_mutex_unlock( &cache->mutex ) != 0 ) |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 122 | ret = 1; |
| 123 | #endif |
| 124 | |
| 125 | return( ret ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame^] | 128 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | f47199d | 2021-05-13 06:29:13 +0100 | [diff] [blame] | 129 | static int ssl_cache_pick_writing_slot( mbedtls_ssl_cache_context *cache, |
| 130 | unsigned char const *session_id, |
| 131 | size_t session_id_len, |
| 132 | mbedtls_ssl_cache_entry **dst ) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 133 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 134 | #if defined(MBEDTLS_HAVE_TIME) |
Simon Butcher | a55e084 | 2017-07-28 23:46:43 +0100 | [diff] [blame] | 135 | mbedtls_time_t t = mbedtls_time( NULL ), oldest = 0; |
Hanno Becker | 006f2cc | 2021-05-14 04:55:35 +0100 | [diff] [blame] | 136 | #endif /* MBEDTLS_HAVE_TIME */ |
| 137 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 138 | mbedtls_ssl_cache_entry *old = NULL; |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 139 | int count = 0; |
Hanno Becker | 466ed6f | 2021-05-14 14:54:00 +0100 | [diff] [blame] | 140 | mbedtls_ssl_cache_entry *cur, *last; |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 141 | |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 142 | /* Check 1: Is there already an entry with the given session ID? |
| 143 | * |
| 144 | * If yes, overwrite it. |
| 145 | * |
| 146 | * If not, `count` will hold the size of the session cache |
Hanno Becker | 466ed6f | 2021-05-14 14:54:00 +0100 | [diff] [blame] | 147 | * at the end of this loop, and `last` will point to the last |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 148 | * entry, both of which will be used later. */ |
| 149 | |
Hanno Becker | 78196e3 | 2021-05-14 14:45:38 +0100 | [diff] [blame] | 150 | last = NULL; |
| 151 | for( cur = cache->chain; cur != NULL; cur = cur->next ) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 152 | { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 153 | count++; |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 154 | if( session_id_len == cur->session_id_len && |
| 155 | memcmp( session_id, cur->session_id, cur->session_id_len ) == 0 ) |
Hanno Becker | ccdaf6e | 2021-04-15 09:26:17 +0100 | [diff] [blame] | 156 | { |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 157 | goto found; |
Hanno Becker | ccdaf6e | 2021-04-15 09:26:17 +0100 | [diff] [blame] | 158 | } |
Hanno Becker | 78196e3 | 2021-05-14 14:45:38 +0100 | [diff] [blame] | 159 | last = cur; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 162 | /* Check 2: Is there an outdated entry in the cache? |
| 163 | * |
| 164 | * If so, overwrite it. |
| 165 | * |
| 166 | * If not, remember the oldest entry in `old` for later. |
| 167 | */ |
| 168 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 169 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 78196e3 | 2021-05-14 14:45:38 +0100 | [diff] [blame] | 170 | for( cur = cache->chain; cur != NULL; cur = cur->next ) |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 171 | { |
| 172 | if( cache->timeout != 0 && |
| 173 | (int) ( t - cur->timestamp ) > cache->timeout ) |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 174 | { |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 175 | goto found; |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 176 | } |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 177 | |
| 178 | if( oldest == 0 || cur->timestamp < oldest ) |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 179 | { |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 180 | oldest = cur->timestamp; |
| 181 | old = cur; |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 182 | } |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 183 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 184 | #endif /* MBEDTLS_HAVE_TIME */ |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 185 | |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 186 | /* Check 3: Is there free space in the cache? */ |
| 187 | |
| 188 | if( count < cache->max_entries ) |
| 189 | { |
| 190 | /* Create new entry */ |
| 191 | cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) ); |
| 192 | if( cur == NULL ) |
| 193 | return( 1 ); |
| 194 | |
| 195 | /* Append to the end of the linked list. */ |
Hanno Becker | 466ed6f | 2021-05-14 14:54:00 +0100 | [diff] [blame] | 196 | if( last == NULL ) |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 197 | cache->chain = cur; |
| 198 | else |
Hanno Becker | 466ed6f | 2021-05-14 14:54:00 +0100 | [diff] [blame] | 199 | last->next = cur; |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 200 | |
| 201 | goto found; |
| 202 | } |
| 203 | |
| 204 | /* Last resort: The cache is full and doesn't contain any outdated |
| 205 | * elements. In this case, we evict the oldest one, judged by timestamp |
| 206 | * (if present) or cache-order. */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 207 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 209 | if( old == NULL ) |
Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 210 | { |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 211 | /* This should only happen on an ill-configured cache |
| 212 | * with max_entries == 0. */ |
| 213 | return( 1 ); |
| 214 | } |
| 215 | #else /* MBEDTLS_HAVE_TIME */ |
| 216 | /* Reuse first entry in chain, but move to last place. */ |
| 217 | if( cache->chain == NULL ) |
| 218 | return( 1 ); |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 219 | |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 220 | old = cache->chain; |
| 221 | cache->chain = old->next; |
Hanno Becker | 5cf6f7e | 2021-05-14 14:45:04 +0100 | [diff] [blame] | 222 | old->next = NULL; |
Hanno Becker | 466ed6f | 2021-05-14 14:54:00 +0100 | [diff] [blame] | 223 | last->next = old; |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 224 | #endif /* MBEDTLS_HAVE_TIME */ |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 225 | |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 226 | /* Now `old` points to the oldest entry to be overwritten. */ |
| 227 | cur = old; |
| 228 | |
| 229 | found: |
| 230 | |
| 231 | #if defined(MBEDTLS_HAVE_TIME) |
| 232 | cur->timestamp = t; |
| 233 | #endif |
| 234 | |
| 235 | /* If we're reusing an entry, free it first. */ |
| 236 | if( cur->session != NULL ) |
| 237 | { |
| 238 | mbedtls_free( cur->session ); |
| 239 | cur->session = NULL; |
| 240 | cur->session_len = 0; |
| 241 | memset( cur->session_id, 0, sizeof( cur->session_id ) ); |
| 242 | cur->session_id_len = 0; |
Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 243 | } |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 244 | |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 245 | *dst = cur; |
| 246 | return( 0 ); |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | int mbedtls_ssl_cache_set( void *data, |
| 250 | unsigned char const *session_id, |
| 251 | size_t session_id_len, |
| 252 | const mbedtls_ssl_session *session ) |
| 253 | { |
| 254 | int ret = 1; |
| 255 | mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data; |
| 256 | mbedtls_ssl_cache_entry *cur; |
| 257 | |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 258 | size_t session_serialized_len; |
| 259 | unsigned char *session_serialized = NULL; |
| 260 | |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 261 | #if defined(MBEDTLS_THREADING_C) |
| 262 | if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 ) |
| 263 | return( ret ); |
| 264 | #endif |
| 265 | |
Hanno Becker | f47199d | 2021-05-13 06:29:13 +0100 | [diff] [blame] | 266 | ret = ssl_cache_pick_writing_slot( cache, |
| 267 | session_id, session_id_len, |
| 268 | &cur ); |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 269 | if( ret != 0 ) |
| 270 | goto exit; |
Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 271 | |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 272 | /* Check how much space we need to serialize the session |
| 273 | * and allocate a sufficiently large buffer. */ |
| 274 | ret = mbedtls_ssl_session_save( session, NULL, 0, &session_serialized_len ); |
| 275 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 276 | { |
Hanno Becker | aee8717 | 2019-02-06 14:53:19 +0000 | [diff] [blame] | 277 | ret = 1; |
| 278 | goto exit; |
| 279 | } |
| 280 | |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 281 | session_serialized = mbedtls_calloc( 1, session_serialized_len ); |
| 282 | if( session_serialized == NULL ) |
Hanno Becker | aee8717 | 2019-02-06 14:53:19 +0000 | [diff] [blame] | 283 | { |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 284 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 285 | goto exit; |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 286 | } |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 287 | |
| 288 | /* Now serialize the session into the allocated buffer. */ |
| 289 | ret = mbedtls_ssl_session_save( session, |
| 290 | session_serialized, |
| 291 | session_serialized_len, |
| 292 | &session_serialized_len ); |
| 293 | if( ret != 0 ) |
| 294 | goto exit; |
| 295 | |
Hanno Becker | aee4cc4 | 2021-04-15 16:49:32 +0100 | [diff] [blame] | 296 | if( session_id_len > sizeof( cur->session_id ) ) |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 297 | { |
| 298 | ret = 1; |
| 299 | goto exit; |
| 300 | } |
| 301 | cur->session_id_len = session_id_len; |
| 302 | memcpy( cur->session_id, session_id, session_id_len ); |
| 303 | |
| 304 | cur->session = session_serialized; |
| 305 | cur->session_len = session_serialized_len; |
| 306 | session_serialized = NULL; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 307 | |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 308 | ret = 0; |
| 309 | |
| 310 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 311 | #if defined(MBEDTLS_THREADING_C) |
| 312 | if( mbedtls_mutex_unlock( &cache->mutex ) != 0 ) |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 313 | ret = 1; |
| 314 | #endif |
| 315 | |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 316 | if( session_serialized != NULL ) |
| 317 | mbedtls_platform_zeroize( session_serialized, session_serialized_len ); |
| 318 | |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 319 | return( ret ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 322 | #if defined(MBEDTLS_HAVE_TIME) |
| 323 | 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] | 324 | { |
| 325 | if( timeout < 0 ) timeout = 0; |
| 326 | |
| 327 | cache->timeout = timeout; |
| 328 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 329 | #endif /* MBEDTLS_HAVE_TIME */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 330 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 331 | 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] | 332 | { |
| 333 | if( max < 0 ) max = 0; |
| 334 | |
| 335 | cache->max_entries = max; |
| 336 | } |
| 337 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 338 | void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache ) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 339 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 340 | mbedtls_ssl_cache_entry *cur, *prv; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 341 | |
| 342 | cur = cache->chain; |
| 343 | |
| 344 | while( cur != NULL ) |
| 345 | { |
| 346 | prv = cur; |
| 347 | cur = cur->next; |
| 348 | |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 349 | mbedtls_free( prv->session ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | mbedtls_free( prv ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 351 | } |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 352 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 353 | #if defined(MBEDTLS_THREADING_C) |
| 354 | mbedtls_mutex_free( &cache->mutex ); |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 355 | #endif |
Ron Eldor | 2236082 | 2017-10-29 17:53:52 +0200 | [diff] [blame] | 356 | cache->chain = NULL; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 359 | #endif /* MBEDTLS_SSL_CACHE_C */ |