2022-03-15 13:29:32 +01:00
|
|
|
/*
|
|
|
|
* SSL session cache implementation
|
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* These session callbacks use a simple chained list
|
|
|
|
* to store and retrieve the session information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_CACHE_C)
|
|
|
|
|
|
|
|
#include "mbedtls/platform.h"
|
2023-10-21 14:06:23 +02:00
|
|
|
#include "mbedtls/error.h"
|
2022-03-15 13:29:32 +01:00
|
|
|
|
|
|
|
#include "mbedtls/ssl_cache.h"
|
|
|
|
#include "mbedtls/ssl_internal.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
void mbedtls_ssl_cache_init(mbedtls_ssl_cache_context *cache)
|
2022-03-15 13:29:32 +01:00
|
|
|
{
|
2023-08-30 11:18:00 +02:00
|
|
|
memset(cache, 0, sizeof(mbedtls_ssl_cache_context));
|
2022-03-15 13:29:32 +01:00
|
|
|
|
|
|
|
cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
|
|
|
|
cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-08-30 11:18:00 +02:00
|
|
|
mbedtls_mutex_init(&cache->mutex);
|
2022-03-15 13:29:32 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
int mbedtls_ssl_cache_get(void *data, mbedtls_ssl_session *session)
|
2022-03-15 13:29:32 +01:00
|
|
|
{
|
2023-10-21 14:06:23 +02:00
|
|
|
int ret = MBEDTLS_ERR_SSL_CACHE_ENTRY_NOT_FOUND;
|
2022-03-15 13:29:32 +01:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-08-30 11:18:00 +02:00
|
|
|
mbedtls_time_t t = mbedtls_time(NULL);
|
2022-03-15 13:29:32 +01:00
|
|
|
#endif
|
|
|
|
mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
|
|
|
|
mbedtls_ssl_cache_entry *cur, *entry;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-10-21 14:06:23 +02:00
|
|
|
if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) {
|
|
|
|
return ret;
|
2023-08-30 11:18:00 +02:00
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
cur = cache->chain;
|
|
|
|
entry = NULL;
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
while (cur != NULL) {
|
2022-03-15 13:29:32 +01:00
|
|
|
entry = cur;
|
|
|
|
cur = cur->next;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-08-30 11:18:00 +02:00
|
|
|
if (cache->timeout != 0 &&
|
|
|
|
(int) (t - entry->timestamp) > cache->timeout) {
|
2022-03-15 13:29:32 +01:00
|
|
|
continue;
|
2023-08-30 11:18:00 +02:00
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
#endif
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
if (session->id_len != entry->session.id_len ||
|
|
|
|
memcmp(session->id, entry->session.id,
|
|
|
|
entry->session.id_len) != 0) {
|
2022-03-15 13:29:32 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
ret = mbedtls_ssl_session_copy(session, &entry->session);
|
|
|
|
if (ret != 0) {
|
2022-03-15 13:29:32 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
2023-08-30 11:18:00 +02:00
|
|
|
defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
2022-03-15 13:29:32 +01:00
|
|
|
/*
|
|
|
|
* Restore peer certificate (without rest of the original chain)
|
|
|
|
*/
|
2023-08-30 11:18:00 +02:00
|
|
|
if (entry->peer_cert.p != NULL) {
|
2022-03-15 13:29:32 +01:00
|
|
|
/* `session->peer_cert` is NULL after the call to
|
|
|
|
* mbedtls_ssl_session_copy(), because cache entries
|
|
|
|
* have the `peer_cert` field set to NULL. */
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
if ((session->peer_cert = mbedtls_calloc(1,
|
|
|
|
sizeof(mbedtls_x509_crt))) == NULL) {
|
2023-10-21 14:06:23 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
2022-03-15 13:29:32 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
mbedtls_x509_crt_init(session->peer_cert);
|
2023-10-21 14:06:23 +02:00
|
|
|
if ((ret = mbedtls_x509_crt_parse(session->peer_cert, entry->peer_cert.p,
|
|
|
|
entry->peer_cert.len)) != 0) {
|
2023-08-30 11:18:00 +02:00
|
|
|
mbedtls_free(session->peer_cert);
|
2022-03-15 13:29:32 +01:00
|
|
|
session->peer_cert = NULL;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-08-30 11:18:00 +02:00
|
|
|
if (mbedtls_mutex_unlock(&cache->mutex) != 0) {
|
2023-10-21 14:06:23 +02:00
|
|
|
ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
|
2023-08-30 11:18:00 +02:00
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
#endif
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
return ret;
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
int mbedtls_ssl_cache_set(void *data, const mbedtls_ssl_session *session)
|
2022-03-15 13:29:32 +01:00
|
|
|
{
|
2023-10-21 14:06:23 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-03-15 13:29:32 +01:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-08-30 11:18:00 +02:00
|
|
|
mbedtls_time_t t = mbedtls_time(NULL), oldest = 0;
|
2022-03-15 13:29:32 +01:00
|
|
|
mbedtls_ssl_cache_entry *old = NULL;
|
|
|
|
#endif
|
|
|
|
mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
|
|
|
|
mbedtls_ssl_cache_entry *cur, *prv;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-08-30 11:18:00 +02:00
|
|
|
if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
cur = cache->chain;
|
|
|
|
prv = NULL;
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
while (cur != NULL) {
|
2022-03-15 13:29:32 +01:00
|
|
|
count++;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-08-30 11:18:00 +02:00
|
|
|
if (cache->timeout != 0 &&
|
|
|
|
(int) (t - cur->timestamp) > cache->timeout) {
|
2022-03-15 13:29:32 +01:00
|
|
|
cur->timestamp = t;
|
|
|
|
break; /* expired, reuse this slot, update timestamp */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
if (memcmp(session->id, cur->session.id, cur->session.id_len) == 0) {
|
2022-03-15 13:29:32 +01:00
|
|
|
break; /* client reconnected, keep timestamp for session id */
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-08-30 11:18:00 +02:00
|
|
|
if (oldest == 0 || cur->timestamp < oldest) {
|
2022-03-15 13:29:32 +01:00
|
|
|
oldest = cur->timestamp;
|
|
|
|
old = cur;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
prv = cur;
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
if (cur == NULL) {
|
2022-03-15 13:29:32 +01:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
/*
|
|
|
|
* Reuse oldest entry if max_entries reached
|
|
|
|
*/
|
2023-08-30 11:18:00 +02:00
|
|
|
if (count >= cache->max_entries) {
|
|
|
|
if (old == NULL) {
|
2023-10-21 14:06:23 +02:00
|
|
|
/* This should only happen on an ill-configured cache
|
|
|
|
* with max_entries == 0. */
|
|
|
|
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-03-15 13:29:32 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = old;
|
|
|
|
}
|
|
|
|
#else /* MBEDTLS_HAVE_TIME */
|
|
|
|
/*
|
|
|
|
* Reuse first entry in chain if max_entries reached,
|
|
|
|
* but move to last place
|
|
|
|
*/
|
2023-08-30 11:18:00 +02:00
|
|
|
if (count >= cache->max_entries) {
|
|
|
|
if (cache->chain == NULL) {
|
2023-10-21 14:06:23 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-03-15 13:29:32 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = cache->chain;
|
|
|
|
cache->chain = cur->next;
|
|
|
|
cur->next = NULL;
|
|
|
|
prv->next = cur;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
2023-08-30 11:18:00 +02:00
|
|
|
else {
|
2022-03-15 13:29:32 +01:00
|
|
|
/*
|
|
|
|
* max_entries not reached, create new entry
|
|
|
|
*/
|
2023-08-30 11:18:00 +02:00
|
|
|
cur = mbedtls_calloc(1, sizeof(mbedtls_ssl_cache_entry));
|
|
|
|
if (cur == NULL) {
|
2023-10-21 14:06:23 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
2022-03-15 13:29:32 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
if (prv == NULL) {
|
2022-03-15 13:29:32 +01:00
|
|
|
cache->chain = cur;
|
2023-08-30 11:18:00 +02:00
|
|
|
} else {
|
2022-03-15 13:29:32 +01:00
|
|
|
prv->next = cur;
|
2023-08-30 11:18:00 +02:00
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
cur->timestamp = t;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
|
|
|
defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
/*
|
|
|
|
* If we're reusing an entry, free its certificate first
|
|
|
|
*/
|
2023-08-30 11:18:00 +02:00
|
|
|
if (cur->peer_cert.p != NULL) {
|
|
|
|
mbedtls_free(cur->peer_cert.p);
|
|
|
|
memset(&cur->peer_cert, 0, sizeof(mbedtls_x509_buf));
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
|
|
|
|
/* Copy the entire session; this temporarily makes a copy of the
|
|
|
|
* X.509 CRT structure even though we only want to store the raw CRT.
|
|
|
|
* This inefficiency will go away as soon as we implement on-demand
|
|
|
|
* parsing of CRTs, in which case there's no need for the `peer_cert`
|
|
|
|
* field anymore in the first place, and we're done after this call. */
|
2023-08-30 11:18:00 +02:00
|
|
|
ret = mbedtls_ssl_session_copy(&cur->session, session);
|
|
|
|
if (ret != 0) {
|
2022-03-15 13:29:32 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
|
|
|
defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
/* If present, free the X.509 structure and only store the raw CRT data. */
|
2023-08-30 11:18:00 +02:00
|
|
|
if (cur->session.peer_cert != NULL) {
|
2022-03-15 13:29:32 +01:00
|
|
|
cur->peer_cert.p =
|
2023-08-30 11:18:00 +02:00
|
|
|
mbedtls_calloc(1, cur->session.peer_cert->raw.len);
|
|
|
|
if (cur->peer_cert.p == NULL) {
|
2023-10-21 14:06:23 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
2022-03-15 13:29:32 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
memcpy(cur->peer_cert.p,
|
|
|
|
cur->session.peer_cert->raw.p,
|
|
|
|
cur->session.peer_cert->raw.len);
|
2022-03-15 13:29:32 +01:00
|
|
|
cur->peer_cert.len = session->peer_cert->raw.len;
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
mbedtls_x509_crt_free(cur->session.peer_cert);
|
|
|
|
mbedtls_free(cur->session.peer_cert);
|
2022-03-15 13:29:32 +01:00
|
|
|
cur->session.peer_cert = NULL;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-08-30 11:18:00 +02:00
|
|
|
if (mbedtls_mutex_unlock(&cache->mutex) != 0) {
|
2023-10-21 14:06:23 +02:00
|
|
|
ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
|
2023-08-30 11:18:00 +02:00
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
#endif
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
return ret;
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-08-30 11:18:00 +02:00
|
|
|
void mbedtls_ssl_cache_set_timeout(mbedtls_ssl_cache_context *cache, int timeout)
|
2022-03-15 13:29:32 +01:00
|
|
|
{
|
2023-08-30 11:18:00 +02:00
|
|
|
if (timeout < 0) {
|
|
|
|
timeout = 0;
|
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
|
|
|
|
cache->timeout = timeout;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
void mbedtls_ssl_cache_set_max_entries(mbedtls_ssl_cache_context *cache, int max)
|
2022-03-15 13:29:32 +01:00
|
|
|
{
|
2023-08-30 11:18:00 +02:00
|
|
|
if (max < 0) {
|
|
|
|
max = 0;
|
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
|
|
|
|
cache->max_entries = max;
|
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
void mbedtls_ssl_cache_free(mbedtls_ssl_cache_context *cache)
|
2022-03-15 13:29:32 +01:00
|
|
|
{
|
|
|
|
mbedtls_ssl_cache_entry *cur, *prv;
|
|
|
|
|
|
|
|
cur = cache->chain;
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
while (cur != NULL) {
|
2022-03-15 13:29:32 +01:00
|
|
|
prv = cur;
|
|
|
|
cur = cur->next;
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
mbedtls_ssl_session_free(&prv->session);
|
2022-03-15 13:29:32 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
2023-08-30 11:18:00 +02:00
|
|
|
defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
mbedtls_free(prv->peer_cert.p);
|
2022-03-15 13:29:32 +01:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
mbedtls_free(prv);
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-08-30 11:18:00 +02:00
|
|
|
mbedtls_mutex_free(&cache->mutex);
|
2022-03-15 13:29:32 +01:00
|
|
|
#endif
|
|
|
|
cache->chain = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_SSL_CACHE_C */
|