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