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,153 +0,0 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "libc/log/check.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "third_party/mbedtls/ctr_drbg.h"
#include "third_party/mbedtls/dhm.h"
#include "third_party/mbedtls/entropy.h"
#include "third_party/mbedtls/error.h"
#include "third_party/mbedtls/net_sockets.h"
#include "third_party/mbedtls/pk.h"
#include "third_party/mbedtls/platform.h"
#include "third_party/mbedtls/ssl.h"
#include "third_party/mbedtls/ssl_cache.h"
#include "third_party/mbedtls/x509_crt.h"
#define R(e) \
if ((r = e)) goto Die
char buf[1024], ebuf[100];
mbedtls_net_context server, client;
mbedtls_x509_crt cert;
mbedtls_x509_crt cacert;
mbedtls_entropy_context entropy;
mbedtls_ssl_cache_context cache;
mbedtls_ctr_drbg_context ctrdrbg;
mbedtls_pk_context pkey;
mbedtls_ssl_config conf;
mbedtls_ssl_context ssl;
void OnDebug(void *ctx, int lev, const char *file, int line, const char *str) {
fprintf(stderr, "%s:%04d: %s", file, line, str);
fflush(stderr);
}
int main(int argc, char *argv[]) {
int r, len;
system("openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem "
"-days 3650 -nodes -subj '/CN=localhost'");
mbedtls_net_init(&server);
mbedtls_net_init(&client);
mbedtls_ssl_init(&ssl);
mbedtls_ssl_config_init(&conf);
mbedtls_ssl_cache_init(&cache);
mbedtls_entropy_init(&entropy);
mbedtls_x509_crt_init(&cert);
mbedtls_pk_init(&pkey);
mbedtls_ctr_drbg_init(&ctrdrbg);
R(mbedtls_pk_parse_keyfile(&pkey, "key.pem", 0));
R(mbedtls_x509_crt_parse_file(&cert, "cert.pem"));
R(mbedtls_net_bind(&server, "0.0.0.0", "8080", MBEDTLS_NET_PROTO_TCP));
R(mbedtls_ctr_drbg_seed(&ctrdrbg, mbedtls_entropy_func, &entropy,
(const unsigned char *)"redbean", 7));
R(mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER,
MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT));
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctrdrbg);
mbedtls_ssl_conf_dbg(&conf, OnDebug, 0);
mbedtls_ssl_conf_session_cache(&conf, &cache, mbedtls_ssl_cache_get,
mbedtls_ssl_cache_set);
R(mbedtls_ssl_conf_own_cert(&conf, &cert, &pkey));
R(mbedtls_ssl_setup(&ssl, &conf));
Reset:
mbedtls_net_free(&client);
R(mbedtls_ssl_session_reset(&ssl));
R(mbedtls_net_accept(&server, &client, 0, 0, 0));
mbedtls_ssl_set_bio(&ssl, &client, mbedtls_net_send, mbedtls_net_recv, 0);
while ((r = mbedtls_ssl_handshake(&ssl)) != 0) {
if (r != MBEDTLS_ERR_SSL_WANT_READ && r != MBEDTLS_ERR_SSL_WANT_WRITE) {
printf(" failed\n ! mbedtls_ssl_handshake returned %d\n", r);
goto Reset;
}
}
do {
len = sizeof(buf) - 1;
memset(buf, 0, sizeof(buf));
r = mbedtls_ssl_read(&ssl, (void *)buf, len);
if (r == MBEDTLS_ERR_SSL_WANT_READ || r == MBEDTLS_ERR_SSL_WANT_WRITE)
continue;
if (r <= 0) {
switch (r) {
case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
printf("connection was closed gracefully\n");
break;
case MBEDTLS_ERR_NET_CONN_RESET:
printf("connection was reset by peer\n");
break;
default:
printf("mbedtls_ssl_read returned -0x%x\n", -r);
break;
}
break;
}
len = r;
printf("%d bytes read\n%s", len, buf);
if (r > 0) break;
} while (1);
len = sprintf(buf,
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"<h2>Mbed TLS Test Server</h2>\r\n"
"<p>Successful connection using: %s\r\n",
mbedtls_ssl_get_ciphersuite(&ssl));
while ((r = mbedtls_ssl_write(&ssl, (void *)buf, len)) <= 0) {
if (r == MBEDTLS_ERR_NET_CONN_RESET) {
printf("failed\n ! peer closed the connection\n");
goto Reset;
}
if (r != MBEDTLS_ERR_SSL_WANT_READ && r != MBEDTLS_ERR_SSL_WANT_WRITE) {
printf("failed\n ! mbedtls_ssl_write returned %d\n", r);
exit(1);
}
}
while ((r = mbedtls_ssl_close_notify(&ssl)) < 0) {
if (r != MBEDTLS_ERR_SSL_WANT_READ && r != MBEDTLS_ERR_SSL_WANT_WRITE) {
printf("error: mbedtls_ssl_close_notify returned %d\n", r);
goto Reset;
}
}
printf("ok\n");
r = 0;
goto Reset;
mbedtls_net_free(&client);
mbedtls_net_free(&server);
mbedtls_x509_crt_free(&cert);
mbedtls_pk_free(&pkey);
mbedtls_ssl_cache_free(&cache);
return 0;
Die:
mbedtls_strerror(r, ebuf, 100);
printf("last error was: %d - %s\n", r, ebuf);
return 1;
}