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