2012-09-25 23:55:46 +02:00
|
|
|
/*
|
|
|
|
* SSL session cache implementation
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2012-09-25 23:55:46 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2012-09-25 23:55:46 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-09-25 23:55:46 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2012-09-25 23:55:46 +02:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* These session callbacks use a simple chained list
|
|
|
|
* to store and retrieve the session information.
|
|
|
|
*/
|
|
|
|
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_CACHE_C)
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2013-07-03 13:37:05 +02:00
|
|
|
|
2016-04-26 08:43:27 +02:00
|
|
|
#include "mbedtls/ssl_cache.h"
|
2021-03-05 19:38:47 +01:00
|
|
|
#include "ssl_misc.h"
|
2023-03-16 04:38:43 +01:00
|
|
|
#include "mbedtls/error.h"
|
2016-04-26 08:43:27 +02:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_cache_init(mbedtls_ssl_cache_context *cache)
|
2012-09-25 23:55:46 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(cache, 0, sizeof(mbedtls_ssl_cache_context));
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
|
|
|
|
cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
|
2013-09-28 15:01:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mutex_init(&cache->mutex);
|
2013-09-28 15:01:27 +02:00
|
|
|
#endif
|
2012-09-25 23:55:46 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_cache_find_entry(mbedtls_ssl_cache_context *cache,
|
|
|
|
unsigned char const *session_id,
|
|
|
|
size_t session_id_len,
|
|
|
|
mbedtls_ssl_cache_entry **dst)
|
2012-09-25 23:55:46 +02:00
|
|
|
{
|
2023-04-10 08:40:03 +02:00
|
|
|
int ret = MBEDTLS_ERR_SSL_CACHE_ENTRY_NOT_FOUND;
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_time_t t = mbedtls_time(NULL);
|
2013-07-03 15:31:03 +02:00
|
|
|
#endif
|
2021-04-15 11:17:53 +02:00
|
|
|
mbedtls_ssl_cache_entry *cur;
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (cur = cache->chain; cur != NULL; cur = cur->next) {
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cache->timeout != 0 &&
|
|
|
|
(int) (t - cur->timestamp) > cache->timeout) {
|
2012-09-25 23:55:46 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-07-03 15:31:03 +02:00
|
|
|
#endif
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session_id_len != cur->session_id_len ||
|
|
|
|
memcmp(session_id, cur->session_id,
|
|
|
|
cur->session_id_len) != 0) {
|
2012-09-25 23:55:46 +02:00
|
|
|
continue;
|
2021-04-15 09:19:40 +02:00
|
|
|
}
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2021-04-15 11:17:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cur != NULL) {
|
2021-04-15 11:17:53 +02:00
|
|
|
*dst = cur;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-04-15 11:17:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_cache_get(void *data,
|
|
|
|
unsigned char const *session_id,
|
|
|
|
size_t session_id_len,
|
|
|
|
mbedtls_ssl_session *session)
|
2021-04-15 11:17:53 +02:00
|
|
|
{
|
2023-03-23 08:49:52 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-04-15 11:17:53 +02:00
|
|
|
mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
|
|
|
|
mbedtls_ssl_cache_entry *entry;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-03-15 07:37:32 +01:00
|
|
|
if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) {
|
|
|
|
return ret;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-04-15 11:17:53 +02:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_cache_find_entry(cache, session_id, session_id_len, &entry);
|
|
|
|
if (ret != 0) {
|
2021-04-15 11:17:53 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-04-15 11:17:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_session_load(session,
|
|
|
|
entry->session,
|
|
|
|
entry->session_len);
|
|
|
|
if (ret != 0) {
|
2021-04-15 11:17:53 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-04-15 11:17:53 +02:00
|
|
|
|
|
|
|
ret = 0;
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2013-09-28 15:01:27 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mutex_unlock(&cache->mutex) != 0) {
|
2023-03-15 07:37:32 +01:00
|
|
|
ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-09-28 15:01:27 +02:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2012-09-25 23:55:46 +02:00
|
|
|
}
|
|
|
|
|
2023-03-15 05:17:14 +01:00
|
|
|
/* zeroize a cache entry */
|
|
|
|
static void ssl_cache_entry_zeroize(mbedtls_ssl_cache_entry *entry)
|
|
|
|
{
|
|
|
|
if (entry == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* zeroize and free session structure */
|
|
|
|
if (entry->session != NULL) {
|
|
|
|
mbedtls_platform_zeroize(entry->session, entry->session_len);
|
|
|
|
mbedtls_free(entry->session);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* zeroize the whole entry structure */
|
|
|
|
mbedtls_platform_zeroize(entry, sizeof(mbedtls_ssl_cache_entry));
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_cache_pick_writing_slot(mbedtls_ssl_cache_context *cache,
|
|
|
|
unsigned char const *session_id,
|
|
|
|
size_t session_id_len,
|
|
|
|
mbedtls_ssl_cache_entry **dst)
|
2012-09-25 23:55:46 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_time_t t = mbedtls_time(NULL), oldest = 0;
|
2021-05-14 05:55:35 +02:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ssl_cache_entry *old = NULL;
|
2012-10-24 00:18:28 +02:00
|
|
|
int count = 0;
|
2021-05-14 15:54:00 +02:00
|
|
|
mbedtls_ssl_cache_entry *cur, *last;
|
2013-09-28 15:01:27 +02:00
|
|
|
|
2021-05-13 08:05:07 +02:00
|
|
|
/* Check 1: Is there already an entry with the given session ID?
|
|
|
|
*
|
|
|
|
* If yes, overwrite it.
|
|
|
|
*
|
|
|
|
* If not, `count` will hold the size of the session cache
|
2021-05-14 15:54:00 +02:00
|
|
|
* at the end of this loop, and `last` will point to the last
|
2021-05-13 08:05:07 +02:00
|
|
|
* entry, both of which will be used later. */
|
|
|
|
|
2021-05-14 15:45:38 +02:00
|
|
|
last = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (cur = cache->chain; cur != NULL; cur = cur->next) {
|
2012-10-24 00:18:28 +02:00
|
|
|
count++;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session_id_len == cur->session_id_len &&
|
|
|
|
memcmp(session_id, cur->session_id, cur->session_id_len) == 0) {
|
2021-05-13 08:05:07 +02:00
|
|
|
goto found;
|
2021-04-15 10:26:17 +02:00
|
|
|
}
|
2021-05-14 15:45:38 +02:00
|
|
|
last = cur;
|
2021-05-13 08:05:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check 2: Is there an outdated entry in the cache?
|
|
|
|
*
|
|
|
|
* If so, overwrite it.
|
|
|
|
*
|
|
|
|
* If not, remember the oldest entry in `old` for later.
|
|
|
|
*/
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-01-11 14:50:10 +01:00
|
|
|
for (cur = cache->chain; cur != NULL; cur = cur->next) {
|
|
|
|
if (cache->timeout != 0 &&
|
|
|
|
(int) (t - cur->timestamp) > cache->timeout) {
|
2021-05-13 08:05:07 +02:00
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (oldest == 0 || cur->timestamp < oldest) {
|
2012-10-24 00:18:28 +02:00
|
|
|
oldest = cur->timestamp;
|
|
|
|
old = cur;
|
|
|
|
}
|
2012-09-25 23:55:46 +02:00
|
|
|
}
|
2021-05-13 08:05:07 +02:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
|
|
|
|
|
|
|
/* Check 3: Is there free space in the cache? */
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (count < cache->max_entries) {
|
2021-05-13 08:05:07 +02:00
|
|
|
/* Create new entry */
|
2023-01-11 14:50:10 +01:00
|
|
|
cur = mbedtls_calloc(1, sizeof(mbedtls_ssl_cache_entry));
|
|
|
|
if (cur == NULL) {
|
2023-03-23 08:49:52 +01:00
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-05-13 08:05:07 +02:00
|
|
|
|
|
|
|
/* Append to the end of the linked list. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (last == NULL) {
|
2021-05-13 08:05:07 +02:00
|
|
|
cache->chain = cur;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2021-05-14 15:54:00 +02:00
|
|
|
last->next = cur;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2014-02-20 21:32:08 +01:00
|
|
|
|
2021-05-13 08:05:07 +02:00
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Last resort: The cache is full and doesn't contain any outdated
|
|
|
|
* elements. In this case, we evict the oldest one, judged by timestamp
|
|
|
|
* (if present) or cache-order. */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (old == NULL) {
|
2021-05-13 08:05:07 +02:00
|
|
|
/* This should only happen on an ill-configured cache
|
|
|
|
* with max_entries == 0. */
|
2023-03-23 08:49:52 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2021-05-13 08:05:07 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#else /* MBEDTLS_HAVE_TIME */
|
2021-05-13 08:05:07 +02:00
|
|
|
/* Reuse first entry in chain, but move to last place. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cache->chain == NULL) {
|
2023-03-23 08:49:52 +01:00
|
|
|
/* This should never happen */
|
|
|
|
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-05-13 08:05:07 +02:00
|
|
|
|
|
|
|
old = cache->chain;
|
|
|
|
cache->chain = old->next;
|
2021-05-14 15:45:04 +02:00
|
|
|
old->next = NULL;
|
2021-05-14 15:54:00 +02:00
|
|
|
last->next = old;
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
2021-05-13 08:05:07 +02:00
|
|
|
|
|
|
|
/* Now `old` points to the oldest entry to be overwritten. */
|
|
|
|
cur = old;
|
|
|
|
|
|
|
|
found:
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2021-05-13 08:05:07 +02:00
|
|
|
/* If we're reusing an entry, free it first. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cur->session != NULL) {
|
2023-03-15 05:17:14 +01:00
|
|
|
/* `ssl_cache_entry_zeroize` would break the chain,
|
|
|
|
* so we reuse `old` to record `next` temporarily. */
|
|
|
|
old = cur->next;
|
|
|
|
ssl_cache_entry_zeroize(cur);
|
|
|
|
cur->next = old;
|
2014-02-26 17:38:55 +01:00
|
|
|
}
|
2021-04-15 10:57:17 +02:00
|
|
|
|
2023-03-15 05:17:14 +01:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
cur->timestamp = t;
|
|
|
|
#endif
|
|
|
|
|
2021-05-13 08:05:07 +02:00
|
|
|
*dst = cur;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-04-15 10:57:17 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_cache_set(void *data,
|
|
|
|
unsigned char const *session_id,
|
|
|
|
size_t session_id_len,
|
|
|
|
const mbedtls_ssl_session *session)
|
2021-04-15 10:57:17 +02:00
|
|
|
{
|
2023-03-23 08:49:52 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-04-15 10:57:17 +02:00
|
|
|
mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
|
|
|
|
mbedtls_ssl_cache_entry *cur;
|
|
|
|
|
2021-04-15 11:26:06 +02:00
|
|
|
size_t session_serialized_len;
|
|
|
|
unsigned char *session_serialized = NULL;
|
|
|
|
|
2021-04-15 10:57:17 +02:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2021-04-15 10:57:17 +02:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_cache_pick_writing_slot(cache,
|
|
|
|
session_id, session_id_len,
|
|
|
|
&cur);
|
|
|
|
if (ret != 0) {
|
2021-04-15 10:57:17 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-02-06 15:53:19 +01:00
|
|
|
|
2021-04-15 11:26:06 +02:00
|
|
|
/* Check how much space we need to serialize the session
|
|
|
|
* and allocate a sufficiently large buffer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_session_save(session, NULL, 0, &session_serialized_len);
|
|
|
|
if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
|
2019-02-06 15:53:19 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2014-02-26 17:38:55 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
session_serialized = mbedtls_calloc(1, session_serialized_len);
|
|
|
|
if (session_serialized == NULL) {
|
2021-04-15 11:26:06 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
goto exit;
|
|
|
|
}
|
2013-03-06 17:40:46 +01:00
|
|
|
|
2021-04-15 11:26:06 +02:00
|
|
|
/* Now serialize the session into the allocated buffer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_session_save(session,
|
|
|
|
session_serialized,
|
|
|
|
session_serialized_len,
|
|
|
|
&session_serialized_len);
|
|
|
|
if (ret != 0) {
|
2021-04-15 11:26:06 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-03-06 17:40:46 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session_id_len > sizeof(cur->session_id)) {
|
2023-03-23 08:49:52 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2021-04-15 11:26:06 +02:00
|
|
|
goto exit;
|
2013-03-06 17:40:46 +01:00
|
|
|
}
|
2021-04-15 11:26:06 +02:00
|
|
|
cur->session_id_len = session_id_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(cur->session_id, session_id, session_id_len);
|
2021-04-15 11:26:06 +02:00
|
|
|
|
|
|
|
cur->session = session_serialized;
|
|
|
|
cur->session_len = session_serialized_len;
|
|
|
|
session_serialized = NULL;
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2013-09-28 15:01:27 +02:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mutex_unlock(&cache->mutex) != 0) {
|
2023-03-15 07:37:32 +01:00
|
|
|
ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-09-28 15:01:27 +02:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session_serialized != NULL) {
|
|
|
|
mbedtls_platform_zeroize(session_serialized, session_serialized_len);
|
|
|
|
mbedtls_free(session_serialized);
|
2022-04-21 22:05:10 +02:00
|
|
|
session_serialized = NULL;
|
|
|
|
}
|
2021-04-15 11:26:06 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2012-09-25 23:55:46 +02:00
|
|
|
}
|
|
|
|
|
2023-03-07 07:38:45 +01:00
|
|
|
int mbedtls_ssl_cache_remove(void *data,
|
|
|
|
unsigned char const *session_id,
|
|
|
|
size_t session_id_len)
|
|
|
|
{
|
2023-03-23 08:49:52 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-03-07 07:38:45 +01:00
|
|
|
mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
|
|
|
|
mbedtls_ssl_cache_entry *entry;
|
|
|
|
mbedtls_ssl_cache_entry *prev;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-03-15 07:37:32 +01:00
|
|
|
if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) {
|
|
|
|
return ret;
|
2023-03-07 07:38:45 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ret = ssl_cache_find_entry(cache, session_id, session_id_len, &entry);
|
|
|
|
/* No valid entry found, exit with success */
|
|
|
|
if (ret != 0) {
|
|
|
|
ret = 0;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now we remove the entry from the chain */
|
|
|
|
if (entry == cache->chain) {
|
|
|
|
cache->chain = entry->next;
|
|
|
|
goto free;
|
|
|
|
}
|
|
|
|
for (prev = cache->chain; prev->next != NULL; prev = prev->next) {
|
|
|
|
if (prev->next == entry) {
|
|
|
|
prev->next = entry->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free:
|
2023-03-15 05:17:14 +01:00
|
|
|
ssl_cache_entry_zeroize(entry);
|
2023-03-07 07:38:45 +01:00
|
|
|
mbedtls_free(entry);
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
if (mbedtls_mutex_unlock(&cache->mutex) != 0) {
|
2023-03-15 07:37:32 +01:00
|
|
|
ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
|
2023-03-07 07:38:45 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_cache_set_timeout(mbedtls_ssl_cache_context *cache, int timeout)
|
2012-09-25 23:55:46 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (timeout < 0) {
|
|
|
|
timeout = 0;
|
|
|
|
}
|
2012-09-25 23:55:46 +02:00
|
|
|
|
|
|
|
cache->timeout = timeout;
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_cache_set_max_entries(mbedtls_ssl_cache_context *cache, int max)
|
2012-10-24 00:18:28 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (max < 0) {
|
|
|
|
max = 0;
|
|
|
|
}
|
2012-10-24 00:18:28 +02:00
|
|
|
|
|
|
|
cache->max_entries = max;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_cache_free(mbedtls_ssl_cache_context *cache)
|
2012-09-25 23:55:46 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ssl_cache_entry *cur, *prv;
|
2012-09-25 23:55:46 +02:00
|
|
|
|
|
|
|
cur = cache->chain;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (cur != NULL) {
|
2012-09-25 23:55:46 +02:00
|
|
|
prv = cur;
|
|
|
|
cur = cur->next;
|
|
|
|
|
2023-03-15 05:17:14 +01:00
|
|
|
ssl_cache_entry_zeroize(prv);
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(prv);
|
2012-09-25 23:55:46 +02:00
|
|
|
}
|
2013-09-28 15:01:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mutex_free(&cache->mutex);
|
2013-09-28 15:01:27 +02:00
|
|
|
#endif
|
2017-10-29 16:53:52 +01:00
|
|
|
cache->chain = NULL;
|
2012-09-25 23:55:46 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_CACHE_C */
|