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

@ -8,58 +8,116 @@
*/
#endif
#include "libc/calls/calls.h"
#include "libc/calls/sigbits.h"
#include "libc/calls/struct/rusage.h"
#include "libc/errno.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/math.h"
#include "libc/runtime/clktck.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/sig.h"
#include "libc/time/time.h"
void Show(const char *name, long measurement, const char *unit) {
fprintf(stderr, "%-*s%,*d %s\n", 32, name, 32, measurement, unit);
#define PREFIX "\e[1mRL\e[0m: "
static void PrintResourceReport(struct rusage *ru) {
long utime, stime;
long double ticks;
if (ru->ru_maxrss) {
fprintf(stderr, "%sballooned to %,ldkb in size\n", PREFIX, ru->ru_maxrss);
}
if ((utime = ru->ru_utime.tv_sec * 1000000 + ru->ru_utime.tv_usec) |
(stime = ru->ru_stime.tv_sec * 1000000 + ru->ru_stime.tv_usec)) {
ticks = ceill((long double)(utime + stime) / (1000000.L / CLK_TCK));
fprintf(stderr, "%sneeded %,ldµs cpu (%d%% kernel)\n", PREFIX,
utime + stime, (int)((long double)stime / (utime + stime) * 100));
if (ru->ru_idrss) {
fprintf(stderr, "%sneeded %,ldkb memory on average\n", PREFIX,
lroundl(ru->ru_idrss / ticks));
}
if (ru->ru_isrss) {
fprintf(stderr, "%sneeded %,ldkb stack on average\n", PREFIX,
lroundl(ru->ru_isrss / ticks));
}
if (ru->ru_ixrss) {
fprintf(stderr, "%smapped %,ldkb shared on average\n", PREFIX,
lroundl(ru->ru_ixrss / ticks));
}
}
if (ru->ru_minflt || ru->ru_majflt) {
fprintf(stderr, "%scaused %,ld page faults (%d%% memcpy)\n", PREFIX,
ru->ru_minflt + ru->ru_majflt,
(int)((long double)ru->ru_minflt / (ru->ru_minflt + ru->ru_majflt) *
100));
}
if (ru->ru_nvcsw + ru->ru_nivcsw > 1) {
fprintf(stderr, "%s%,ld context switches (%d%% consensual)\n", PREFIX,
ru->ru_nvcsw + ru->ru_nivcsw,
(int)((long double)ru->ru_nvcsw / (ru->ru_nvcsw + ru->ru_nivcsw) *
100));
}
if (ru->ru_msgrcv || ru->ru_msgsnd) {
fprintf(stderr, "%sreceived %,ld message%s and sent %,ld\n", PREFIX,
ru->ru_msgrcv, ru->ru_msgrcv == 1 ? "" : "s", ru->ru_msgsnd);
}
if (ru->ru_inblock || ru->ru_oublock) {
fprintf(stderr, "%sperformed %,ld read%s and %,ld write i/o operations\n",
PREFIX, ru->ru_inblock, ru->ru_inblock == 1 ? "" : "s",
ru->ru_oublock);
}
if (ru->ru_nsignals) {
fprintf(stderr, "%sreceived %,ld signals\n", PREFIX, ru->ru_nsignals);
}
if (ru->ru_nswap) {
fprintf(stderr, "%sgot swapped %,ld times\n", PREFIX, ru->ru_nswap);
}
}
long TvToNs(struct timeval tv) {
return tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
}
struct rusage rusage;
int main(int argc, char *argv[]) {
int pid, wstatus;
long double ts1, ts2;
struct rusage rusage;
sigset_t chldmask, savemask;
struct sigaction dflt, ignore, saveint, savequit;
if (argc < 2) {
fprintf(stderr, "Usage: %s PROG [ARGS...]\n", argv[0]);
return 1;
}
memset(&rusage, -1, sizeof(rusage));
CHECK_GT(argc, 1);
dflt.sa_flags = 0;
dflt.sa_handler = SIG_DFL;
sigemptyset(&dflt.sa_mask);
ignore.sa_flags = 0;
ignore.sa_handler = SIG_IGN;
sigemptyset(&ignore.sa_mask);
sigaction(SIGINT, &ignore, &saveint);
sigaction(SIGQUIT, &ignore, &savequit);
sigemptyset(&chldmask);
sigaddset(&chldmask, SIGCHLD);
sigprocmask(SIG_BLOCK, &chldmask, &savemask);
ts1 = nowl();
if (!(pid = vfork())) {
CHECK_NE(-1, (pid = vfork()));
if (!pid) {
sigaction(SIGINT, &dflt, 0);
sigaction(SIGQUIT, &dflt, 0);
sigprocmask(SIG_SETMASK, &savemask, 0);
execvp(argv[1], argv + 1);
abort();
_Exit(127);
}
while (wait4(pid, &wstatus, 0, &rusage) == -1) {
CHECK_EQ(EINTR, errno);
}
CHECK_NE(-1, wait4(pid, &wstatus, 0, &rusage));
ts2 = nowl();
Show("wall time", lroundl((ts2 - ts1) * 1e9l), "ns");
Show("user time", TvToNs(rusage.ru_utime), "ns");
Show("sys time", TvToNs(rusage.ru_stime), "ns");
Show("maximum resident set size", rusage.ru_maxrss, "");
Show("integral shared memory size", rusage.ru_ixrss, "");
Show("integral unshared data size", rusage.ru_idrss, "");
Show("integral unshared stack size", rusage.ru_isrss, "");
Show("minor page faults", rusage.ru_minflt, "");
Show("major page faults", rusage.ru_majflt, "");
Show("swaps", rusage.ru_nswap, "");
Show("block input ops", rusage.ru_inblock, "");
Show("block output ops", rusage.ru_oublock, "");
Show("ipc messages sent", rusage.ru_msgsnd, "");
Show("ipc messages received", rusage.ru_msgrcv, "");
Show("signals received", rusage.ru_nsignals, "");
Show("voluntary context switches", rusage.ru_nvcsw, "");
Show("involuntary context switches", rusage.ru_nivcsw, "");
sigaction(SIGINT, &saveint, 0);
sigaction(SIGQUIT, &savequit, 0);
sigprocmask(SIG_SETMASK, &savemask, 0);
fprintf(stderr, "%stook %,ldµs wall time\n", PREFIX,
(int64_t)((ts2 - ts1) * 1e6));
PrintResourceReport(&rusage);
if (WIFEXITED(wstatus)) {
return WEXITSTATUS(wstatus);
} else {