The pieces / weirdnet-tls

weirdnet-tls

TLS certificates: ACME issuance and renewal, certificate stores and an SNI resolver for rustls.

v0.3.0 GitHub crates.io docs.rs weirdnet-tls = "0.3.0"

What it brings

Certificates for any Weirdnet host without each app writing its own ACME client. It replaces the ACME code that was separate in Mouser, Muninn, itcmdr and TAUR, and it never trades availability for a certificate: a failed order keeps the last certificate in service and never falls back to plain HTTP.

Use it when an app terminates TLS itself with rustls: a relay on 443, a self-hosted site behind Weirdnet forwarding, a service like TAUR. HTTP servers and port forwarding stay in the app. Muninn orders its certificates through it.

What it is

Certificates from Let’s Encrypt or any ACME directory over instant-acme, kept current in the background and served through a rustls resolver that swaps them without a restart. It is the shared replacement for the ACME code in Mouser, Muninn, itcmdr and TAUR.

A failed order never drops TLS: the last certificate stays in service, and the crate never falls back to plain HTTP. HTTP servers and port forwarding are left to the app.

How it works

  1. Order: Acme keeps an account per directory in the store (Let's Encrypt production or staging, or any RFC 8555 directory), with several names per certificate, wildcards and ACME profiles. From 0.2.0, AcmeConfig::profile_optional orders with the CA's default when the profile is refused. From 0.3.0, names a public CA refuses (localhost, IP addresses, all-digit top labels) fail validation unless AcmeConfig::private_names is set for a private CA.
  2. Challenge: TLS-ALPN-01 is answered by the resolver on your port 443; HTTP-01 from an Http01Board (served by serve_http01 or your router); DNS-01 through the DnsChallenge trait: a hook command (it gets WEIRDNET_TLS_ACTION, WEIRDNET_TLS_DOMAIN, WEIRDNET_TLS_NAME and WEIRDNET_TLS_TXT, and from 0.3.0 the same under an app's own prefix with HookDns::with_prefix), a manual prompt, or a grindvakt-core provider. While the app's own listener isn't running, Acme::serve_challenge answers TLS-ALPN-01 or HTTP-01 on a listener of its own.
  3. Store: the CertStore trait with FileStore (files 0600 in directories 0700, atomic writes), MemoryStore, or SqlStore in an encrypted weirdnet-storage database. Acme::import copies an app's earlier account, certificate and state in, never overwriting.
  4. Serve: the Resolver picks a certificate per name, in the order pinned (internal names, a private CA), the user's own, ACME, then self-signed, skipping expired ones, with a default for handshakes without SNI (the ACME certificate with Acme::with_default_cert). It swaps certificates without a restart; server_config hands a rustls config to tokio-rustls or hyper.
  5. Renew: at a point in the CA's ACME Renewal Information window (RFC 9773) with replaces, else two thirds into the lifetime, so shorter certificate lifetimes need no change. Failures back off from an hour to a day; a forced renewal within a day of the last issue is refused (Acme::trigger_renewal asks the running renewer for one). Acme::watch gives a typed Status, and acme::schedule reads the next renewal from the store alone, for status commands.

Linux, macOS and Windows are tested in CI; Android (aarch64, API 26) and iOS (aarch64) build.

Key types and modules

  • ACME: an account per directory, several names per certificate, wildcards, ACME profiles, replaces on renewal.
  • Challenges: TLS-ALPN-01 answered by the resolver, HTTP-01 from a token board, DNS-01 by a hook command, a manual prompt or a grindvakt-core provider.
  • Stores: files (0600), memory, or a table in an encrypted weirdnet-storage database.
  • Resolver: per-name certificates in the order pinned, user, ACME, self-signed, with a default for handshakes without SNI.
  • Renewal: inside the CA’s renewal window (ARI), else two thirds into the lifetime; backoff from an hour to a day; a typed status.
  • Testing: a mock ACME CA that checks the real challenge answers.

Feature flags

  • ring (default): rustls, rcgen and instant-acme on ring.
  • aws-lc-rs: the same on aws-lc-rs; wins when both are on.
  • grindvakt: DNS-01 through grindvakt-core providers. They can’t delete records, so the _acme-challenge TXT is overwritten on each order and left in place.
  • storage: SqlStore in a weirdnet-storage (SQLCipher) database.
  • testing: testing::MockCa, an in-process ACME CA that checks the real HTTP-01, TLS-ALPN-01 and DNS-01 answers and serves ARI.

Usage

In Cargo.toml:

[dependencies]
weirdnet-tls = "0.3.0"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

Then (built and run against the published crate when this page was made):

// Serve TLS now (self-signed), then let ACME replace the certificate in the background.
use std::sync::Arc;
use weirdnet_tls::*;

#[tokio::main]
async fn main() -> weirdnet_tls::Result<()> {
    let names = vec!["example.com".to_string(), "www.example.com".to_string()];
    let resolver = Arc::new(Resolver::with_self_signed(&names)?);
    let mut cfg = AcmeConfig::new("main", names);
    cfg.contact = vec!["admin@example.com".into()];
    let acme = Arc::new(Acme::new(cfg, Arc::new(FileStore::new("/var/lib/myapp/tls")), resolver.clone())?);
    acme.load()?;
    let _renewer = acme.clone().spawn();
    let _tls = resolver.server_config(&[b"h2", b"http/1.1"])?; // hand to tokio-rustls, hyper, ...
    Ok(())
}

Where it fits

Version 0.3.0 is on crates.io (docs.rs). It adds app prefixes and extra variables for DNS hooks (HookDns::with_prefix, with_env), an offline renewal schedule read from the store (acme::schedule), the ACME certificate as the default for handshakes without SNI (Acme::with_default_cert), forced renewals from the running renewer behind the repeat-certificate guard (Acme::trigger_renewal), challenge-only TLS-ALPN-01 and HTTP-01 listeners (Acme::serve_challenge), and importing an app’s earlier account, certificate and state (Acme::import). 0.3.0 is breaking: valid_name and AcmeConfig::validate refuse localhost, IP addresses and all-digit top labels unless AcmeConfig::private_names is set for a private CA, AcmeConfig is #[non_exhaustive] (build it with AcmeConfig::new), and HookDns is built with HookDns::new.