blob: d93508c6b3cdc3d501a4f8daaae3f81a93db5f49 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakker0a597072012-09-25 21:55:46 +000018 */
19/*
20 * These session callbacks use a simple chained list
21 * to store and retrieve the session information.
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker0a597072012-09-25 21:55:46 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_CACHE_C)
Paul Bakker0a597072012-09-25 21:55:46 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020029
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/ssl_cache.h"
Chris Jones84a773f2021-03-05 18:38:47 +000031#include "ssl_misc.h"
SimonBd5800b72016-04-26 07:43:27 +010032
33#include <string.h>
34
Gilles Peskine449bd832023-01-11 14:50:10 +010035void mbedtls_ssl_cache_init(mbedtls_ssl_cache_context *cache)
Paul Bakker0a597072012-09-25 21:55:46 +000036{
Gilles Peskine449bd832023-01-11 14:50:10 +010037 memset(cache, 0, sizeof(mbedtls_ssl_cache_context));
Paul Bakker0a597072012-09-25 21:55:46 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
40 cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
Paul Bakkerc5598842013-09-28 15:01:27 +020041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010043 mbedtls_mutex_init(&cache->mutex);
Paul Bakkerc5598842013-09-28 15:01:27 +020044#endif
Paul Bakker0a597072012-09-25 21:55:46 +000045}
46
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020047MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010048static 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 Beckerf938c432021-04-15 10:17:53 +010052{
53 int ret = 1;
54#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +010055 mbedtls_time_t t = mbedtls_time(NULL);
Hanno Beckerf938c432021-04-15 10:17:53 +010056#endif
57 mbedtls_ssl_cache_entry *cur;
58
Gilles Peskine449bd832023-01-11 14:50:10 +010059 for (cur = cache->chain; cur != NULL; cur = cur->next) {
Hanno Beckerf938c432021-04-15 10:17:53 +010060#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +010061 if (cache->timeout != 0 &&
62 (int) (t - cur->timestamp) > cache->timeout) {
Hanno Beckerf938c432021-04-15 10:17:53 +010063 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +010064 }
Hanno Beckerf938c432021-04-15 10:17:53 +010065#endif
66
Gilles Peskine449bd832023-01-11 14:50:10 +010067 if (session_id_len != cur->session_id_len ||
68 memcmp(session_id, cur->session_id,
69 cur->session_id_len) != 0) {
Hanno Beckerf938c432021-04-15 10:17:53 +010070 continue;
71 }
72
73 break;
74 }
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076 if (cur != NULL) {
Hanno Beckerf938c432021-04-15 10:17:53 +010077 *dst = cur;
78 ret = 0;
79 }
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081 return ret;
Hanno Beckerf938c432021-04-15 10:17:53 +010082}
83
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085int mbedtls_ssl_cache_get(void *data,
86 unsigned char const *session_id,
87 size_t session_id_len,
88 mbedtls_ssl_session *session)
Paul Bakker0a597072012-09-25 21:55:46 +000089{
Paul Bakkerc5598842013-09-28 15:01:27 +020090 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
Hanno Beckerf938c432021-04-15 10:17:53 +010092 mbedtls_ssl_cache_entry *entry;
Paul Bakker0a597072012-09-25 21:55:46 +000093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010095 if (mbedtls_mutex_lock(&cache->mutex) != 0) {
96 return 1;
97 }
Paul Bakkerc5598842013-09-28 15:01:27 +020098#endif
99
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 ret = ssl_cache_find_entry(cache, session_id, session_id_len, &entry);
101 if (ret != 0) {
Hanno Beckerf938c432021-04-15 10:17:53 +0100102 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 }
Paul Bakker0a597072012-09-25 21:55:46 +0000104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 ret = mbedtls_ssl_session_load(session,
106 entry->session,
107 entry->session_len);
108 if (ret != 0) {
Hanno Beckerf938c432021-04-15 10:17:53 +0100109 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 }
Hanno Beckerf938c432021-04-15 10:17:53 +0100111
Hanno Beckerf938c432021-04-15 10:17:53 +0100112 ret = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000113
Paul Bakkerc5598842013-09-28 15:01:27 +0200114exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if (mbedtls_mutex_unlock(&cache->mutex) != 0) {
Paul Bakkerc5598842013-09-28 15:01:27 +0200117 ret = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200119#endif
120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 return ret;
Paul Bakker0a597072012-09-25 21:55:46 +0000122}
123
Pengyu Lv744b5072023-03-15 12:17:14 +0800124/* zeroize a cache entry */
125static void ssl_cache_entry_zeroize(mbedtls_ssl_cache_entry *entry)
126{
127 if (entry == NULL) {
128 return;
129 }
130
131 /* zeroize and free session structure */
132 if (entry->session != NULL) {
133 mbedtls_platform_zeroize(entry->session, entry->session_len);
134 mbedtls_free(entry->session);
135 }
136
137 /* zeroize the whole entry structure */
138 mbedtls_platform_zeroize(entry, sizeof(mbedtls_ssl_cache_entry));
139}
140
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200141MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100142static 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 Bakker0a597072012-09-25 21:55:46 +0000146{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 mbedtls_time_t t = mbedtls_time(NULL), oldest = 0;
Hanno Becker006f2cc2021-05-14 04:55:35 +0100149#endif /* MBEDTLS_HAVE_TIME */
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_ssl_cache_entry *old = NULL;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000152 int count = 0;
Hanno Becker466ed6f2021-05-14 14:54:00 +0100153 mbedtls_ssl_cache_entry *cur, *last;
Paul Bakkerc5598842013-09-28 15:01:27 +0200154
Hanno Becker845ceb72021-05-13 07:05:07 +0100155 /* 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 Becker466ed6f2021-05-14 14:54:00 +0100160 * at the end of this loop, and `last` will point to the last
Hanno Becker845ceb72021-05-13 07:05:07 +0100161 * entry, both of which will be used later. */
162
Hanno Becker78196e32021-05-14 14:45:38 +0100163 last = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 for (cur = cache->chain; cur != NULL; cur = cur->next) {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000165 count++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 if (session_id_len == cur->session_id_len &&
167 memcmp(session_id, cur->session_id, cur->session_id_len) == 0) {
Hanno Becker845ceb72021-05-13 07:05:07 +0100168 goto found;
Hanno Beckerccdaf6e2021-04-15 09:26:17 +0100169 }
Hanno Becker78196e32021-05-14 14:45:38 +0100170 last = cur;
Paul Bakker0a597072012-09-25 21:55:46 +0000171 }
172
Hanno Becker845ceb72021-05-13 07:05:07 +0100173 /* 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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 for (cur = cache->chain; cur != NULL; cur = cur->next) {
182 if (cache->timeout != 0 &&
183 (int) (t - cur->timestamp) > cache->timeout) {
Hanno Becker845ceb72021-05-13 07:05:07 +0100184 goto found;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000185 }
Hanno Becker845ceb72021-05-13 07:05:07 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (oldest == 0 || cur->timestamp < oldest) {
Hanno Becker845ceb72021-05-13 07:05:07 +0100188 oldest = cur->timestamp;
189 old = cur;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200190 }
Hanno Becker845ceb72021-05-13 07:05:07 +0100191 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#endif /* MBEDTLS_HAVE_TIME */
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000193
Hanno Becker845ceb72021-05-13 07:05:07 +0100194 /* Check 3: Is there free space in the cache? */
195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if (count < cache->max_entries) {
Hanno Becker845ceb72021-05-13 07:05:07 +0100197 /* Create new entry */
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 cur = mbedtls_calloc(1, sizeof(mbedtls_ssl_cache_entry));
199 if (cur == NULL) {
200 return 1;
201 }
Hanno Becker845ceb72021-05-13 07:05:07 +0100202
203 /* Append to the end of the linked list. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if (last == NULL) {
Hanno Becker845ceb72021-05-13 07:05:07 +0100205 cache->chain = cur;
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 } else {
Hanno Becker466ed6f2021-05-14 14:54:00 +0100207 last->next = cur;
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 }
Hanno Becker845ceb72021-05-13 07:05:07 +0100209
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 Bakker0a597072012-09-25 21:55:46 +0000216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if (old == NULL) {
Hanno Becker845ceb72021-05-13 07:05:07 +0100219 /* This should only happen on an ill-configured cache
220 * with max_entries == 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 return 1;
Hanno Becker845ceb72021-05-13 07:05:07 +0100222 }
223#else /* MBEDTLS_HAVE_TIME */
224 /* Reuse first entry in chain, but move to last place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (cache->chain == NULL) {
226 return 1;
227 }
Hanno Becker02a68eb2021-04-15 09:57:17 +0100228
Hanno Becker845ceb72021-05-13 07:05:07 +0100229 old = cache->chain;
230 cache->chain = old->next;
Hanno Becker5cf6f7e2021-05-14 14:45:04 +0100231 old->next = NULL;
Hanno Becker466ed6f2021-05-14 14:54:00 +0100232 last->next = old;
Hanno Becker845ceb72021-05-13 07:05:07 +0100233#endif /* MBEDTLS_HAVE_TIME */
Hanno Becker02a68eb2021-04-15 09:57:17 +0100234
Hanno Becker845ceb72021-05-13 07:05:07 +0100235 /* Now `old` points to the oldest entry to be overwritten. */
236 cur = old;
237
238found:
239
Pengyu Lv744b5072023-03-15 12:17:14 +0800240 /* If we're reusing an entry, free it first. */
241 if (cur->session != NULL) {
242 /* `ssl_cache_entry_zeroize` would break the chain,
243 * so we reuse `old` to record `next` temporarily. */
244 old = cur->next;
245 ssl_cache_entry_zeroize(cur);
246 cur->next = old;
247 }
248
Hanno Becker845ceb72021-05-13 07:05:07 +0100249#if defined(MBEDTLS_HAVE_TIME)
250 cur->timestamp = t;
251#endif
252
Hanno Becker845ceb72021-05-13 07:05:07 +0100253 *dst = cur;
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return 0;
Hanno Becker02a68eb2021-04-15 09:57:17 +0100255}
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257int mbedtls_ssl_cache_set(void *data,
258 unsigned char const *session_id,
259 size_t session_id_len,
260 const mbedtls_ssl_session *session)
Hanno Becker02a68eb2021-04-15 09:57:17 +0100261{
262 int ret = 1;
263 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
264 mbedtls_ssl_cache_entry *cur;
265
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100266 size_t session_serialized_len;
267 unsigned char *session_serialized = NULL;
268
Hanno Becker02a68eb2021-04-15 09:57:17 +0100269#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) {
271 return ret;
272 }
Hanno Becker02a68eb2021-04-15 09:57:17 +0100273#endif
274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 ret = ssl_cache_pick_writing_slot(cache,
276 session_id, session_id_len,
277 &cur);
278 if (ret != 0) {
Hanno Becker02a68eb2021-04-15 09:57:17 +0100279 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 }
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100281
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100282 /* Check how much space we need to serialize the session
283 * and allocate a sufficiently large buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 ret = mbedtls_ssl_session_save(session, NULL, 0, &session_serialized_len);
285 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
Hanno Beckeraee87172019-02-06 14:53:19 +0000286 ret = 1;
287 goto exit;
288 }
289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 session_serialized = mbedtls_calloc(1, session_serialized_len);
291 if (session_serialized == NULL) {
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100292 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
293 goto exit;
Paul Bakkere81beda2013-03-06 17:40:46 +0100294 }
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100295
296 /* Now serialize the session into the allocated buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 ret = mbedtls_ssl_session_save(session,
298 session_serialized,
299 session_serialized_len,
300 &session_serialized_len);
301 if (ret != 0) {
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100302 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 }
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if (session_id_len > sizeof(cur->session_id)) {
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100306 ret = 1;
307 goto exit;
308 }
309 cur->session_id_len = session_id_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 memcpy(cur->session_id, session_id, session_id_len);
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100311
312 cur->session = session_serialized;
313 cur->session_len = session_serialized_len;
314 session_serialized = NULL;
Paul Bakker0a597072012-09-25 21:55:46 +0000315
Paul Bakkerc5598842013-09-28 15:01:27 +0200316 ret = 0;
317
318exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (mbedtls_mutex_unlock(&cache->mutex) != 0) {
Paul Bakkerc5598842013-09-28 15:01:27 +0200321 ret = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200323#endif
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (session_serialized != NULL) {
326 mbedtls_platform_zeroize(session_serialized, session_serialized_len);
327 mbedtls_free(session_serialized);
Leonid Rozenboim116f50c2022-04-21 13:05:10 -0700328 session_serialized = NULL;
329 }
Hanno Becker7e6eb9f2021-04-15 10:26:06 +0100330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return ret;
Paul Bakker0a597072012-09-25 21:55:46 +0000332}
333
Pengyu Lv7b6299b2023-03-07 14:38:45 +0800334int mbedtls_ssl_cache_remove(void *data,
335 unsigned char const *session_id,
336 size_t session_id_len)
337{
338 int ret = 1;
339 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
340 mbedtls_ssl_cache_entry *entry;
341 mbedtls_ssl_cache_entry *prev;
342
343#if defined(MBEDTLS_THREADING_C)
344 if (mbedtls_mutex_lock(&cache->mutex) != 0) {
345 return 1;
346 }
347#endif
348
349 ret = ssl_cache_find_entry(cache, session_id, session_id_len, &entry);
350 /* No valid entry found, exit with success */
351 if (ret != 0) {
352 ret = 0;
353 goto exit;
354 }
355
356 /* Now we remove the entry from the chain */
357 if (entry == cache->chain) {
358 cache->chain = entry->next;
359 goto free;
360 }
361 for (prev = cache->chain; prev->next != NULL; prev = prev->next) {
362 if (prev->next == entry) {
363 prev->next = entry->next;
364 break;
365 }
366 }
367
368free:
Pengyu Lv744b5072023-03-15 12:17:14 +0800369 ssl_cache_entry_zeroize(entry);
Pengyu Lv7b6299b2023-03-07 14:38:45 +0800370 mbedtls_free(entry);
371 ret = 0;
372
373exit:
374#if defined(MBEDTLS_THREADING_C)
375 if (mbedtls_mutex_unlock(&cache->mutex) != 0) {
376 ret = 1;
377 }
378#endif
379
380 return ret;
381}
382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100384void mbedtls_ssl_cache_set_timeout(mbedtls_ssl_cache_context *cache, int timeout)
Paul Bakker0a597072012-09-25 21:55:46 +0000385{
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (timeout < 0) {
387 timeout = 0;
388 }
Paul Bakker0a597072012-09-25 21:55:46 +0000389
390 cache->timeout = timeout;
391}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394void mbedtls_ssl_cache_set_max_entries(mbedtls_ssl_cache_context *cache, int max)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000395{
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if (max < 0) {
397 max = 0;
398 }
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000399
400 cache->max_entries = max;
401}
402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403void mbedtls_ssl_cache_free(mbedtls_ssl_cache_context *cache)
Paul Bakker0a597072012-09-25 21:55:46 +0000404{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakker0a597072012-09-25 21:55:46 +0000406
407 cur = cache->chain;
408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 while (cur != NULL) {
Paul Bakker0a597072012-09-25 21:55:46 +0000410 prv = cur;
411 cur = cur->next;
412
Pengyu Lv744b5072023-03-15 12:17:14 +0800413 ssl_cache_entry_zeroize(prv);
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 mbedtls_free(prv);
Paul Bakker0a597072012-09-25 21:55:46 +0000415 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 mbedtls_mutex_free(&cache->mutex);
Paul Bakkerc5598842013-09-28 15:01:27 +0200419#endif
Ron Eldor22360822017-10-29 17:53:52 +0200420 cache->chain = NULL;
Paul Bakker0a597072012-09-25 21:55:46 +0000421}
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423#endif /* MBEDTLS_SSL_CACHE_C */