Add SSL to redbean

Your redbean can now interoperate with clients that require TLS crypto.
This is accomplished using a protocol polyglot that lets us distinguish
between HTTP and HTTPS regardless of the port number. Certificates will
be generated automatically, if none are supplied by the user. Footprint
increases by only a few hundred kb so redbean in MODY=tiny is now 1.0mb

- Add lseek() polyfills for ZIP executable
- Automatically polyfill /tmp/FOO paths on NT
- Fix readdir() / ftw() / nftw() bugs on Windows
- Introduce -B flag for slower SSL that's stronger
- Remove mbedtls features Cosmopolitan doesn't need
- Have base64 decoder support the uri-safe alternative
- Remove Truncated HMAC because it's forbidden by the IETF
- Add all the mbedtls test suites and make them go 3x faster
- Support opendir() / readdir() / closedir() on ZIP executable
- Use Everest for ECDHE-ECDSA because it's so good it's so good
- Add tinier implementation of sha1 since it's not worth the rom
- Add chi-square monte-carlo mean correlation tests for getrandom()
- Source entropy on Windows from the proper interface everyone uses

We're continuing to outperform NGINX and other servers on raw message
throughput. Using SSL means that instead of 1,000,000 qps you can get
around 300,000 qps. However redbean isn't as fast as NGINX yet at SSL
handshakes, since redbean can do 2,627 per second and NGINX does 4.3k

Right now, the SSL UX story works best if you give your redbean a key
signing key since that can be easily generated by openssl using a one
liner then redbean will do all the things that are impossibly hard to
do like signing ecdsa and rsa certificates that'll work in chrome. We
should integrate the let's encrypt acme protocol in the future.

Live Demo: https://redbean.justine.lol/
Root Cert: https://redbean.justine.lol/redbean1.crt
This commit is contained in:
Justine Tunney 2021-06-24 12:31:26 -07:00
parent 1beeb7a829
commit cc1920749e
1032 changed files with 152673 additions and 69310 deletions

View file

@ -1,10 +1,15 @@
/* clang-format off */
#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 The Mbed TLS Contributors\"");
Copyright ARM Limited\\n\
Copyright Mbed TLS Contributors\"");
asm(".include \"libc/disclaimer.inc\"");
/* clang-format off */
/*
* Debugging routines
*
@ -24,37 +29,17 @@ asm(".include \"libc/disclaimer.inc\"");
* limitations under the License.
*/
#include "third_party/mbedtls/common.h"
#if defined(MBEDTLS_DEBUG_C)
#if defined(MBEDTLS_PLATFORM_C)
#include "third_party/mbedtls/platform.h"
#else
#define mbedtls_calloc calloc
#define mbedtls_free free
#define mbedtls_time_t time_t
#define mbedtls_snprintf snprintf
#define mbedtls_vsnprintf vsnprintf
#endif
#include "third_party/mbedtls/debug.h"
#include "third_party/mbedtls/error.h"
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
!defined(inline) && !defined(__cplusplus)
#define inline __inline
#endif
#define DEBUG_BUF_SIZE 512
static int debug_threshold = 0;
char mbedtls_debug_threshold;
void mbedtls_debug_set_threshold( int threshold )
{
debug_threshold = 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
*/
@ -62,18 +47,7 @@ static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
const char *file, int line,
const char *str )
{
/*
* If in a threaded environment, we need a thread identifier.
* Since there is no portable way to get one, use the address of the ssl
* context instead, as it shouldn't be shared between threads.
*/
#if defined(MBEDTLS_THREADING_C)
char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str );
ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
#else
ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
#endif
}
MBEDTLS_PRINTF_ATTRIBUTE(5, 6)
@ -88,7 +62,7 @@ void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
if( NULL == ssl ||
NULL == ssl->conf ||
NULL == ssl->conf->f_dbg ||
level > debug_threshold )
level > mbedtls_debug_threshold )
{
return;
}
@ -99,8 +73,7 @@ void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
{
str[ret] = '\n';
str[ret + 1] = '\0';
str[ret] = '\0';
}
debug_send_line( ssl, level, file, line, str );
@ -115,7 +88,7 @@ void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
if( NULL == ssl ||
NULL == ssl->conf ||
NULL == ssl->conf->f_dbg ||
level > debug_threshold )
level > mbedtls_debug_threshold )
{
return;
}
@ -128,7 +101,7 @@ void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
if( ret == MBEDTLS_ERR_SSL_WANT_READ )
return;
mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)",
text, ret, (unsigned int) -ret );
debug_send_line( ssl, level, file, line, str );
@ -145,12 +118,12 @@ void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
if( NULL == ssl ||
NULL == ssl->conf ||
NULL == ssl->conf->f_dbg ||
level > debug_threshold )
level > mbedtls_debug_threshold )
{
return;
}
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)",
text, (unsigned int) len );
debug_send_line( ssl, level, file, line, str );
@ -166,7 +139,7 @@ void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
{
if( i > 0 )
{
mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s", txt );
debug_send_line( ssl, level, file, line, str );
idx = 0;
@ -188,7 +161,7 @@ void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
for( /* i = i */; i % 16 != 0; i++ )
idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s", txt );
debug_send_line( ssl, level, file, line, str );
}
}
@ -203,7 +176,7 @@ void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
if( NULL == ssl ||
NULL == ssl->conf ||
NULL == ssl->conf->f_dbg ||
level > debug_threshold )
level > mbedtls_debug_threshold )
{
return;
}
@ -218,8 +191,8 @@ void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
#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 )
const char *file, int line,
const char *text, const mbedtls_mpi *X )
{
char str[DEBUG_BUF_SIZE];
int j, k, zeros = 1;
@ -229,11 +202,18 @@ void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
NULL == ssl->conf ||
NULL == ssl->conf->f_dbg ||
NULL == X ||
level > debug_threshold )
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;
@ -242,7 +222,7 @@ void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
if( ( ( X->p[n] >> j ) & 1 ) != 0 )
break;
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
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 );
@ -264,7 +244,6 @@ void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
{
if( j > 0 )
{
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
debug_send_line( ssl, level, file, line, str );
idx = 0;
}
@ -281,7 +260,6 @@ void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
if( zeros == 1 )
idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
debug_send_line( ssl, level, file, line, str );
}
#endif /* MBEDTLS_BIGNUM_C */
@ -300,7 +278,7 @@ static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
if( mbedtls_pk_debug( pk, items ) != 0 )
{
debug_send_line( ssl, level, file, line,
"invalid PK context\n" );
"invalid PK context" );
return;
}
@ -321,7 +299,7 @@ static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
else
#endif
debug_send_line( ssl, level, file, line,
"should not happen\n" );
"should not happen" );
}
}
@ -336,7 +314,7 @@ static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
{
if( *cur == '\n' )
{
size_t len = cur - start + 1;
size_t len = cur - start;
if( len > DEBUG_BUF_SIZE - 1 )
len = DEBUG_BUF_SIZE - 1;
@ -361,7 +339,7 @@ void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
NULL == ssl->conf ||
NULL == ssl->conf->f_dbg ||
NULL == crt ||
level > debug_threshold )
level > mbedtls_debug_threshold )
{
return;
}
@ -370,7 +348,7 @@ void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
{
char buf[1024];
mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
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 );