blob: 28d0cfbb7d6377bbb5707d334d8d208186018bdc [file] [log] [blame]
Paul Bakker0a597072012-09-25 21:55:46 +00001/*
2 * SSL session cache implementation
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker0a597072012-09-25 21:55:46 +00006 */
7/*
8 * These session callbacks use a simple chained list
9 * to store and retrieve the session information.
10 */
11
Harry Ramsey0f6bc412024-10-04 10:36:54 +010012#include "ssl_misc.h"
Paul Bakker0a597072012-09-25 21:55:46 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_SSL_CACHE_C)
Paul Bakker0a597072012-09-25 21:55:46 +000015
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000016#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020017
SimonBd5800b72016-04-26 07:43:27 +010018#include "mbedtls/ssl_cache.h"
Pengyu Lvb1895892023-03-16 11:38:43 +080019#include "mbedtls/error.h"
SimonBd5800b72016-04-26 07:43:27 +010020
21#include <string.h>
22
Gilles Peskine449bd832023-01-11 14:50:10 +010023void mbedtls_ssl_cache_init(mbedtls_ssl_cache_context *cache)
Paul Bakker0a597072012-09-25 21:55:46 +000024{
Gilles Peskine449bd832023-01-11 14:50:10 +010025 memset(cache, 0, sizeof(mbedtls_ssl_cache_context));
Paul Bakker0a597072012-09-25 21:55:46 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027 cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
28 cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
Paul Bakkerc5598842013-09-28 15:01:27 +020029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010031 mbedtls_mutex_init(&cache->mutex);
Paul Bakkerc5598842013-09-28 15:01:27 +020032#endif
Paul Bakker0a597072012-09-25 21:55:46 +000033}
34
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020035MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010036static int ssl_cache_find_entry(mbedtls_ssl_cache_context *cache,
37 unsigned char const *session_id,
38 size_t session_id_len,
39 mbedtls_ssl_cache_entry **dst)
Hanno Beckerf938c432021-04-15 10:17:53 +010040{
Pengyu Lve3746d72023-04-10 14:40:03 +080041 int ret = MBEDTLS_ERR_SSL_CACHE_ENTRY_NOT_FOUND;
Hanno Beckerf938c432021-04-15 10:17:53 +010042#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +010043 mbedtls_time_t t = mbedtls_time(NULL);
Hanno Beckerf938c432021-04-15 10:17:53 +010044#endif
45 mbedtls_ssl_cache_entry *cur;
46
Gilles Peskine449bd832023-01-11 14:50:10 +010047 for (cur = cache->chain; cur != NULL; cur = cur->next) {
Hanno Beckerf938c432021-04-15 10:17:53 +010048#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +010049 if (cache->timeout != 0 &&
50 (int) (t - cur->timestamp) > cache->timeout) {
Hanno Beckerf938c432021-04-15 10:17:53 +010051 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +010052 }
Hanno Beckerf938c432021-04-15 10:17:53 +010053#endif
54
Gilles Peskine449bd832023-01-11 14:50:10 +010055 if (session_id_len != cur->session_id_len ||
56 memcmp(session_id, cur->session_id,
57 cur->session_id_len) != 0) {
Hanno Beckerf938c432021-04-15 10:17:53 +010058 continue;
59 }
60
61 break;
62 }
63
Gilles Peskine449bd832023-01-11 14:50:10 +010064 if (cur != NULL) {
Hanno Beckerf938c432021-04-15 10:17:53 +010065 *dst = cur;
66 ret = 0;
67 }
68
Gilles Peskine449bd832023-01-11 14:50:10 +010069 return ret;
Hanno Beckerf938c432021-04-15 10:17:53 +010070}
71
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073int mbedtls_ssl_cache_get(void *data,
74 unsigned char const *session_id,
75 size_t session_id_len,
76 mbedtls_ssl_session *session)
Paul Bakker0a597072012-09-25 21:55:46 +000077{
Pengyu Lv5038a382023-03-23 15:49:52 +080078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
Hanno Beckerf938c432021-04-15 10:17:53 +010080 mbedtls_ssl_cache_entry *entry;
Paul Bakker0a597072012-09-25 21:55:46 +000081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_THREADING_C)
Pengyu Lv0b9c0122023-03-15 14:37:32 +080083 if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) {
84 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +010085 }
Paul Bakkerc5598842013-09-28 15:01:27 +020086#endif
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088 ret = ssl_cache_find_entry(cache, session_id, session_id_len, &entry);
89 if (ret != 0) {
Hanno Beckerf938c432021-04-15 10:17:53 +010090 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010091 }
Paul Bakker0a597072012-09-25 21:55:46 +000092
Gilles Peskine449bd832023-01-11 14:50:10 +010093 ret = mbedtls_ssl_session_load(session,
94 entry->session,
95 entry->session_len);
96 if (ret != 0) {
Hanno Beckerf938c432021-04-15 10:17:53 +010097 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010098 }
Hanno Beckerf938c432021-04-15 10:17:53 +010099
Hanno Beckerf938c432021-04-15 10:17:53 +0100100 ret = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000101
Paul Bakkerc5598842013-09-28 15:01:27 +0200102exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 if (mbedtls_mutex_unlock(&cache->mutex) != 0) {
Pengyu Lv0b9c0122023-03-15 14:37:32 +0800105 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200107#endif
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 return ret;
Paul Bakker0a597072012-09-25 21:55:46 +0000110}
111
Pengyu Lv744b5072023-03-15 12:17:14 +0800112/* zeroize a cache entry */
113static void ssl_cache_entry_zeroize(mbedtls_ssl_cache_entry *entry)
114{
115 if (entry == NULL) {
116 return;
117 }
118
119 /* zeroize and free session structure */
120 if (entry->session != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100121 mbedtls_zeroize_and_free(entry->session, entry->session_len);
Pengyu Lv744b5072023-03-15 12:17:14 +0800122 }
123
124 /* zeroize the whole entry structure */
125 mbedtls_platform_zeroize(entry, sizeof(mbedtls_ssl_cache_entry));
126}
127
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200128MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100129static 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 Bakker0a597072012-09-25 21:55:46 +0000133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 mbedtls_time_t t = mbedtls_time(NULL), oldest = 0;
Hanno Becker006f2cc2021-05-14 04:55:35 +0100136#endif /* MBEDTLS_HAVE_TIME */
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_ssl_cache_entry *old = NULL;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000139 int count = 0;
Hanno Becker466ed6f2021-05-14 14:54:00 +0100140 mbedtls_ssl_cache_entry *cur, *last;
Paul Bakkerc5598842013-09-28 15:01:27 +0200141
Hanno Becker845ceb72021-05-13 07:05:07 +0100142 /* 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 Becker466ed6f2021-05-14 14:54:00 +0100147 * at the end of this loop, and `last` will point to the last
Hanno Becker845ceb72021-05-13 07:05:07 +0100148 * entry, both of which will be used later. */
149
Hanno Becker78196e32021-05-14 14:45:38 +0100150 last = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 for (cur = cache->chain; cur != NULL; cur = cur->next) {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000152 count++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (session_id_len == cur->session_id_len &&
154 memcmp(session_id, cur->session_id, cur->session_id_len) == 0) {
Hanno Becker845ceb72021-05-13 07:05:07 +0100155 goto found;
Hanno Beckerccdaf6e2021-04-15 09:26:17 +0100156 }
Hanno Becker78196e32021-05-14 14:45:38 +0100157 last = cur;
Paul Bakker0a597072012-09-25 21:55:46 +0000158 }
159
Hanno Becker845ceb72021-05-13 07:05:07 +0100160 /* Check 2: Is there an outdated entry in the cache?
161 *
162 * If so, overwrite it.
163 *
164 * If not, remember the oldest entry in `old` for later.
165 */
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 for (cur = cache->chain; cur != NULL; cur = cur->next) {
169 if (cache->timeout != 0 &&
170 (int) (t - cur->timestamp) > cache->timeout) {
Hanno Becker845ceb72021-05-13 07:05:07 +0100171 goto found;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000172 }
Hanno Becker845ceb72021-05-13 07:05:07 +0100173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 if (oldest == 0 || cur->timestamp < oldest) {
Hanno Becker845ceb72021-05-13 07:05:07 +0100175 oldest = cur->timestamp;
176 old = cur;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200177 }
Hanno Becker845ceb72021-05-13 07:05:07 +0100178 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#endif /* MBEDTLS_HAVE_TIME */
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000180
Hanno Becker845ceb72021-05-13 07:05:07 +0100181 /* Check 3: Is there free space in the cache? */
182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 if (count < cache->max_entries) {
Hanno Becker845ceb72021-05-13 07:05:07 +0100184 /* Create new entry */
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 cur = mbedtls_calloc(1, sizeof(mbedtls_ssl_cache_entry));
186 if (cur == NULL) {
Pengyu Lv5038a382023-03-23 15:49:52 +0800187 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 }
Hanno Becker845ceb72021-05-13 07:05:07 +0100189
190 /* Append to the end of the linked list. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 if (last == NULL) {
Hanno Becker845ceb72021-05-13 07:05:07 +0100192 cache->chain = cur;
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 } else {
Hanno Becker466ed6f2021-05-14 14:54:00 +0100194 last->next = cur;
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 }
Hanno Becker845ceb72021-05-13 07:05:07 +0100196
197 goto found;
198 }
199
200 /* Last resort: The cache is full and doesn't contain any outdated
201 * elements. In this case, we evict the oldest one, judged by timestamp
202 * (if present) or cache-order. */
Paul Bakker0a597072012-09-25 21:55:46 +0000203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if (old == NULL) {
Hanno Becker845ceb72021-05-13 07:05:07 +0100206 /* This should only happen on an ill-configured cache
207 * with max_entries == 0. */
Pengyu Lv5038a382023-03-23 15:49:52 +0800208 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker845ceb72021-05-13 07:05:07 +0100209 }
210#else /* MBEDTLS_HAVE_TIME */
211 /* Reuse first entry in chain, but move to last place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 if (cache->chain == NULL) {
Pengyu Lv5038a382023-03-23 15:49:52 +0800213 /* This should never happen */
214 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 }
Hanno Becker02a68eb2021-04-15 09:57:17 +0100216
Hanno Becker845ceb72021-05-13 07:05:07 +0100217 old = cache->chain;
218 cache->chain = old->next;
Hanno Becker5cf6f7e2021-05-14 14:45:04 +0100219 old->next = NULL;
Hanno Becker466ed6f2021-05-14 14:54:00 +0100220 last->next = old;
Hanno Becker845ceb72021-05-13 07:05:07 +0100221#endif /* MBEDTLS_HAVE_TIME */
Hanno Becker02a68eb2021-04-15 09:57:17 +0100222
Hanno Becker845ceb72021-05-13 07:05:07 +0100223 /* Now `old` points to the oldest entry to be overwritten. */
224 cur = old;
225
226found:
227
Pengyu Lv744b5072023-03-15 12:17:14 +0800228 /* If we're reusing an entry, free it first. */
229 if (cur->session != NULL) {
230 /* `ssl_cache_entry_zeroize` would break the chain,
231 * so we reuse `old` to record `next` temporarily. */
232 old = cur->next;
233 ssl_cache_entry_zeroize(cur);
234 cur->next = old;
235 }
236
Hanno Becker845ceb72021-05-13 07:05:07 +0100237#if defined(MBEDTLS_HAVE_TIME)
238 cur->timestamp = t;
239#endif
240
Hanno Becker845ceb72021-05-13 07:05:07 +0100241 *dst = cur;
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return 0;
Hanno Becker02a68eb2021-04-15 09:57:17 +0100243}
244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245int mbedtls_ssl_cache_set(void *data,
246 unsigned char const *session_id,
247 size_t session_id_len,
248 const mbedtls_ssl_session *session)
Hanno Becker02a68eb2021-04-15 09:57:17 +0100249{
Pengyu Lv5038a382023-03-23 15:49:52 +0800250 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker02a68eb2021-04-15 09:57:17 +0100251 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
252 mbedtls_ssl_cache_entry *cur;
253
Sergeybef1f632023-03-06 15:25:06 -0700254 size_t session_serialized_len = 0;
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100255 unsigned char *session_serialized = NULL;
256
Hanno Becker02a68eb2021-04-15 09:57:17 +0100257#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) {
259 return ret;
260 }
Hanno Becker02a68eb2021-04-15 09:57:17 +0100261#endif
262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 ret = ssl_cache_pick_writing_slot(cache,
264 session_id, session_id_len,
265 &cur);
266 if (ret != 0) {
Hanno Becker02a68eb2021-04-15 09:57:17 +0100267 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 }
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100269
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100270 /* Check how much space we need to serialize the session
271 * and allocate a sufficiently large buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 ret = mbedtls_ssl_session_save(session, NULL, 0, &session_serialized_len);
273 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
Hanno Beckeraee87172019-02-06 14:53:19 +0000274 goto exit;
275 }
276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 session_serialized = mbedtls_calloc(1, session_serialized_len);
278 if (session_serialized == NULL) {
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100279 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
280 goto exit;
Paul Bakkere81beda2013-03-06 17:40:46 +0100281 }
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100282
283 /* Now serialize the session into the allocated buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 ret = mbedtls_ssl_session_save(session,
285 session_serialized,
286 session_serialized_len,
287 &session_serialized_len);
288 if (ret != 0) {
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100289 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 }
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (session_id_len > sizeof(cur->session_id)) {
Pengyu Lv5038a382023-03-23 15:49:52 +0800293 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100294 goto exit;
295 }
296 cur->session_id_len = session_id_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 memcpy(cur->session_id, session_id, session_id_len);
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100298
299 cur->session = session_serialized;
300 cur->session_len = session_serialized_len;
301 session_serialized = NULL;
Paul Bakker0a597072012-09-25 21:55:46 +0000302
Paul Bakkerc5598842013-09-28 15:01:27 +0200303 ret = 0;
304
305exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if (mbedtls_mutex_unlock(&cache->mutex) != 0) {
Pengyu Lv0b9c0122023-03-15 14:37:32 +0800308 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200310#endif
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 if (session_serialized != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100313 mbedtls_zeroize_and_free(session_serialized, session_serialized_len);
Leonid Rozenboim116f50c2022-04-21 13:05:10 -0700314 session_serialized = NULL;
315 }
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return ret;
Paul Bakker0a597072012-09-25 21:55:46 +0000318}
319
Pengyu Lv7b6299b2023-03-07 14:38:45 +0800320int mbedtls_ssl_cache_remove(void *data,
321 unsigned char const *session_id,
322 size_t session_id_len)
323{
Pengyu Lv5038a382023-03-23 15:49:52 +0800324 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Pengyu Lv7b6299b2023-03-07 14:38:45 +0800325 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
326 mbedtls_ssl_cache_entry *entry;
327 mbedtls_ssl_cache_entry *prev;
328
329#if defined(MBEDTLS_THREADING_C)
Pengyu Lv0b9c0122023-03-15 14:37:32 +0800330 if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) {
331 return ret;
Pengyu Lv7b6299b2023-03-07 14:38:45 +0800332 }
333#endif
334
335 ret = ssl_cache_find_entry(cache, session_id, session_id_len, &entry);
336 /* No valid entry found, exit with success */
337 if (ret != 0) {
338 ret = 0;
339 goto exit;
340 }
341
342 /* Now we remove the entry from the chain */
343 if (entry == cache->chain) {
344 cache->chain = entry->next;
345 goto free;
346 }
347 for (prev = cache->chain; prev->next != NULL; prev = prev->next) {
348 if (prev->next == entry) {
349 prev->next = entry->next;
350 break;
351 }
352 }
353
354free:
Pengyu Lv744b5072023-03-15 12:17:14 +0800355 ssl_cache_entry_zeroize(entry);
Pengyu Lv7b6299b2023-03-07 14:38:45 +0800356 mbedtls_free(entry);
357 ret = 0;
358
359exit:
360#if defined(MBEDTLS_THREADING_C)
361 if (mbedtls_mutex_unlock(&cache->mutex) != 0) {
Pengyu Lv0b9c0122023-03-15 14:37:32 +0800362 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Pengyu Lv7b6299b2023-03-07 14:38:45 +0800363 }
364#endif
365
366 return ret;
367}
368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100370void mbedtls_ssl_cache_set_timeout(mbedtls_ssl_cache_context *cache, int timeout)
Paul Bakker0a597072012-09-25 21:55:46 +0000371{
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if (timeout < 0) {
373 timeout = 0;
374 }
Paul Bakker0a597072012-09-25 21:55:46 +0000375
376 cache->timeout = timeout;
377}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380void mbedtls_ssl_cache_set_max_entries(mbedtls_ssl_cache_context *cache, int max)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000381{
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if (max < 0) {
383 max = 0;
384 }
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000385
386 cache->max_entries = max;
387}
388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389void mbedtls_ssl_cache_free(mbedtls_ssl_cache_context *cache)
Paul Bakker0a597072012-09-25 21:55:46 +0000390{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakker0a597072012-09-25 21:55:46 +0000392
393 cur = cache->chain;
394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 while (cur != NULL) {
Paul Bakker0a597072012-09-25 21:55:46 +0000396 prv = cur;
397 cur = cur->next;
398
Pengyu Lv744b5072023-03-15 12:17:14 +0800399 ssl_cache_entry_zeroize(prv);
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 mbedtls_free(prv);
Paul Bakker0a597072012-09-25 21:55:46 +0000401 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 mbedtls_mutex_free(&cache->mutex);
Paul Bakkerc5598842013-09-28 15:01:27 +0200405#endif
Ron Eldor22360822017-10-29 17:53:52 +0200406 cache->chain = NULL;
Paul Bakker0a597072012-09-25 21:55:46 +0000407}
408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#endif /* MBEDTLS_SSL_CACHE_C */