Downgraded mbedtls and updated for latest dynarmic

This commit is contained in:
darktux
2024-04-05 01:58:29 +02:00
parent 9bb9b8b30b
commit 920e2504c3
1506 changed files with 134012 additions and 363726 deletions

View File

@@ -3,406 +3,348 @@
*
* Copyright The Mbed TLS Contributors
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
*
* This file is provided under the Apache License 2.0, or the
* GNU General Public License v2.0 or later.
*
* **********
* Apache License 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.
*
* **********
*
* **********
* GNU General Public License v2.0 or later:
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* **********
*/
/*
* These session callbacks use a simple chained list
* to store and retrieve the session information.
*/
#include "common.h"
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if defined(MBEDTLS_SSL_CACHE_C)
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#else
#include <stdlib.h>
#define mbedtls_calloc calloc
#define mbedtls_free free
#endif
#include "mbedtls/ssl_cache.h"
#include "ssl_misc.h"
#include "mbedtls/error.h"
#include <string.h>
void mbedtls_ssl_cache_init(mbedtls_ssl_cache_context *cache)
void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache )
{
memset(cache, 0, sizeof(mbedtls_ssl_cache_context));
memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) );
cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
#if defined(MBEDTLS_THREADING_C)
mbedtls_mutex_init(&cache->mutex);
mbedtls_mutex_init( &cache->mutex );
#endif
}
MBEDTLS_CHECK_RETURN_CRITICAL
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)
int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session )
{
int ret = MBEDTLS_ERR_SSL_CACHE_ENTRY_NOT_FOUND;
int ret = 1;
#if defined(MBEDTLS_HAVE_TIME)
mbedtls_time_t t = mbedtls_time(NULL);
mbedtls_time_t t = mbedtls_time( NULL );
#endif
mbedtls_ssl_cache_entry *cur;
for (cur = cache->chain; cur != NULL; cur = cur->next) {
#if defined(MBEDTLS_HAVE_TIME)
if (cache->timeout != 0 &&
(int) (t - cur->timestamp) > cache->timeout) {
continue;
}
#endif
if (session_id_len != cur->session_id_len ||
memcmp(session_id, cur->session_id,
cur->session_id_len) != 0) {
continue;
}
break;
}
if (cur != NULL) {
*dst = cur;
ret = 0;
}
return ret;
}
int mbedtls_ssl_cache_get(void *data,
unsigned char const *session_id,
size_t session_id_len,
mbedtls_ssl_session *session)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
mbedtls_ssl_cache_entry *entry;
mbedtls_ssl_cache_entry *cur, *entry;
#if defined(MBEDTLS_THREADING_C)
if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) {
return ret;
}
if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
return( 1 );
#endif
ret = ssl_cache_find_entry(cache, session_id, session_id_len, &entry);
if (ret != 0) {
cur = cache->chain;
entry = NULL;
while( cur != NULL )
{
entry = cur;
cur = cur->next;
#if defined(MBEDTLS_HAVE_TIME)
if( cache->timeout != 0 &&
(int) ( t - entry->timestamp ) > cache->timeout )
continue;
#endif
if( session->ciphersuite != entry->session.ciphersuite ||
session->compression != entry->session.compression ||
session->id_len != entry->session.id_len )
continue;
if( memcmp( session->id, entry->session.id,
entry->session.id_len ) != 0 )
continue;
memcpy( session->master, entry->session.master, 48 );
session->verify_result = entry->session.verify_result;
#if defined(MBEDTLS_X509_CRT_PARSE_C)
/*
* Restore peer certificate (without rest of the original chain)
*/
if( entry->peer_cert.p != NULL )
{
if( ( session->peer_cert = mbedtls_calloc( 1,
sizeof(mbedtls_x509_crt) ) ) == NULL )
{
ret = 1;
goto exit;
}
mbedtls_x509_crt_init( session->peer_cert );
if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p,
entry->peer_cert.len ) != 0 )
{
mbedtls_free( session->peer_cert );
session->peer_cert = NULL;
ret = 1;
goto exit;
}
}
#endif /* MBEDTLS_X509_CRT_PARSE_C */
ret = 0;
goto exit;
}
ret = mbedtls_ssl_session_load(session,
entry->session,
entry->session_len);
if (ret != 0) {
goto exit;
}
ret = 0;
exit:
#if defined(MBEDTLS_THREADING_C)
if (mbedtls_mutex_unlock(&cache->mutex) != 0) {
ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
}
if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
ret = 1;
#endif
return ret;
return( ret );
}
/* 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_zeroize_and_free(entry->session, entry->session_len);
}
/* zeroize the whole entry structure */
mbedtls_platform_zeroize(entry, sizeof(mbedtls_ssl_cache_entry));
}
MBEDTLS_CHECK_RETURN_CRITICAL
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)
int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session )
{
int ret = 1;
#if defined(MBEDTLS_HAVE_TIME)
mbedtls_time_t t = mbedtls_time(NULL), oldest = 0;
#endif /* MBEDTLS_HAVE_TIME */
mbedtls_time_t t = mbedtls_time( NULL ), oldest = 0;
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;
mbedtls_ssl_cache_entry *cur, *last;
/* 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
* at the end of this loop, and `last` will point to the last
* entry, both of which will be used later. */
#if defined(MBEDTLS_THREADING_C)
if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
return( ret );
#endif
last = NULL;
for (cur = cache->chain; cur != NULL; cur = cur->next) {
cur = cache->chain;
prv = NULL;
while( cur != NULL )
{
count++;
if (session_id_len == cur->session_id_len &&
memcmp(session_id, cur->session_id, cur->session_id_len) == 0) {
goto found;
}
last = cur;
}
/* Check 2: Is there an outdated entry in the cache?
*
* If so, overwrite it.
*
* If not, remember the oldest entry in `old` for later.
*/
#if defined(MBEDTLS_HAVE_TIME)
for (cur = cache->chain; cur != NULL; cur = cur->next) {
if (cache->timeout != 0 &&
(int) (t - cur->timestamp) > cache->timeout) {
goto found;
if( cache->timeout != 0 &&
(int) ( t - cur->timestamp ) > cache->timeout )
{
cur->timestamp = t;
break; /* expired, reuse this slot, update timestamp */
}
#endif
if (oldest == 0 || cur->timestamp < oldest) {
if( memcmp( session->id, cur->session.id, cur->session.id_len ) == 0 )
break; /* client reconnected, keep timestamp for session id */
#if defined(MBEDTLS_HAVE_TIME)
if( oldest == 0 || cur->timestamp < oldest )
{
oldest = cur->timestamp;
old = cur;
}
}
#endif /* MBEDTLS_HAVE_TIME */
#endif
/* Check 3: Is there free space in the cache? */
if (count < cache->max_entries) {
/* Create new entry */
cur = mbedtls_calloc(1, sizeof(mbedtls_ssl_cache_entry));
if (cur == NULL) {
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
}
/* Append to the end of the linked list. */
if (last == NULL) {
cache->chain = cur;
} else {
last->next = cur;
}
goto found;
prv = cur;
cur = cur->next;
}
/* 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( cur == NULL )
{
#if defined(MBEDTLS_HAVE_TIME)
if (old == NULL) {
/* This should only happen on an ill-configured cache
* with max_entries == 0. */
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
}
/*
* Reuse oldest entry if max_entries reached
*/
if( count >= cache->max_entries )
{
if( old == NULL )
{
ret = 1;
goto exit;
}
cur = old;
}
#else /* MBEDTLS_HAVE_TIME */
/* Reuse first entry in chain, but move to last place. */
if (cache->chain == NULL) {
/* This should never happen */
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
}
/*
* Reuse first entry in chain if max_entries reached,
* but move to last place
*/
if( count >= cache->max_entries )
{
if( cache->chain == NULL )
{
ret = 1;
goto exit;
}
old = cache->chain;
cache->chain = old->next;
old->next = NULL;
last->next = old;
#endif /* MBEDTLS_HAVE_TIME */
/* Now `old` points to the oldest entry to be overwritten. */
cur = old;
found:
/* If we're reusing an entry, free it first. */
if (cur->session != NULL) {
/* `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;
}
#if defined(MBEDTLS_HAVE_TIME)
cur->timestamp = t;
#endif
*dst = cur;
return 0;
}
int mbedtls_ssl_cache_set(void *data,
unsigned char const *session_id,
size_t session_id_len,
const mbedtls_ssl_session *session)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
mbedtls_ssl_cache_entry *cur;
size_t session_serialized_len = 0;
unsigned char *session_serialized = NULL;
#if defined(MBEDTLS_THREADING_C)
if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) {
return ret;
}
#endif
ret = ssl_cache_pick_writing_slot(cache,
session_id, session_id_len,
&cur);
if (ret != 0) {
goto exit;
}
/* Check how much space we need to serialize the session
* and allocate a sufficiently large buffer. */
ret = mbedtls_ssl_session_save(session, NULL, 0, &session_serialized_len);
if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
goto exit;
}
session_serialized = mbedtls_calloc(1, session_serialized_len);
if (session_serialized == NULL) {
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
goto exit;
}
/* Now serialize the session into the allocated buffer. */
ret = mbedtls_ssl_session_save(session,
session_serialized,
session_serialized_len,
&session_serialized_len);
if (ret != 0) {
goto exit;
}
if (session_id_len > sizeof(cur->session_id)) {
ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
goto exit;
}
cur->session_id_len = session_id_len;
memcpy(cur->session_id, session_id, session_id_len);
cur->session = session_serialized;
cur->session_len = session_serialized_len;
session_serialized = NULL;
ret = 0;
exit:
#if defined(MBEDTLS_THREADING_C)
if (mbedtls_mutex_unlock(&cache->mutex) != 0) {
ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
}
#endif
if (session_serialized != NULL) {
mbedtls_zeroize_and_free(session_serialized, session_serialized_len);
session_serialized = NULL;
}
return ret;
}
int mbedtls_ssl_cache_remove(void *data,
unsigned char const *session_id,
size_t session_id_len)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
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)
if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) {
return ret;
}
#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;
cur = cache->chain;
cache->chain = cur->next;
cur->next = NULL;
prv->next = cur;
}
#endif /* MBEDTLS_HAVE_TIME */
else
{
/*
* max_entries not reached, create new entry
*/
cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) );
if( cur == NULL )
{
ret = 1;
goto exit;
}
if( prv == NULL )
cache->chain = cur;
else
prv->next = cur;
}
#if defined(MBEDTLS_HAVE_TIME)
cur->timestamp = t;
#endif
}
free:
ssl_cache_entry_zeroize(entry);
mbedtls_free(entry);
memcpy( &cur->session, session, sizeof( mbedtls_ssl_session ) );
#if defined(MBEDTLS_X509_CRT_PARSE_C)
/*
* If we're reusing an entry, free its certificate first
*/
if( cur->peer_cert.p != NULL )
{
mbedtls_free( cur->peer_cert.p );
memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) );
}
/*
* Store peer certificate
*/
if( session->peer_cert != NULL )
{
cur->peer_cert.p = mbedtls_calloc( 1, session->peer_cert->raw.len );
if( cur->peer_cert.p == NULL )
{
ret = 1;
goto exit;
}
memcpy( cur->peer_cert.p, session->peer_cert->raw.p,
session->peer_cert->raw.len );
cur->peer_cert.len = session->peer_cert->raw.len;
cur->session.peer_cert = NULL;
}
#endif /* MBEDTLS_X509_CRT_PARSE_C */
ret = 0;
exit:
#if defined(MBEDTLS_THREADING_C)
if (mbedtls_mutex_unlock(&cache->mutex) != 0) {
ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
}
if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
ret = 1;
#endif
return ret;
return( ret );
}
#if defined(MBEDTLS_HAVE_TIME)
void mbedtls_ssl_cache_set_timeout(mbedtls_ssl_cache_context *cache, int timeout)
void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
{
if (timeout < 0) {
timeout = 0;
}
if( timeout < 0 ) timeout = 0;
cache->timeout = timeout;
}
#endif /* MBEDTLS_HAVE_TIME */
void mbedtls_ssl_cache_set_max_entries(mbedtls_ssl_cache_context *cache, int max)
void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
{
if (max < 0) {
max = 0;
}
if( max < 0 ) max = 0;
cache->max_entries = max;
}
void mbedtls_ssl_cache_free(mbedtls_ssl_cache_context *cache)
void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
{
mbedtls_ssl_cache_entry *cur, *prv;
cur = cache->chain;
while (cur != NULL) {
while( cur != NULL )
{
prv = cur;
cur = cur->next;
ssl_cache_entry_zeroize(prv);
mbedtls_free(prv);
mbedtls_ssl_session_free( &prv->session );
#if defined(MBEDTLS_X509_CRT_PARSE_C)
mbedtls_free( prv->peer_cert.p );
#endif /* MBEDTLS_X509_CRT_PARSE_C */
mbedtls_free( prv );
}
#if defined(MBEDTLS_THREADING_C)
mbedtls_mutex_free(&cache->mutex);
mbedtls_mutex_free( &cache->mutex );
#endif
cache->chain = NULL;
}