JFLib

netio/acme.rs#

Source: lib/netio/acme.rs

acme -- certificates from a CA that speaks ACME (RFC 8555): Let's Encrypt, ZeroSSL, Google
Trust Services, a company's step-ca.  http-01, tls-alpn-01 (RFC 8737) and dns-01 challenges,
DNS names and IP addresses (RFC 8738), renewal information (ARI, RFC 9773), external
account binding (RFC 8555 7.3.4), profiles, key change, revocation

  Acme::new(client, directory_url) -> Result<Acme, AcmeError>     reads the CA's directory
  acme.account(key, &NewAccount { contact, terms_agreed, eab, .. }) -> Account
  account.issue(&ids, &cert_key, &mut solver, &OrderOptions) -> Issued { chain, pem, .. }
      the whole issuance: an order, each authorization proved by `solver`, a certificate
      request for `cert_key`, the chain once the CA has issued it
  step by step: new_order, authorization, respond, wait_authorization, finalize,
      wait_order, certificate
  acme.renewal_info(&cert) (ARI: when to renew), account.revoke, change_key, update_contact,
      deactivate; revoke_with_key (by the certificate's own key)
  Solver: what proves control of a name -- Http01 (a netio Server's handler answers
      /.well-known/acme-challenge/), TlsAlpn01 (a tls::ServerConfig that answers
      "acme-tls/1"), a DNS provider's code of the program's own (dns01_name, dns01_value)

Every request is a JWS (RFC 7515) signed with the account key: ES256 (P-256), ES384 (P-384),
RS256 (RSA) or EdDSA (Ed25519; not every CA takes it).  The CA's URLs must be https; its
answers are bounded (64 KiB, a certificate chain 1 MiB) and checked: the chain issued must be
for the certificate key and every identifier ordered.  A wait for an authorization or the
order follows Retry-After and ends after Acme::timeout_ms.
/// Let's Encrypt's directory, and its staging one (for tests: higher rate limits, an untrusted
/// root)
LETS_ENCRYPT: &str = "https://acme-v02.api.letsencrypt.org/directory"
LETS_ENCRYPT_STAGING: &str = "https://acme-staging-v02.api.letsencrypt.org/directory"
/// the ALPN protocol of tls-alpn-01 (RFC 8737 6.2)
ACME_TLS_ALPN: &str = "acme-tls/1"
/// the longest answer of the CA (JSON), and of a certificate chain
MAX_ANSWER: usize = 64 * 1024
MAX_CHAIN: usize = 1 << 20
  • struct Problem a problem document of the CA (RFC 8555 6.7, RFC 7807)
    • status: u16 -- the HTTP status of the answer (0 inside an object: an authorization's error)
    • kind: String -- "urn:ietf:params:acme:error:rateLimited", ...
    • detail: String
    • identifier: Option<Identifier> -- the identifier a subproblem is about (RFC 8555 6.7.1)
    • subproblems: Vec<Problem>
    • retry_after: Option<u64> -- seconds to wait before trying again (Retry-After), when the CA says

impl Problem

  • fn is(&self, name: &str) -> bool whether it is the ACME error name ("badNonce", "rateLimited", "unauthorized", ...)

  • enum AcmeError

    • Http(HttpError) -- the request did not reach the CA, or no HTTP answer came
    • Problem(Problem) -- the CA refused (its problem document)
    • Protocol(String) -- an answer of the CA that does not follow RFC 8555
    • Invalid(String, Option<Problem>) -- an authorization or the order ended invalid (the CA's reason, when it gave one)
    • Solver(String) -- the solver could not set a challenge up
    • Timeout(String) -- the CA did not decide within Acme::timeout_ms
  • enum Identifier what a certificate is for

    • Dns(String) -- a DNS name, lower case; "*.example.com" for a wildcard (dns-01 only)
    • Ip(IpAddr) -- an IP address (RFC 8738)

impl Identifier

  • fn parse(s: &str) -> Identifier a name or an address ("example.com", "*.example.com", "192.0.2.1", "2001:db8::1")

  • fn value(&self) -> String the text of the identifier (an address in its RFC 5952 form)

  • fn tls_name(&self) -> String the name a validator puts in server_name for tls-alpn-01: the name, or for an address its reverse DNS name (RFC 8738 6)

  • struct Directory the CA's directory (RFC 8555 7.1.1)

    • new_nonce: String
    • new_account: String
    • new_order: String
    • revoke_cert: String
    • key_change: String
    • new_authz: Option<String> -- pre-authorization (most CAs have none)
    • renewal_info: Option<String> -- renewal information (RFC 9773)
    • terms_of_service: Option<String> -- what an account must agree to (NewAccount::terms_agreed)
    • website: Option<String>
    • caa_identities: Vec<String> -- the names a CAA record must name for this CA
    • external_account_required: bool -- accounts need an external account binding (NewAccount::eab)
    • profiles: Vec<(String, String)> -- the certificate profiles offered: (name, description)
  • fn rfc3339(s: &str) -> Option<i64> an RFC 3339 time ("2026-11-24T08:00:00Z", fractions, an offset) -> Unix seconds

  • fn rfc3339_text(t: i64) -> String Unix seconds -> "2026-11-24T08:00:00Z"

  • fn thumbprint(key: &PubKey) -> Option<String> the JWK thumbprint (RFC 7638, SHA-256), base64url

  • struct Eab an external account binding (RFC 8555 7.3.4): the key id and MAC key the CA handed out

    • kid: String
    • hmac_key: Vec<u8>

impl Eab

  • fn new(kid: &str, hmac_key_b64: &str) -> Option<Eab> the key id and the MAC key as the CA shows it (base64url)

  • struct NewAccount a new (or existing) account

    • contact: Vec<String> -- "mailto:admin@example.com" (some CAs need one; Let's Encrypt no longer writes to them)
    • terms_agreed: bool -- the directory's terms of service agreed to
    • only_existing: bool -- only find the account of the key, make none (RFC 8555 7.3.1)
    • eab: Option<Eab>
  • struct OrderOptions an order's options

    • not_before: Option<i64> -- the validity asked for (Unix seconds; most CAs decide it themselves)
    • not_after: Option<i64>
    • profile: Option<String> -- a profile of the directory's (Directory::profiles)
    • replaces: Option<String> -- the certificate this one replaces (its ARI id, ari_id): the CA can tell a renewal
    • must_staple: bool -- a certificate that must come with a stapled OCSP response (RFC 7633; not every CA issues them)
  • struct Order

    • url: String
    • status: String -- "pending", "ready", "processing", "valid", "invalid"
    • expires: Option<i64>
    • identifiers: Vec<Identifier>
    • authorizations: Vec<String>
    • finalize: String
    • certificate: Option<String>
    • error: Option<Problem>
    • profile: Option<String>
  • struct Challenge

    • kind: String -- "http-01", "tls-alpn-01", "dns-01", ...
    • url: String
    • status: String -- "pending", "processing", "valid", "invalid"
    • token: String -- base64url (checked: only its alphabet, 16 to 256 characters)
    • error: Option<Problem>
  • struct Authorization

    • url: String
    • status: String -- "pending", "valid", "invalid", "deactivated", "expired", "revoked"
    • identifier: Identifier
    • wildcard: bool -- for "*." plus the identifier (dns-01 only)
    • expires: Option<i64>
    • challenges: Vec<Challenge>
  • struct Issued a certificate issued

    • chain: Vec<Vec<u8>> -- DER, leaf first
    • pem: String -- the chain as the CA sent it (PEM): for the server's certificate file
    • alternates: Vec<String> -- other chains the CA offers (Link rel="alternate"; Acme::certificate fetches them)
    • order: Order

impl Issued

  • fn identity(&self, key: &PrivateKey) -> Identity the identity for a TLS server: the chain and the certificate key

  • struct RenewalInfo when to renew a certificate (ARI, RFC 9773)

    • start: i64 -- the window the CA suggests (Unix seconds): renew at a random time in it
    • end: i64
    • explanation: Option<String>
    • retry_after: u64 -- when to ask again (seconds, Retry-After; 6 hours when the CA does not say)

impl RenewalInfo

  • fn renew_at(&self, fraction: f64) -> i64 a time in the window to renew at: start + (end - start) * fraction (a random fraction in [0, 1) spreads the renewals out, as RFC 9773 4.2 asks)

  • fn ari_id(cert: &Cert) -> Option<String> a certificate's id in renewal information and in an order's "replaces" (RFC 9773 4.1): the key identifier of its authority key identifier and its serial number; None without the former

  • struct Acme the client of one CA (cheap to clone: the clones share the nonces)

    • directory: Arc<Directory>
    • timeout_ms: u64 -- the longest wait for an authorization or the order to be decided (ms; 3 minutes)
    • poll_ms: u32 -- the pause between two looks at an authorization or the order when the CA sends no Retry-After (ms; 2 seconds)
    • max_poll_ms: u32 -- the longest pause taken from a Retry-After (ms; 30 s); a shorter Retry-After counts as at least half a second (or max_poll_ms, when that is less). No pause of either kind goes past timeout_ms: the last one ends at it

impl Acme

  • fn new(client: Client, directory: &str) -> Result<Acme, AcmeError> the CA at directory (an https URL: LETS_ENCRYPT, ...) over client (with an https connector: trust::system_connector for public CAs)

  • fn account(&self, key: PrivateKey, new: &NewAccount) -> Result<Account, AcmeError> the account of key (made when there is none and new.only_existing is false)

  • fn renewal_info(&self, cert: &Cert) -> Result<RenewalInfo, AcmeError> when the CA suggests renewing cert (ARI, RFC 9773); Err when the directory has no renewalInfo or the certificate no authority key identifier

  • struct Account an account of a CA

    • url: String -- the account's URL (its key id, "kid")
    • status: String -- "valid", "deactivated", "revoked"
    • contact: Vec<String>
    • created: bool -- the CA made it on this call (201)

impl Account

  • fn acme(&self) -> &Acme

  • fn key(&self) -> &PrivateKey

  • fn key_authorization(&self, token: &str) -> String what a challenge with this token must show (RFC 8555 8.1): token "." the thumbprint of the account key

  • fn new_order(&self, ids: &[Identifier], opts: &OrderOptions) -> Result<Order, AcmeError> a new order for these identifiers

  • fn order(&self, url: &str) -> Result<Order, AcmeError> an order as it stands now

  • fn authorization(&self, url: &str) -> Result<Authorization, AcmeError>

  • fn respond(&self, ch: &Challenge) -> Result<(), AcmeError> tell the CA a challenge is ready to be checked (RFC 8555 7.5.1)

  • fn wait_authorization(&self, url: &str) -> Result<Authorization, AcmeError> wait until the authorization is decided: Ok when valid. Acme::timeout_ms bounds the waiting: no look starts after it (0: none); a request under way runs out under the Client's timeouts, and what it brings counts (a valid authorization is the CA's decision, however late it comes)

  • fn wait_order(&self, url: &str) -> Result<Order, AcmeError> wait until the order is no longer pending or processing (Acme::timeout_ms as wait_authorization)

  • fn finalize(&self, order: &Order, csr: &[u8]) -> Result<Order, AcmeError> send the certificate request (DER) of a ready order

  • fn certificate(&self, url: &str) -> Result<(Vec<Vec<u8>>, String, Vec<String>), AcmeError> the chain at a certificate URL (an order's, or an alternate one) -> (DER chain, PEM, alternates)

  • fn issue(&self, ids: &[Identifier], cert_key: &PrivateKey, solver: &mut dyn Solver, opts: &OrderOptions) -> Result<Issued, AcmeError> the whole issuance for ids and the key cert_key (not the account key): an order; each pending authorization proved with the first challenge type of solver.kinds() it offers (a wildcard only by dns-01), set up, answered, waited for and taken down; the certificate request; the chain, checked to be for cert_key and every identifier

  • fn revoke(&self, cert_der: &[u8], reason: Option<u8>) -> Result<(), AcmeError> revoke a certificate of this account (RFC 8555 7.6); reason: a CRLReason (RFC 5280 5.3.1: 1 keyCompromise, 4 superseded, 5 cessationOfOperation, ...)

  • fn change_key(&mut self, new_key: PrivateKey) -> Result<(), AcmeError> change the account's key (RFC 8555 7.3.5)

  • fn update_contact(&mut self, contact: &[String]) -> Result<(), AcmeError> replace the account's contact addresses

  • fn deactivate(&mut self) -> Result<(), AcmeError> end the account (RFC 8555 7.3.6): it cannot be used again

  • fn revoke_with_key(acme: &Acme, cert_der: &[u8], cert_key: &PrivateKey, reason: Option<u8>) -> Result<(), AcmeError> revoke a certificate by its own key (signed with it, no account: a lost account key, a compromised certificate key)

  • trait Solver what proves control of an identifier

    • fn kinds(&self) -> Vec<&'static str> the challenge types it sets up, the preferred first ("http-01", "tls-alpn-01", "dns-01")
    • fn present(&mut self, id: &Identifier, ch: &Challenge, key_auth: &str) -> Result<(), String> set the challenge up for id ("*.example.com" for a wildcard): key_auth is what it must show (dns-01: dns01_value(key_auth) as TXT of dns01_name(id))
    • fn cleanup(&mut self, id: &Identifier, ch: &Challenge) take it down once the authorization is decided
  • fn dns01_name(id: &Identifier) -> String the TXT record's name of dns-01 (RFC 8555 8.4): "_acme-challenge." and the name (a wildcard's without "*.")

  • fn dns01_value(key_auth: &str) -> String the TXT record's value of dns-01: base64url of SHA-256 of the key authorization

  • struct Http01 http-01 (RFC 8555 8.3): the key authorizations a netio HTTP server answers at /.well-known/acme-challenge/<token> on port 80. Clones share the tokens: one goes to the solver, one into the server (handler or answer)

impl Http01

  • fn new() -> Http01

  • fn answer(&self, req: &Request) -> Option<Response> the answer to a challenge request, None for every other request

  • fn handler(&self, next: Handler) -> Handler a handler that answers challenges and hands every other request to next

  • struct TlsAlpn01 tls-alpn-01 (RFC 8737): a certificate for the challenge, shown on port 443 to a client that offers only "acme-tls/1". Clones share the certificates: one goes to the solver, one into the server's config (setup). A name has one challenge at a time: another one for it fails to be set up until the first is cleaned up; the same challenge set up twice (two orders the CA gave it to) keeps its first certificate and stays until both are cleaned up

impl TlsAlpn01

  • fn new() -> TlsAlpn01

  • fn setup(&self, cfg: &mut ServerConfig) let cfg answer the challenges: "acme-tls/1" among its protocols (last; to a client that does not offer it alone the server's list is as if it were not there, so a server without protocols of its own still answers without ALPN), and a select that shows the challenge certificate for a ClientHello offering only it (before the config's own select)

  • fn certificate(id: &Identifier, key_auth: &str) -> Option<Identity> the self-signed certificate of a challenge (RFC 8737 3): the identifier as its only name, the critical acmeIdentifier with SHA-256 of the key authorization

  • struct Solvers(pub Vec<Box<dyn Solver + Send>>) several solvers, each for the challenge types it sets up (a DNS provider for wildcards, http-01 for the rest): the first that sets up a type the authorization offers takes it

Source: docs/API.md in the JFLib repository.