trevrpc/dev_certificate

A self-signed ECDSA P-256 certificate for local development that rotates itself, so that a server works without any certificate setup and browsers can trust it through the WebTransport serverCertificateHashes option, which accepts only certificates valid for at most 14 days. Production servers use a certificate from a certificate authority instead.

let assert Ok(certificate) = dev_certificate.start()
let assert Ok(srv) =
  server.start(
    server.Config(
      ..server.development_config(4433, certificate, routes),
      webtransport: True,
    ),
  )
// Native clients trust it as a root, such as through `ca_file`.
let pem = dev_certificate.pem(certificate)

Each certificate is valid for 13 days and presented for 6, and the next one is generated a period ahead. hashes, pem and the JSON a server publishes list both, so a client that fetched them before a rotation still trusts the certificate presented after it.

A server with a development certificate presents it over QUIC and TCP, and answers GET and HEAD requests for its HTTP path followed by /certificate-hashes (/trevrpc/certificate-hashes by default) with the hashes as JSON, over every HTTP version it speaks, for the JavaScript client’s fetchServerCertificateHashes option. See trevrpc_dev_certificate for the details.

Types

A running development certificate, which holds its certificates.

pub type DevelopmentCertificate

Values

pub fn handler(
  certificate: DevelopmentCertificate,
) -> fn(http_request.Request) -> Nil

An HTTP fallback handler that answers GET and HEAD requests with the hashes as JSON, as a server with the certificate answers them itself. Its responses allow any origin to read them. Use it only to publish the hashes elsewhere, such as on the server that serves the web app, and point the client’s serverCertificateHashesUrl option at it.

pub fn hashes(
  certificate: DevelopmentCertificate,
) -> List(BitArray)

The SHA-256 hashes of the current and the next certificate, for serverCertificateHashes.

pub fn pem(certificate: DevelopmentCertificate) -> String

The current and the next certificate PEM-encoded, for native clients to trust as roots. A client that loads them keeps trusting the server across one rotation.

pub fn start() -> Result(DevelopmentCertificate, String)

Starts a development certificate for localhost, 127.0.0.1 and ::1, linked to the calling process: it stops when that process exits, and the servers that present it fail their handshakes once it is gone.

pub fn start_for_hosts(
  hosts: List(String),
) -> Result(DevelopmentCertificate, String)

Starts a development certificate for hosts, which are DNS names or IP addresses: those of start when empty. An error says what is wrong, such as an empty name.

pub fn stop(certificate: DevelopmentCertificate) -> Nil

Stops the certificate. Stopping one that is already stopped does nothing.

✨ Search Document