acme - ACME / Let's Encrypt TLS certificates
Import with import "acme.j" as acme;. An ACME (RFC 8555) client: obtain and renew TLS certificates from Let's Encrypt and compatible CAs. It drives the whole flow - account registration, a new order, an HTTP-01 or DNS-01 challenge, CSR finalize, and certificate download - over http + json, with every request a JWS signed by the account key (RS256 for an RSA key; ES256 / ES384 / ES512 for an EC key, by curve). Keys, the CSR, and the JWK thumbprint come from crypto, so acme needs the default jennifer binary.
Use a staging endpoint first. Let's Encrypt's staging CA (
https://acme-staging-v02.api.letsencrypt.org/directory) issues untrusted certificates with far higher rate limits - develop against it, then switch to production.
Runnable: examples/modules/acme_demo.j.
The flow
Proving you control a domain is your job, so the steps are separate calls rather than one obtain(): register an account, create an order, and for each domain fetch its authorization, pick a challenge, publish the response, accept, and poll until valid; then finalize with a CSR and download the cert.
use crypto;
import "acme.j" as acme;
def const DIRECTORY_URL as string init "https://acme-staging-v02.api.letsencrypt.org/directory";
def key as bytes init crypto.keypair("p256").private; # account key
def client as acme.Client init acme.connect(DIRECTORY_URL, $key);
$client = acme.register($client, "you@example.com");
def order as acme.Order init acme.order($client, ["example.com"]);
def authz as acme.Authorization init acme.authorization($client, $order.authorizations[0]);
def ch as acme.Challenge init acme.challenge("http-01", $authz);
# --- publish the challenge response (see below), then: ---
acme.accept($client, $ch.url);
acme.pollAuthorization($client, $order.authorizations[0], 2000, 30); # throws unless valid
def certKey as bytes init crypto.keypair("p256").private; # certificate key
def csr as bytes init crypto.csr($certKey, ["example.com"]);
def issued as acme.Order init acme.finalize($client, $order, $csr, 2000, 30);
def pem as string init acme.downloadCertificate($client, $issued); # the cert chainPublishing the challenge response
For a challenge ch, compute the response and publish it, then accept:
- HTTP-01: serve
acme.keyAuthorization($client, $ch.token)as the body athttp://<domain>/.well-known/acme-challenge/<ch.token>(pair withweb/httpd). - DNS-01: publish
acme.dnsRecord($client, $ch.token)as a TXT record at_acme-challenge.<domain>(via your DNS provider). DNS-01 is the only way to get a wildcard (*.example.com) certificate.
Both are pure (no network) - compute them, provision the response your own way, then tell the CA it is ready with accept.
Surface
| Call | Returns | Notes |
|---|---|---|
acme.connect(directoryUrl, accountKey) | Client | Fetch the CA directory; build a client around the account key (PEM bytes). |
acme.connectWith(directoryUrl, accountKey, options) | Client | connect with Options{allowInsecureHttp} (plain http:// to a test CA). |
acme.register(client, email) | Client | Register the account (agrees to the ToS); returns a client with its kid. |
acme.order(client, domains) | Order | Create an order for a list of string of domains. |
acme.authorization(client, authzUrl) | Authorization | Fetch an authorization and its challenges. |
acme.challenge(kind, authz) | Challenge | The "http-01" / "dns-01" challenge within an authorization. |
acme.keyAuthorization(client, token) | string | The HTTP-01 response body (token.thumbprint). Pure. |
acme.dnsRecord(client, token) | string | The DNS-01 TXT value. Pure. |
acme.accept(client, challengeUrl) | null | Tell the CA the response is published. |
acme.pollAuthorization(client, authzUrl, intervalMs, maxTries) | Authorization | Poll until the authorization settles; returns it when valid, throws when it settles otherwise. |
acme.finalize(client, order, csrDer, intervalMs, maxTries) | Order | Submit the CSR and poll until the certificate is issued. |
acme.downloadCertificate(client, order) | string | The issued PEM certificate chain. |
acme.fetchOrder(client, orderUrl) | Order | Re-fetch an order's state. |
The structs (Client, Order, Authorization, Challenge) are value-semantic. Client holds the account key and the CA endpoints. Every signed request draws a fresh anti-replay nonce from the CA before it is sent; a badNonce rejection is retried once.
acme.Client { directory, newNonce, newAccount, newOrder as string, accountKey as bytes,
alg, kid as string, allowInsecureHttp as bool }
acme.Options { allowInsecureHttp as bool }
acme.Order { url, status as string, authorizations as list of string,
finalize, certificate as string }
acme.Authorization { domain, status as string, challenges as list of acme.Challenge }
acme.Challenge { kind, url, token, status as string }An Order's status moves pending -> ready -> processing -> valid, its authorizations are the per-domain authorization URLs, finalize takes the CSR, and certificate is the download URL once issued. An Authorization carries the domain, its status, and the offered challenges; each Challenge carries its kind (http-01 / dns-01 / tls-alpn-01), the url to accept, the token (base64url, checked by authorization, so it is safe as a file name), and its status.
pollAuthorization returns only a valid authorization. One that settles otherwise (invalid, deactivated, expired, revoked) throws an AcmeError whose problem, problemType and detail are the failed challenge's error document - why the CA could not validate the domain (urn:ietf:params:acme:error:connection, ...:dns, ...:incorrectResponse).
HTTPS only
RFC 8555 (section 6.1) carries every ACME request over HTTPS. A directory URL, or an endpoint the directory or an order names, with http:// to a non-loopback host throws an AcmeError before anything is sent. http:// to localhost, 127.0.0.0/8 or [::1] is allowed (a CA in a test harness). A test CA elsewhere on a trusted network needs the explicit opt-in:
use crypto;
import "acme.j" as acme;
def key as bytes init crypto.keypair("p256").private;
def client as acme.Client init acme.connectWith(
"http://ca.lab.internal:4001/directory", $key,
acme.Options{allowInsecureHttp: true});Never set it for a real CA: anyone on the path could then rewrite the directory and the orders.
Keys and algorithms
- Account key: the
privatePEM ofcrypto.keypair("p256")(→ES256) orcrypto.keypair("rsa2048")(→RS256).connectderives the JWS algorithm from the key and signs throughcrypto.sign("ecdsa-sha256","rsa-pkcs1-sha256", ...), binding each EC curve to its JOSE algorithm - P-256 →ES256, P-384 →ES384, P-521 →ES512(a curve must not be signed under another curve's algorithm).ES256(the P-256 default) is the most widely accepted; check your CA supports a curve before using P-384 / P-521. Reuse the same account key across renewals: keep it in a file only its owner can read (chmod 600) and load it withfs.readSecretBytes, which refuses a looser one (seefs). - Certificate key: a separate key, only used for the CSR (
crypto.csr(certKey, domains)). Generate a fresh one, or reuse per domain; a stored one is read withfs.readSecretBytestoo.
A CA's refusal or a malformed answer (not JSON, a required member missing or of the wrong type, a status RFC 8555 does not define) is an acme.AcmeError (kind "acme"); a socket failure, a timeout or a reply over http's cap goes on as http threw it (kind "net", "timeout", "limit", ...). An error from the CA carries its RFC 7807 problem document: problemType (urn:ietf:params:acme:error:rateLimited, ...:badCSR, ...:unauthorized), detail, the HTTP status, the whole problem (with subproblems), and retryAfter, the Retry-After seconds of a rate limit (0 when absent or given as a date).
use io;
use strings;
try {
$order = acme.order($client, ["example.com"]);
} catch (e) {
if ($e.kind == "acme" and strings.endsWith($e.problemType, ":rateLimited")) {
io.printf("rate limited; retry in %d s\n", $e.retryAfter);
} else {
throw $e;
}
}Renewal
Renewing is running the flow again before expiry (Let's Encrypt certs last 90 days; renew at ~60). Keep the account key; a new order re-validates the domains and issues a fresh certificate.
Platform
acme needs net (via http) and the crypto RSA / ECDSA / CSR / JWK surface, so it runs on the default jennifer binary only; on jennifer-tiny the crypto and network calls throw.
Academy talks
| Talk | What it shows |
|---|---|
| Crypto, tokens and certificates with Jennifer | account, order, http-01 challenge and certificate issuance |
API reference
Every exported function, struct and constant of acme, with its signature, generated from the module's doc comments: the acme API reference.