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 | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 28 | #include "mbedtls/platform.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 29 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 30 | #include "mbedtls/ssl_cache.h" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 31 | #include "ssl_misc.h" |
Pengyu Lv | b189589 | 2023-03-16 11:38:43 +0800 | [diff] [blame] | 32 | #include "mbedtls/error.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 33 | |
| 34 | #include <string.h> |
| 35 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 36 | void mbedtls_ssl_cache_init(mbedtls_ssl_cache_context *cache) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 37 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | memset(cache, 0, sizeof(mbedtls_ssl_cache_context)); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 39 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT; |
| 41 | cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES; |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 42 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 44 | mbedtls_mutex_init(&cache->mutex); |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 45 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 48 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | static int ssl_cache_find_entry(mbedtls_ssl_cache_context *cache, |
| 50 | unsigned char const *session_id, |
| 51 | size_t session_id_len, |
| 52 | mbedtls_ssl_cache_entry **dst) |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 53 | { |
Pengyu Lv | e3746d7 | 2023-04-10 14:40:03 +0800 | [diff] [blame] | 54 | int ret = MBEDTLS_ERR_SSL_CACHE_ENTRY_NOT_FOUND; |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 55 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | mbedtls_time_t t = mbedtls_time(NULL); |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 57 | #endif |
| 58 | mbedtls_ssl_cache_entry *cur; |
| 59 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 60 | for (cur = cache->chain; cur != NULL; cur = cur->next) { |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 61 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 62 | if (cache->timeout != 0 && |
| 63 | (int) (t - cur->timestamp) > cache->timeout) { |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 64 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 65 | } |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 66 | #endif |
| 67 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 68 | if (session_id_len != cur->session_id_len || |
| 69 | memcmp(session_id, cur->session_id, |
| 70 | cur->session_id_len) != 0) { |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 71 | continue; |
| 72 | } |
| 73 | |
| 74 | break; |
| 75 | } |
| 76 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 77 | if (cur != NULL) { |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 78 | *dst = cur; |
| 79 | ret = 0; |
| 80 | } |
| 81 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | return ret; |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | int mbedtls_ssl_cache_get(void *data, |
| 87 | unsigned char const *session_id, |
| 88 | size_t session_id_len, |
| 89 | mbedtls_ssl_session *session) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 90 | { |
Pengyu Lv | 5038a38 | 2023-03-23 15:49:52 +0800 | [diff] [blame] | 91 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data; |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 93 | mbedtls_ssl_cache_entry *entry; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 94 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 95 | #if defined(MBEDTLS_THREADING_C) |
Pengyu Lv | 0b9c012 | 2023-03-15 14:37:32 +0800 | [diff] [blame] | 96 | if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) { |
| 97 | return ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | } |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 99 | #endif |
| 100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 101 | ret = ssl_cache_find_entry(cache, session_id, session_id_len, &entry); |
| 102 | if (ret != 0) { |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 103 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 104 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 106 | ret = mbedtls_ssl_session_load(session, |
| 107 | entry->session, |
| 108 | entry->session_len); |
| 109 | if (ret != 0) { |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 110 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 111 | } |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 112 | |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame] | 113 | ret = 0; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 114 | |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 115 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 117 | if (mbedtls_mutex_unlock(&cache->mutex) != 0) { |
Pengyu Lv | 0b9c012 | 2023-03-15 14:37:32 +0800 | [diff] [blame] | 118 | ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 119 | } |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 120 | #endif |
| 121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 122 | return ret; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Pengyu Lv | 744b507 | 2023-03-15 12:17:14 +0800 | [diff] [blame] | 125 | /* zeroize a cache entry */ |
| 126 | static void ssl_cache_entry_zeroize(mbedtls_ssl_cache_entry *entry) |
| 127 | { |
| 128 | if (entry == NULL) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | /* zeroize and free session structure */ |
| 133 | if (entry->session != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 134 | mbedtls_zeroize_and_free(entry->session, entry->session_len); |
Pengyu Lv | 744b507 | 2023-03-15 12:17:14 +0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* zeroize the whole entry structure */ |
| 138 | mbedtls_platform_zeroize(entry, sizeof(mbedtls_ssl_cache_entry)); |
| 139 | } |
| 140 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 141 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | static int ssl_cache_pick_writing_slot(mbedtls_ssl_cache_context *cache, |
| 143 | unsigned char const *session_id, |
| 144 | size_t session_id_len, |
| 145 | mbedtls_ssl_cache_entry **dst) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 146 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 148 | mbedtls_time_t t = mbedtls_time(NULL), oldest = 0; |
Hanno Becker | 006f2cc | 2021-05-14 04:55:35 +0100 | [diff] [blame] | 149 | #endif /* MBEDTLS_HAVE_TIME */ |
| 150 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | mbedtls_ssl_cache_entry *old = NULL; |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 152 | int count = 0; |
Hanno Becker | 466ed6f | 2021-05-14 14:54:00 +0100 | [diff] [blame] | 153 | mbedtls_ssl_cache_entry *cur, *last; |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 154 | |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 155 | /* Check 1: Is there already an entry with the given session ID? |
| 156 | * |
| 157 | * If yes, overwrite it. |
| 158 | * |
| 159 | * If not, `count` will hold the size of the session cache |
Hanno Becker | 466ed6f | 2021-05-14 14:54:00 +0100 | [diff] [blame] | 160 | * 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] | 161 | * entry, both of which will be used later. */ |
| 162 | |
Hanno Becker | 78196e3 | 2021-05-14 14:45:38 +0100 | [diff] [blame] | 163 | last = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 164 | for (cur = cache->chain; cur != NULL; cur = cur->next) { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 165 | count++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 166 | if (session_id_len == cur->session_id_len && |
| 167 | memcmp(session_id, cur->session_id, cur->session_id_len) == 0) { |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 168 | goto found; |
Hanno Becker | ccdaf6e | 2021-04-15 09:26:17 +0100 | [diff] [blame] | 169 | } |
Hanno Becker | 78196e3 | 2021-05-14 14:45:38 +0100 | [diff] [blame] | 170 | last = cur; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 173 | /* Check 2: Is there an outdated entry in the cache? |
| 174 | * |
| 175 | * If so, overwrite it. |
| 176 | * |
| 177 | * If not, remember the oldest entry in `old` for later. |
| 178 | */ |
| 179 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 180 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 181 | for (cur = cache->chain; cur != NULL; cur = cur->next) { |
| 182 | if (cache->timeout != 0 && |
| 183 | (int) (t - cur->timestamp) > cache->timeout) { |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 184 | goto found; |
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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 187 | if (oldest == 0 || cur->timestamp < oldest) { |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 188 | oldest = cur->timestamp; |
| 189 | old = cur; |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 190 | } |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 191 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | #endif /* MBEDTLS_HAVE_TIME */ |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 193 | |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 194 | /* Check 3: Is there free space in the cache? */ |
| 195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 196 | if (count < cache->max_entries) { |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 197 | /* Create new entry */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 198 | cur = mbedtls_calloc(1, sizeof(mbedtls_ssl_cache_entry)); |
| 199 | if (cur == NULL) { |
Pengyu Lv | 5038a38 | 2023-03-23 15:49:52 +0800 | [diff] [blame] | 200 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 201 | } |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 202 | |
| 203 | /* Append to the end of the linked list. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 204 | if (last == NULL) { |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 205 | cache->chain = cur; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | } else { |
Hanno Becker | 466ed6f | 2021-05-14 14:54:00 +0100 | [diff] [blame] | 207 | last->next = cur; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 208 | } |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 209 | |
| 210 | goto found; |
| 211 | } |
| 212 | |
| 213 | /* Last resort: The cache is full and doesn't contain any outdated |
| 214 | * elements. In this case, we evict the oldest one, judged by timestamp |
| 215 | * (if present) or cache-order. */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 216 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 217 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 218 | if (old == NULL) { |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 219 | /* This should only happen on an ill-configured cache |
| 220 | * with max_entries == 0. */ |
Pengyu Lv | 5038a38 | 2023-03-23 15:49:52 +0800 | [diff] [blame] | 221 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 222 | } |
| 223 | #else /* MBEDTLS_HAVE_TIME */ |
| 224 | /* Reuse first entry in chain, but move to last place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 225 | if (cache->chain == NULL) { |
Pengyu Lv | 5038a38 | 2023-03-23 15:49:52 +0800 | [diff] [blame] | 226 | /* This should never happen */ |
| 227 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 228 | } |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 229 | |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 230 | old = cache->chain; |
| 231 | cache->chain = old->next; |
Hanno Becker | 5cf6f7e | 2021-05-14 14:45:04 +0100 | [diff] [blame] | 232 | old->next = NULL; |
Hanno Becker | 466ed6f | 2021-05-14 14:54:00 +0100 | [diff] [blame] | 233 | last->next = old; |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 234 | #endif /* MBEDTLS_HAVE_TIME */ |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 235 | |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 236 | /* Now `old` points to the oldest entry to be overwritten. */ |
| 237 | cur = old; |
| 238 | |
| 239 | found: |
| 240 | |
Pengyu Lv | 744b507 | 2023-03-15 12:17:14 +0800 | [diff] [blame] | 241 | /* If we're reusing an entry, free it first. */ |
| 242 | if (cur->session != NULL) { |
| 243 | /* `ssl_cache_entry_zeroize` would break the chain, |
| 244 | * so we reuse `old` to record `next` temporarily. */ |
| 245 | old = cur->next; |
| 246 | ssl_cache_entry_zeroize(cur); |
| 247 | cur->next = old; |
| 248 | } |
| 249 | |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 250 | #if defined(MBEDTLS_HAVE_TIME) |
| 251 | cur->timestamp = t; |
| 252 | #endif |
| 253 | |
Hanno Becker | 845ceb7 | 2021-05-13 07:05:07 +0100 | [diff] [blame] | 254 | *dst = cur; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | return 0; |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 258 | int mbedtls_ssl_cache_set(void *data, |
| 259 | unsigned char const *session_id, |
| 260 | size_t session_id_len, |
| 261 | const mbedtls_ssl_session *session) |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 262 | { |
Pengyu Lv | 5038a38 | 2023-03-23 15:49:52 +0800 | [diff] [blame] | 263 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 264 | mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data; |
| 265 | mbedtls_ssl_cache_entry *cur; |
| 266 | |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 267 | size_t session_serialized_len; |
| 268 | unsigned char *session_serialized = NULL; |
| 269 | |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 270 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 271 | if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) { |
| 272 | return ret; |
| 273 | } |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 274 | #endif |
| 275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | ret = ssl_cache_pick_writing_slot(cache, |
| 277 | session_id, session_id_len, |
| 278 | &cur); |
| 279 | if (ret != 0) { |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 280 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 281 | } |
Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 282 | |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 283 | /* Check how much space we need to serialize the session |
| 284 | * and allocate a sufficiently large buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 285 | ret = mbedtls_ssl_session_save(session, NULL, 0, &session_serialized_len); |
| 286 | if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
Hanno Becker | aee8717 | 2019-02-06 14:53:19 +0000 | [diff] [blame] | 287 | goto exit; |
| 288 | } |
| 289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 290 | session_serialized = mbedtls_calloc(1, session_serialized_len); |
| 291 | if (session_serialized == NULL) { |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 292 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 293 | goto exit; |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 294 | } |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 295 | |
| 296 | /* Now serialize the session into the allocated buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 297 | ret = mbedtls_ssl_session_save(session, |
| 298 | session_serialized, |
| 299 | session_serialized_len, |
| 300 | &session_serialized_len); |
| 301 | if (ret != 0) { |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 302 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | } |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 305 | if (session_id_len > sizeof(cur->session_id)) { |
Pengyu Lv | 5038a38 | 2023-03-23 15:49:52 +0800 | [diff] [blame] | 306 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 307 | goto exit; |
| 308 | } |
| 309 | cur->session_id_len = session_id_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | memcpy(cur->session_id, session_id, session_id_len); |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 311 | |
| 312 | cur->session = session_serialized; |
| 313 | cur->session_len = session_serialized_len; |
| 314 | session_serialized = NULL; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 315 | |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 316 | ret = 0; |
| 317 | |
| 318 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 319 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 320 | if (mbedtls_mutex_unlock(&cache->mutex) != 0) { |
Pengyu Lv | 0b9c012 | 2023-03-15 14:37:32 +0800 | [diff] [blame] | 321 | ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 322 | } |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 323 | #endif |
| 324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 325 | if (session_serialized != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 326 | mbedtls_zeroize_and_free(session_serialized, session_serialized_len); |
Leonid Rozenboim | 116f50c | 2022-04-21 13:05:10 -0700 | [diff] [blame] | 327 | session_serialized = NULL; |
| 328 | } |
Hanno Becker | 7e6eb9f | 2021-04-15 10:26:06 +0100 | [diff] [blame] | 329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 330 | return ret; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Pengyu Lv | 7b6299b | 2023-03-07 14:38:45 +0800 | [diff] [blame] | 333 | int mbedtls_ssl_cache_remove(void *data, |
| 334 | unsigned char const *session_id, |
| 335 | size_t session_id_len) |
| 336 | { |
Pengyu Lv | 5038a38 | 2023-03-23 15:49:52 +0800 | [diff] [blame] | 337 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Pengyu Lv | 7b6299b | 2023-03-07 14:38:45 +0800 | [diff] [blame] | 338 | mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data; |
| 339 | mbedtls_ssl_cache_entry *entry; |
| 340 | mbedtls_ssl_cache_entry *prev; |
| 341 | |
| 342 | #if defined(MBEDTLS_THREADING_C) |
Pengyu Lv | 0b9c012 | 2023-03-15 14:37:32 +0800 | [diff] [blame] | 343 | if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) { |
| 344 | return ret; |
Pengyu Lv | 7b6299b | 2023-03-07 14:38:45 +0800 | [diff] [blame] | 345 | } |
| 346 | #endif |
| 347 | |
| 348 | ret = ssl_cache_find_entry(cache, session_id, session_id_len, &entry); |
| 349 | /* No valid entry found, exit with success */ |
| 350 | if (ret != 0) { |
| 351 | ret = 0; |
| 352 | goto exit; |
| 353 | } |
| 354 | |
| 355 | /* Now we remove the entry from the chain */ |
| 356 | if (entry == cache->chain) { |
| 357 | cache->chain = entry->next; |
| 358 | goto free; |
| 359 | } |
| 360 | for (prev = cache->chain; prev->next != NULL; prev = prev->next) { |
| 361 | if (prev->next == entry) { |
| 362 | prev->next = entry->next; |
| 363 | break; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | free: |
Pengyu Lv | 744b507 | 2023-03-15 12:17:14 +0800 | [diff] [blame] | 368 | ssl_cache_entry_zeroize(entry); |
Pengyu Lv | 7b6299b | 2023-03-07 14:38:45 +0800 | [diff] [blame] | 369 | mbedtls_free(entry); |
| 370 | ret = 0; |
| 371 | |
| 372 | exit: |
| 373 | #if defined(MBEDTLS_THREADING_C) |
| 374 | if (mbedtls_mutex_unlock(&cache->mutex) != 0) { |
Pengyu Lv | 0b9c012 | 2023-03-15 14:37:32 +0800 | [diff] [blame] | 375 | ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
Pengyu Lv | 7b6299b | 2023-03-07 14:38:45 +0800 | [diff] [blame] | 376 | } |
| 377 | #endif |
| 378 | |
| 379 | return ret; |
| 380 | } |
| 381 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 382 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 383 | 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] | 384 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 385 | if (timeout < 0) { |
| 386 | timeout = 0; |
| 387 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 388 | |
| 389 | cache->timeout = timeout; |
| 390 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | #endif /* MBEDTLS_HAVE_TIME */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 393 | 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] | 394 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | if (max < 0) { |
| 396 | max = 0; |
| 397 | } |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 398 | |
| 399 | cache->max_entries = max; |
| 400 | } |
| 401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 402 | void mbedtls_ssl_cache_free(mbedtls_ssl_cache_context *cache) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 403 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 404 | mbedtls_ssl_cache_entry *cur, *prv; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 405 | |
| 406 | cur = cache->chain; |
| 407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 408 | while (cur != NULL) { |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 409 | prv = cur; |
| 410 | cur = cur->next; |
| 411 | |
Pengyu Lv | 744b507 | 2023-03-15 12:17:14 +0800 | [diff] [blame] | 412 | ssl_cache_entry_zeroize(prv); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 413 | mbedtls_free(prv); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 414 | } |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 415 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 416 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | mbedtls_mutex_free(&cache->mutex); |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 418 | #endif |
Ron Eldor | 2236082 | 2017-10-29 17:53:52 +0200 | [diff] [blame] | 419 | cache->chain = NULL; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 422 | #endif /* MBEDTLS_SSL_CACHE_C */ |