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

@ -35,7 +35,7 @@ static struct HostsTxtInitialStaticMemory {
char strings[64];
} g_hoststxt_init;
static textwindows noinline char *getnthoststxtpath(char *pathbuf,
static textwindows noinline char *GetNtHostsTxtPath(char *pathbuf,
uint32_t size) {
const char *const kWinHostsPath = "\\drivers\\etc\\hosts";
uint32_t len = GetSystemDirectoryA(&pathbuf[0], size);
@ -49,7 +49,7 @@ static textwindows noinline char *getnthoststxtpath(char *pathbuf,
}
/**
* Returns parsed sorted singleton hardcoded hostnameip4 map.
* Returns hosts.txt map.
*
* @note yoinking realloc() ensures there's no size limits
*/
@ -68,13 +68,12 @@ const struct HostsTxt *GetHostsTxt(void) {
__cxa_atexit(FreeHostsTxt, &g_hoststxt, NULL);
path = "/etc/hosts";
if (IsWindows()) {
path = firstnonnull(getnthoststxtpath(pathbuf, ARRAYLEN(pathbuf)), path);
path = firstnonnull(GetNtHostsTxtPath(pathbuf, ARRAYLEN(pathbuf)), path);
}
if (!(f = fopen(path, "r")) || ParseHostsTxt(g_hoststxt, f) == -1) {
/* TODO(jart): Elevate robustness. */
}
fclose(f);
SortHostsTxt(g_hoststxt);
}
return g_hoststxt;
}

View file

@ -29,7 +29,6 @@ struct HostsTxt {
const struct HostsTxt *GetHostsTxt(void) returnsnonnull;
void FreeHostsTxt(struct HostsTxt **) paramsnonnull();
int ParseHostsTxt(struct HostsTxt *, FILE *) paramsnonnull();
void SortHostsTxt(struct HostsTxt *) paramsnonnull();
int ResolveHostsTxt(const struct HostsTxt *, int, const char *,
struct sockaddr *, uint32_t, const char **)
paramsnonnull((1, 3));

View file

@ -59,7 +59,7 @@ int ResolveDns(const struct ResolvConf *resolvconf, int af, const char *name,
if (!resolvconf->nameservers.i) return 0;
memset(&h, 0, sizeof(h));
rc = ebadmsg();
h.id = rand32();
h.id = rand64();
h.bf1 = 1; /* recursion desired */
h.qdcount = 1;
q.qname = name;

View file

@ -68,7 +68,7 @@ int ResolveDnsReverse(const struct ResolvConf *resolvconf, int af,
if (!resolvconf->nameservers.i) return 0;
memset(&h, 0, sizeof(h));
rc = ebadmsg();
h.id = rand32();
h.id = rand64();
h.bf1 = 1; /* recursion desired */
h.qdcount = 1;
q.qname = name;

View file

@ -1,8 +1,27 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/alg/alg.h"
#include "libc/bits/bits.h"
#include "libc/dns/consts.h"
#include "libc/dns/dns.h"
#include "libc/dns/hoststxt.h"
#include "libc/fmt/fmt.h"
#include "libc/sock/sock.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/af.h"
@ -16,30 +35,18 @@
* @param ip is IP address in binary (sin_addr)
* @param buf is buffer to store the name
* @param bufsize is length of buf
*
* @return 1 if found, 0 if not found, or -1 w/ errno
* @error EAFNOSUPPORT
*/
int ResolveHostsReverse(const struct HostsTxt *ht, int af, const uint8_t *ip,
char *buf, size_t bufsize) {
struct HostsTxtEntry *entry = NULL;
uint32_t v1, v2;
size_t i;
if (af != AF_INET && af != AF_UNSPEC) return eafnosupport();
if (!ht->entries.p || !buf || bufsize == 0) return -1;
v1 = *((uint32_t *)ip);
for (size_t j = 0; j < ht->entries.i; j++) {
v2 = *((uint32_t *)ht->entries.p[j].ip);
if (v1 == v2) {
entry = &(ht->entries.p[j]);
break;
for (i = 0; i < ht->entries.i; ++i) {
if (READ32LE(ip) == READ32LE(ht->entries.p[i].ip)) {
snprintf(buf, bufsize, "%s", ht->strings.p + ht->entries.p[i].name);
return 1;
}
}
if (entry) {
strncpy(buf, &ht->strings.p[entry->name], bufsize);
return 1;
}
return 0;
}

View file

@ -25,17 +25,9 @@
#include "libc/sysv/consts/af.h"
#include "libc/sysv/errfuns.h"
static int hoststxtgetcmp(const char *node, const struct HostsTxtEntry *entry,
const char *strings) {
return CompareDnsNames(node, &strings[entry->name]);
}
/**
* Finds address associated with name in HOSTS.TXT table.
*
* This function performs binary search, so SortHostsTxt() must be
* called on the table beforehand.
*
* @param ht can be GetHostsTxt()
* @param af can be AF_INET, AF_UNSPEC
* @param name can be a local or fully-qualified hostname
@ -49,21 +41,22 @@ static int hoststxtgetcmp(const char *node, const struct HostsTxtEntry *entry,
int ResolveHostsTxt(const struct HostsTxt *ht, int af, const char *name,
struct sockaddr *addr, uint32_t addrsize,
const char **canon) {
size_t i;
struct sockaddr_in *addr4;
struct HostsTxtEntry *entry;
if (af != AF_INET && af != AF_UNSPEC) return eafnosupport();
if ((entry = bsearch_r(name, ht->entries.p, ht->entries.i,
sizeof(struct HostsTxtEntry), (void *)hoststxtgetcmp,
ht->strings.p))) {
if (addr) {
if (addrsize < kMinSockaddr4Size) return einval();
addr4 = (struct sockaddr_in *)addr;
addr4->sin_family = AF_INET;
memcpy(&addr4->sin_addr.s_addr, &entry->ip[0], 4);
for (i = 0; i < ht->entries.i; ++i) {
if (!CompareDnsNames(name, ht->strings.p + ht->entries.p[i].name)) {
if (addr) {
if (addrsize < kMinSockaddr4Size) return einval();
addr4 = (struct sockaddr_in *)addr;
addr4->sin_family = AF_INET;
memcpy(&addr4->sin_addr.s_addr, &ht->entries.p[i].ip[0], 4);
}
if (canon) {
*canon = ht->strings.p + ht->entries.p[i].canon;
}
return 1;
}
if (canon) *canon = &ht->strings.p[entry->canon];
return 1;
} else {
return 0;
}
return 0;
}

View file

@ -1,49 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/alg/alg.h"
#include "libc/dns/dns.h"
#include "libc/dns/hoststxt.h"
/**
* Compares hostnames in HOSTS.TXT table.
* @see CompareDnsNames(), ParseHostsTxt()
*/
static int cmphoststxt(const struct HostsTxtEntry *e1,
const struct HostsTxtEntry *e2, const char *strings) {
if (e1 == e2) return 0;
return CompareDnsNames(&strings[e1->name], &strings[e2->name]);
}
/**
* Sorts entries in HOSTS.TXT table.
*
* This function enables ResolveHostsTxt() to be called so hard-coded
* hostname lookups take logarithmic time; you can blackhole all the
* spam you want, in your /etc/hosts file.
*
* The sorted order, defined by CompareDnsNames(), also makes it
* possible to efficiently search for subdomains, once the initial sort
* is done.
*/
void SortHostsTxt(struct HostsTxt *ht) {
if (ht->entries.p) {
qsort_r(ht->entries.p, ht->entries.i, sizeof(*ht->entries.p),
(void *)cmphoststxt, ht->strings.p);
}
}