2021-07-19 21:55:20 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│
|
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright The Mbed TLS Contributors │
|
|
|
|
│ │
|
|
|
|
│ 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. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2021-07-12 06:17:47 +00:00
|
|
|
#include "libc/log/log.h"
|
2024-05-05 06:05:36 +00:00
|
|
|
#include "libc/time.h"
|
2021-06-24 19:31:26 +00:00
|
|
|
#include "third_party/mbedtls/common.h"
|
|
|
|
#include "third_party/mbedtls/platform.h"
|
|
|
|
#include "third_party/mbedtls/ssl_cache.h"
|
|
|
|
#include "third_party/mbedtls/ssl_internal.h"
|
Release Cosmopolitan v3.3
This change upgrades to GCC 12.3 and GNU binutils 2.42. The GNU linker
appears to have changed things so that only a single de-duplicated str
table is present in the binary, and it gets placed wherever the linker
wants, regardless of what the linker script says. To cope with that we
need to stop using .ident to embed licenses. As such, this change does
significant work to revamp how third party licenses are defined in the
codebase, using `.section .notice,"aR",@progbits`.
This new GCC 12.3 toolchain has support for GNU indirect functions. It
lets us support __target_clones__ for the first time. This is used for
optimizing the performance of libc string functions such as strlen and
friends so far on x86, by ensuring AVX systems favor a second codepath
that uses VEX encoding. It shaves some latency off certain operations.
It's a useful feature to have for scientific computing for the reasons
explained by the test/libcxx/openmp_test.cc example which compiles for
fifteen different microarchitectures. Thanks to the upgrades, it's now
also possible to use newer instruction sets, such as AVX512FP16, VNNI.
Cosmo now uses the %gs register on x86 by default for TLS. Doing it is
helpful for any program that links `cosmo_dlopen()`. Such programs had
to recompile their binaries at startup to change the TLS instructions.
That's not great, since it means every page in the executable needs to
be faulted. The work of rewriting TLS-related x86 opcodes, is moved to
fixupobj.com instead. This is great news for MacOS x86 users, since we
previously needed to morph the binary every time for that platform but
now that's no longer necessary. The only platforms where we need fixup
of TLS x86 opcodes at runtime are now Windows, OpenBSD, and NetBSD. On
Windows we morph TLS to point deeper into the TIB, based on a TlsAlloc
assignment, and on OpenBSD/NetBSD we morph %gs back into %fs since the
kernels do not allow us to specify a value for the %gs register.
OpenBSD users are now required to use APE Loader to run Cosmo binaries
and assimilation is no longer possible. OpenBSD kernel needs to change
to allow programs to specify a value for the %gs register, or it needs
to stop marking executable pages loaded by the kernel as mimmutable().
This release fixes __constructor__, .ctor, .init_array, and lastly the
.preinit_array so they behave the exact same way as glibc.
We no longer use hex constants to define math.h symbols like M_PI.
2024-02-20 19:12:09 +00:00
|
|
|
__static_yoink("mbedtls_notice");
|
2021-06-16 02:52:02 +00:00
|
|
|
|
2021-06-15 18:39:36 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_CACHE_C)
|
|
|
|
|
|
|
|
void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache )
|
|
|
|
{
|
2021-07-19 21:55:20 +00:00
|
|
|
mbedtls_platform_zeroize( cache, sizeof( mbedtls_ssl_cache_context ) );
|
2021-06-15 18:39:36 +00:00
|
|
|
cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
|
|
|
|
cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session )
|
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
mbedtls_time_t t = mbedtls_time( NULL );
|
|
|
|
#endif
|
|
|
|
mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
|
|
|
|
mbedtls_ssl_cache_entry *cur, *entry;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-09-28 05:58:51 +00:00
|
|
|
if( timingsafe_bcmp( session->id, entry->session.id,
|
|
|
|
entry->session.id_len ) != 0 )
|
2021-06-15 18:39:36 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_session_copy( session, &entry->session );
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
|
|
|
defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
/*
|
|
|
|
* Restore peer certificate (without rest of the original chain)
|
|
|
|
*/
|
|
|
|
if( entry->peer_cert.p != NULL )
|
|
|
|
{
|
|
|
|
/* `session->peer_cert` is NULL after the call to
|
|
|
|
* mbedtls_ssl_session_copy(), because cache entries
|
|
|
|
* have the `peer_cert` field set to 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 && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
|
|
|
|
cur = cache->chain;
|
|
|
|
prv = NULL;
|
|
|
|
|
|
|
|
while( cur != NULL )
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
if( cache->timeout != 0 &&
|
|
|
|
(int) ( t - cur->timestamp ) > cache->timeout )
|
|
|
|
{
|
|
|
|
cur->timestamp = t;
|
|
|
|
break; /* expired, reuse this slot, update timestamp */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-09-28 05:58:51 +00:00
|
|
|
if( timingsafe_bcmp( session->id, cur->session.id, cur->session.id_len ) == 0 )
|
2021-06-15 18:39:36 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
prv = cur;
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( cur == NULL )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
/*
|
|
|
|
* 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 if max_entries reached,
|
|
|
|
* but move to last place
|
|
|
|
*/
|
|
|
|
if( count >= cache->max_entries )
|
|
|
|
{
|
|
|
|
if( cache->chain == NULL )
|
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
|
|
|
defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
/*
|
|
|
|
* If we're reusing an entry, free its certificate first
|
|
|
|
*/
|
|
|
|
if( cur->peer_cert.p != NULL )
|
|
|
|
{
|
|
|
|
mbedtls_free( cur->peer_cert.p );
|
2021-07-19 21:55:20 +00:00
|
|
|
mbedtls_platform_zeroize( &cur->peer_cert, sizeof(mbedtls_x509_buf) );
|
2021-06-15 18:39:36 +00: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. */
|
|
|
|
ret = mbedtls_ssl_session_copy( &cur->session, session );
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
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. */
|
|
|
|
if( cur->session.peer_cert != NULL )
|
|
|
|
{
|
|
|
|
cur->peer_cert.p =
|
|
|
|
mbedtls_calloc( 1, cur->session.peer_cert->raw.len );
|
|
|
|
if( cur->peer_cert.p == NULL )
|
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( cur->peer_cert.p,
|
|
|
|
cur->session.peer_cert->raw.p,
|
|
|
|
cur->session.peer_cert->raw.len );
|
|
|
|
cur->peer_cert.len = session->peer_cert->raw.len;
|
|
|
|
|
|
|
|
mbedtls_x509_crt_free( cur->session.peer_cert );
|
|
|
|
mbedtls_free( cur->session.peer_cert );
|
|
|
|
cur->session.peer_cert = NULL;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
|
|
|
|
{
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
if( max < 0 ) max = 0;
|
|
|
|
|
|
|
|
cache->max_entries = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
|
|
|
|
{
|
|
|
|
mbedtls_ssl_cache_entry *cur, *prv;
|
|
|
|
|
|
|
|
cur = cache->chain;
|
|
|
|
|
|
|
|
while( cur != NULL )
|
|
|
|
{
|
|
|
|
prv = cur;
|
|
|
|
cur = cur->next;
|
|
|
|
|
|
|
|
mbedtls_ssl_session_free( &prv->session );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
|
|
|
defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
mbedtls_free( prv->peer_cert.p );
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
|
|
|
|
mbedtls_free( prv );
|
|
|
|
}
|
|
|
|
|
|
|
|
cache->chain = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_SSL_CACHE_C */
|