mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 11:37:35 +00:00
398f0c16fb
This change makes SSL virtual hosting possible. You can now load
multiple certificates for multiple domains and redbean will just
figure out which one to use, even if you only have 1 ip address.
You can also use a jumbo certificate that lists all your domains
in the the subject alternative names.
This change also makes performance improvements to MbedTLS. Here
are some benchmarks vs. cc1920749e
BEFORE AFTER (microsecs)
suite_ssl.com 2512881 191738 13.11x faster
suite_pkparse.com 36291 3295 11.01x faster
suite_x509parse.com 854669 120293 7.10x faster
suite_pkwrite.com 6549 1265 5.18x faster
suite_ecdsa.com 53347 18778 2.84x faster
suite_pk.com 49051 18717 2.62x faster
suite_ecdh.com 19535 9502 2.06x faster
suite_shax.com 15848 7965 1.99x faster
suite_rsa.com 353257 184828 1.91x faster
suite_x509write.com 162646 85733 1.90x faster
suite_ecp.com 20503 11050 1.86x faster
suite_hmac_drbg.no_reseed.com 19528 11417 1.71x faster
suite_hmac_drbg.nopr.com 12460 8010 1.56x faster
suite_mpi.com 687124 442661 1.55x faster
suite_hmac_drbg.pr.com 11890 7752 1.53x faster
There aren't any special tricks to the performance imporvements.
It's mostly due to code cleanup, assembly and intel instructions
like mulx, adox, and adcx.
412 lines
13 KiB
C
412 lines
13 KiB
C
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│
|
|
│vi: set net 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. │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "third_party/mbedtls/common.h"
|
|
#include "third_party/mbedtls/debug.h"
|
|
#include "third_party/mbedtls/error.h"
|
|
#include "third_party/mbedtls/platform.h"
|
|
|
|
asm(".ident\t\"\\n\\n\
|
|
Mbed TLS (Apache 2.0)\\n\
|
|
Copyright ARM Limited\\n\
|
|
Copyright Mbed TLS Contributors\"");
|
|
asm(".include \"libc/disclaimer.inc\"");
|
|
/* clang-format off */
|
|
|
|
char mbedtls_debug_threshold;
|
|
|
|
void mbedtls_debug_set_threshold( int threshold )
|
|
{
|
|
mbedtls_debug_threshold = threshold;
|
|
}
|
|
|
|
#if defined(MBEDTLS_DEBUG_C)
|
|
|
|
#define DEBUG_BUF_SIZE 512
|
|
|
|
/*
|
|
* All calls to f_dbg must be made via this function
|
|
*/
|
|
static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
|
|
const char *file, int line,
|
|
const char *str )
|
|
{
|
|
ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
|
|
}
|
|
|
|
MBEDTLS_PRINTF_ATTRIBUTE(5, 6)
|
|
void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
|
|
const char *file, int line,
|
|
const char *format, ... )
|
|
{
|
|
va_list argp;
|
|
char str[DEBUG_BUF_SIZE];
|
|
int ret = MBEDTLS_ERR_THIS_CORRUPTION;
|
|
|
|
if( NULL == ssl ||
|
|
NULL == ssl->conf ||
|
|
NULL == ssl->conf->f_dbg ||
|
|
level > mbedtls_debug_threshold )
|
|
{
|
|
return;
|
|
}
|
|
|
|
va_start( argp, format );
|
|
ret = mbedtls_vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
|
|
va_end( argp );
|
|
|
|
if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
|
|
{
|
|
str[ret] = '\0';
|
|
}
|
|
|
|
debug_send_line( ssl, level, file, line, str );
|
|
}
|
|
|
|
void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
|
|
const char *file, int line,
|
|
const char *text, int ret )
|
|
{
|
|
char str[DEBUG_BUF_SIZE];
|
|
|
|
if( NULL == ssl ||
|
|
NULL == ssl->conf ||
|
|
NULL == ssl->conf->f_dbg ||
|
|
level > mbedtls_debug_threshold )
|
|
{
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* With non-blocking I/O and examples that just retry immediately,
|
|
* the logs would be quickly flooded with WANT_READ, so ignore that.
|
|
* Don't ignore WANT_WRITE however, since is is usually rare.
|
|
*/
|
|
if( ret == MBEDTLS_ERR_SSL_WANT_READ )
|
|
return;
|
|
|
|
mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)",
|
|
text, ret, (unsigned int) -ret );
|
|
|
|
debug_send_line( ssl, level, file, line, str );
|
|
}
|
|
|
|
void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
|
|
const char *file, int line, const char *text,
|
|
const unsigned char *buf, size_t len )
|
|
{
|
|
char str[DEBUG_BUF_SIZE];
|
|
char txt[17];
|
|
size_t i, idx = 0;
|
|
|
|
if( NULL == ssl ||
|
|
NULL == ssl->conf ||
|
|
NULL == ssl->conf->f_dbg ||
|
|
level > mbedtls_debug_threshold )
|
|
{
|
|
return;
|
|
}
|
|
|
|
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)",
|
|
text, (unsigned int) len );
|
|
|
|
debug_send_line( ssl, level, file, line, str );
|
|
|
|
idx = 0;
|
|
mbedtls_platform_zeroize( txt, sizeof( txt ) );
|
|
for( i = 0; i < len; i++ )
|
|
{
|
|
if( i >= 4096 )
|
|
break;
|
|
|
|
if( i % 16 == 0 )
|
|
{
|
|
if( i > 0 )
|
|
{
|
|
mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s", txt );
|
|
debug_send_line( ssl, level, file, line, str );
|
|
|
|
idx = 0;
|
|
mbedtls_platform_zeroize( txt, sizeof( txt ) );
|
|
}
|
|
|
|
idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
|
|
(unsigned int) i );
|
|
|
|
}
|
|
|
|
idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
|
|
(unsigned int) buf[i] );
|
|
txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
|
|
}
|
|
|
|
if( len > 0 )
|
|
{
|
|
for( /* i = i */; i % 16 != 0; i++ )
|
|
idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
|
|
|
|
mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s", txt );
|
|
debug_send_line( ssl, level, file, line, str );
|
|
}
|
|
}
|
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
|
|
const char *file, int line,
|
|
const char *text, const mbedtls_ecp_point *X )
|
|
{
|
|
char str[DEBUG_BUF_SIZE];
|
|
|
|
if( NULL == ssl ||
|
|
NULL == ssl->conf ||
|
|
NULL == ssl->conf->f_dbg ||
|
|
level > mbedtls_debug_threshold )
|
|
{
|
|
return;
|
|
}
|
|
|
|
mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
|
|
mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
|
|
|
|
mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
|
|
mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
|
|
}
|
|
#endif /* MBEDTLS_ECP_C */
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
|
|
const char *file, int line,
|
|
const char *text, const mbedtls_mpi *X )
|
|
{
|
|
char str[DEBUG_BUF_SIZE];
|
|
int j, k, zeros = 1;
|
|
size_t i, n, idx = 0;
|
|
|
|
if( NULL == ssl ||
|
|
NULL == ssl->conf ||
|
|
NULL == ssl->conf->f_dbg ||
|
|
NULL == X ||
|
|
level > mbedtls_debug_threshold )
|
|
{
|
|
return;
|
|
}
|
|
|
|
/* TODO(jart): wut */
|
|
if (!X->n) {
|
|
mbedtls_snprintf(str, sizeof(str), "value of '%s' has empty X->n", text);
|
|
debug_send_line(ssl, level, file, line, str);
|
|
return;
|
|
}
|
|
|
|
for( n = X->n - 1; n > 0; n-- )
|
|
if( X->p[n] != 0 )
|
|
break;
|
|
|
|
for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
|
|
if( ( ( X->p[n] >> j ) & 1 ) != 0 )
|
|
break;
|
|
|
|
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:",
|
|
text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
|
|
|
|
debug_send_line( ssl, level, file, line, str );
|
|
|
|
idx = 0;
|
|
for( i = n + 1, j = 0; i > 0; i-- )
|
|
{
|
|
if( zeros && X->p[i - 1] == 0 )
|
|
continue;
|
|
|
|
for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
|
|
{
|
|
if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
|
|
continue;
|
|
else
|
|
zeros = 0;
|
|
|
|
if( j % 16 == 0 )
|
|
{
|
|
if( j > 0 )
|
|
{
|
|
debug_send_line( ssl, level, file, line, str );
|
|
idx = 0;
|
|
}
|
|
}
|
|
|
|
idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
|
|
( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
|
|
|
|
j++;
|
|
}
|
|
|
|
}
|
|
|
|
if( zeros == 1 )
|
|
idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
|
|
|
|
debug_send_line( ssl, level, file, line, str );
|
|
}
|
|
#endif /* MBEDTLS_BIGNUM_C */
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
|
|
const char *file, int line,
|
|
const char *text, const mbedtls_pk_context *pk )
|
|
{
|
|
size_t i;
|
|
mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
|
|
char name[16];
|
|
|
|
mbedtls_platform_zeroize( items, sizeof( items ) );
|
|
|
|
if( mbedtls_pk_debug( pk, items ) != 0 )
|
|
{
|
|
debug_send_line( ssl, level, file, line,
|
|
"invalid PK context" );
|
|
return;
|
|
}
|
|
|
|
for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
|
|
{
|
|
if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
|
|
return;
|
|
|
|
mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
|
|
name[sizeof( name ) - 1] = '\0';
|
|
|
|
if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
|
|
mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
|
|
else
|
|
#if defined(MBEDTLS_ECP_C)
|
|
if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
|
|
mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
|
|
else
|
|
#endif
|
|
debug_send_line( ssl, level, file, line,
|
|
"should not happen" );
|
|
}
|
|
}
|
|
|
|
static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
|
|
const char *file, int line, const char *text )
|
|
{
|
|
char str[DEBUG_BUF_SIZE];
|
|
const char *start, *cur;
|
|
|
|
start = text;
|
|
for( cur = text; *cur != '\0'; cur++ )
|
|
{
|
|
if( *cur == '\n' )
|
|
{
|
|
size_t len = cur - start;
|
|
if( len > DEBUG_BUF_SIZE - 1 )
|
|
len = DEBUG_BUF_SIZE - 1;
|
|
|
|
memcpy( str, start, len );
|
|
str[len] = '\0';
|
|
|
|
debug_send_line( ssl, level, file, line, str );
|
|
|
|
start = cur + 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
|
|
const char *file, int line,
|
|
const char *text, const mbedtls_x509_crt *crt )
|
|
{
|
|
char str[DEBUG_BUF_SIZE];
|
|
int i = 0;
|
|
|
|
if( NULL == ssl ||
|
|
NULL == ssl->conf ||
|
|
NULL == ssl->conf->f_dbg ||
|
|
NULL == crt ||
|
|
level > mbedtls_debug_threshold )
|
|
{
|
|
return;
|
|
}
|
|
|
|
while( crt != NULL )
|
|
{
|
|
char buf[1024];
|
|
|
|
mbedtls_snprintf( str, sizeof( str ), "%s #%d:", text, ++i );
|
|
debug_send_line( ssl, level, file, line, str );
|
|
|
|
mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
|
|
debug_print_line_by_line( ssl, level, file, line, buf );
|
|
|
|
debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
|
|
|
|
crt = crt->next;
|
|
}
|
|
}
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
|
|
|
#if defined(MBEDTLS_ECDH_C)
|
|
static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl,
|
|
int level, const char *file,
|
|
int line,
|
|
const mbedtls_ecdh_context *ecdh,
|
|
mbedtls_debug_ecdh_attr attr )
|
|
{
|
|
#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
|
|
const mbedtls_ecdh_context* ctx = ecdh;
|
|
#else
|
|
const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh;
|
|
#endif
|
|
|
|
switch( attr )
|
|
{
|
|
case MBEDTLS_DEBUG_ECDH_Q:
|
|
mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q",
|
|
&ctx->Q );
|
|
break;
|
|
case MBEDTLS_DEBUG_ECDH_QP:
|
|
mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp",
|
|
&ctx->Qp );
|
|
break;
|
|
case MBEDTLS_DEBUG_ECDH_Z:
|
|
mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z",
|
|
&ctx->z );
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
|
|
const char *file, int line,
|
|
const mbedtls_ecdh_context *ecdh,
|
|
mbedtls_debug_ecdh_attr attr )
|
|
{
|
|
#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
|
|
mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr );
|
|
#else
|
|
switch( ecdh->var )
|
|
{
|
|
default:
|
|
mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh,
|
|
attr );
|
|
}
|
|
#endif
|
|
}
|
|
#endif /* MBEDTLS_ECDH_C */
|
|
|
|
#endif /* MBEDTLS_DEBUG_C */
|