std/acme
import std/acme to use it. Source: src/std/acme.iyi at v0.22.0.
import std/acme::{AcmeClient, AcmeError, AcmeHttp01}
import std/x509::{SigningKey} account = SigningKey.p256 # the account is this key: keep it
acme = AcmeClient.new("https://acme-v02.api.letsencrypt.org/directory", account).or_panic
acme.register(["mailto:[email protected]"], terms_agreed: true).or_panic
order = acme.new_order(["example.com"]).or_panic
order.authorizations.each do |url|
challenge = acme.authorization(url).or_panic.challenge("http-01").or_panic
responder.add(challenge.token, acme.key_authorization(challenge.token))
acme.respond(challenge).or_panic
acme.wait_authorization(url).or_panic
end
order = acme.finalize(order, SigningKey.p256).or_panic # the CSR, then polled
pem = acme.certificate(order).or_panic # the chain, leaf first**Requests.** AcmeClient.new reads the directory (section 7.1.1). Every request after it is a POST whose body is a JWS in the flattened JSON serialization (RFC 7515 section 7.2.2, RFC 8555 section 6.2): its protected header carries alg, nonce, url and either jwk - for newAccount alone, and keyChange's inner JWS - or kid, the account's URL, for everything else. A read is POST-as-GET (section 6.3), the payload empty. A nonce is used once: each answer's Replay-Nonce is kept for the next request and the one sent is dropped, and newNonce is asked (a HEAD) when none is held. An answer of badNonce is sent again with the nonce that answer carried, MAX_BAD_NONCE times at most (section 6.5); bad_nonce_retries counts them.
**HTTPS only.** Section 6.1: "Use of HTTPS is REQUIRED". Every URL is https:// or refused, by an error naming it, before anything is sent to it: the directory's, each the directory names (termsOfService, a page for people, aside), the account's and the order's from Location, and every URL an object carries, as each is asked. Over plaintext an attacker on the path would be the CA: it would read the account and the requests, and answer a certificate of its own CA for the key.
**Keys** are std/x509's SigningKey: ES256 on P-256 and ES384 on P-384 through std/p256 and std/p384, the signature R || S at the curve's width as RFC 7518 section 3.4 has it and not the DER X.509 carries; RS256, RSA PKCS #1 v1.5 under SHA-256, through std/rsa; and EdDSA (RFC 8037) on Ed25519 through std/curve25519 for a server that takes it - Let's Encrypt's Pebble refuses it (badSignatureAlgorithm), taking RS256, ES256, ES384 and ES512 only. The account's jwk is RFC 7638's canonical form - the required members alone, in order, no space - and its thumbprint the SHA-256 of that, base64url without padding (thumbprint), which every key authorization ends with: token.thumbprint (section 8.1).
**Challenges.** http-01 (section 8.3): AcmeHttp01 holds the key authorizations by token and answers GET /.well-known/acme-challenge/<token> from a std/http Server.serve block (responder.answer(request) || ...). dns-01 (section 8.4): dns_txt(token) is the TXT record's value, base64url of the key authorization's SHA-256, for AcmeClient.dns_name(domain); putting it in the zone is the caller's. tls-alpn-01 (RFC 8737): AcmeTlsAlpn01 writes the self-signed certificate - its one dNSName the domain, and the critical id-pe-acmeIdentifier extension holding the key authorization's SHA-256 - and a std/tls TlsServerConfig that shows it, under a P-256 key made for it, to a client whose ALPN offers acme-tls/1 (and refuses one that offers only other protocols). That config is for a listener of its own on port 443 while the CA validates; a server already on 443 shows the certificate beside its own through its config's identity_source, which sees the client's ALPN, as std/autocert does. A domain past 64 characters has no tls-alpn-01 certificate: std/x509 refuses a self-signed one whose common name is that long (RFC 5280's ub-common-name), so such a name is proved by http-01 or dns-01.
**Waiting.** An authorization or an order is polled until it leaves pending (or processing, or ready), waiting what each answer's Retry-After says - seconds or an HTTP-date (section 8.2 asks it be honoured) - and poll_ms when there is none. Every wait is bounded: at most max_polls polls and wait_ms in all, and a Retry-After that reaches past what is left ends the wait at once, by name; every HTTP exchange is bounded by timeout_ms per read and write, and every answer by MAX_BODY bytes. finalize sends a PKCS #10 request std/x509 writes and signs with the certificate's key (X509.request), its names the order's, and polls the order to valid; certificate reads the chain as PEM, each block parsed by std/x509 before it is answered.
**Errors are values.** An answer of 400 or more is read as a problem document (RFC 7807, section 6.7): an AcmeError whose problem_type is its type URN and detail its detail, with its subproblems (section 6.7.1) each naming the identifier it is about. An authorization or an order that ends invalid is the error its challenge or order carries. Below ACME - the socket, TLS, a body past MAX_BODY - the error has an empty problem_type and says what failed.
Not here: external account binding (section 7.3.4) - a directory that requires it is refused by name at register; renewal - when to order again is the caller's (std/autocert's manager is one such caller), and ACME Renewal Information (RFC 9773) is not read; revocation (section 7.6), account deactivation and updates past register, pre-authorization (newAuthz), IP identifiers (RFC 8738), alternate chains (the Link: rel="alternate" answers), and the profiles extension. Keys are not written out: a SigningKey is made from a PEM or its bytes and keeping it is the caller's.
bench/std_acme_exercise.sh holds it to RFC 7515's, 7638's and 8037's examples, to OpenSSL and Python's cryptography for the request and the tls-alpn-01 certificate, to a server in the exercise that breaks the protocol one rule at a time, and to Let's Encrypt's Pebble for whole orders by all three challenges where Go is there to build it.
AcmeAuthorizationstruct
An authorization (section 7.1.4): the identifier, its status, and the challenges by which it can be proved.
pub struct AcmeAuthorization
# The challenge of *kind* offered, or why there is none.
def challenge(kind : String) : AcmeChallenge | AcmeError
def challenges : Array(AcmeChallenge)
def self.from(url : String, object : Hash(String, Any)) : AcmeAuthorization | AcmeError
def identifier : String
def initialize(url : String, identifier : String, status : String, wildcard : Bool, challenges : Array(AcmeChallenge))
def status : String
def url : String
def wildcard : Bool
endAcmeChallengestruct
A challenge (section 8): its type (http-01, dns-01, tls-alpn-01), where to answer it, its token, and its status, with the problem that made it invalid.
pub struct AcmeChallenge
def error : AcmeError | ::Nil
def self.from(object : Hash(String, Any)) : AcmeChallenge | AcmeError
def initialize(kind : String, url : String, token : String, status : String, error : AcmeError | ::Nil)
def kind : String
def status : String
def token : String
def url : String
endAcmeClientclass
A session with an ACME server under one account key.
pub class AcmeClient
protected def account=(url : String | ::Nil) : String | ::Nil
# The account's URL, the `kid` every request after newAccount carries.
def account_url : String | ::Nil
# The authorization at *url*, read (POST-as-GET).
def authorization(url : String) : AcmeAuthorization | AcmeError
# `badNonce` answers sent again, over this session.
def bad_nonce_retries : Int32
# The certificate chain of a valid *order*, PEM, leaf first (section
# 7.4.2): every block std/x509 reads, or why one does not read.
def certificate(order : AcmeOrder) : String | AcmeError
# How long to wait, in milliseconds, before polling again, as
# *retry_after* - a Retry-After value - says (RFC 9110 section 10.2.3:
# seconds, or an HTTP-date, judged against *now*), or *fallback_ms*
# when there is none or it is neither. A date already past is no wait.
def self.delay(retry_after : String | ::Nil, fallback_ms : Int32, now : Time) : Int64
def directory : AcmeDirectory
# The name dns-01's TXT record is put under for *domain*: a wildcard's
# base domain, with `_acme-challenge.` before it.
def self.dns_name(domain : String) : String
# The dns-01 TXT record's value for *token* (section 8.4).
def dns_txt(token : String) : String
# One HTTP exchange, its answer read whole up to `MAX_BODY`; nothing
# sent to a URL that is not `https://`.
protected def self.exchange(method : String, url : String, body : String, accept : String | ::Nil, timeout_ms : Int32) : AcmeResponse | AcmeError
# The order finalized with a certificate request for its names that
# *key* signs (section 7.4): polled until its authorizations make it
# ready, finalized, and polled until it is valid, with the certificate's
# URL; or the error that made it invalid.
def finalize(order : AcmeOrder, key : SigningKey) : AcmeOrder | AcmeError
# Nil when *url* is an `https://` URL with a host, else the error that
# refuses it as *what*: RFC 8555 section 6.1 requires HTTPS of every
# ACME request, and anything else is plaintext an attacker on the path
# can answer as the CA.
def self.https(url : String, what : String) : AcmeError | ::Nil
def initialize(directory_url : String, key : SigningKey, directory : AcmeDirectory, timeout_ms : Int32)
def key : SigningKey
# *token*'s key authorization (section 8.1): `token.thumbprint`.
def key_authorization(token : String) : String
# The most polls one wait makes, and the most milliseconds it takes.
def max_polls : Int32
# The most polls one wait makes, and the most milliseconds it takes.
def max_polls=(value : Int32) : Int32
# A session with the server whose directory is at *url*, signing with
# *key*: the directory read, or why it could not be. *account_url*, an
# account `register` answered before, is the `kid` from the start, so a
# session that kept it need not register again. Both are `https://`.
def self.new(url : String, key : SigningKey, timeout_ms : Int32 = 30000, account_url : String | ::Nil = nil) : AcmeClient | AcmeError
# A new order for *names*, DNS identifiers (section 7.4).
def new_order(names : Array(String)) : AcmeOrder | AcmeError
# The order at *url*, read (POST-as-GET).
def order(url : String) : AcmeOrder | AcmeError
# How long to wait between polls when an answer has no Retry-After.
def poll_ms : Int32
# How long to wait between polls when an answer has no Retry-After.
def poll_ms=(value : Int32) : Int32
# The account for this key, made if there is none (section 7.3) - with
# *contact*'s URLs (`mailto:`), the terms agreed to when
# *terms_agreed* - or only found when *only_existing*: its URL, which
# every request after this carries as `kid`.
def register(contact : Array(String) = [] of String, terms_agreed : Bool = false, only_existing : Bool = false) : String | AcmeError
# Tells the server *challenge* is ready to be checked (section 7.5.1):
# its URL posted `{}`. Put the answer in place first.
def respond(challenge : AcmeChallenge) : AcmeChallenge | AcmeError
# The account moved to *new_key* (section 7.3.5): keyChange's inner JWS
# signed by the new key, carrying its `jwk`, inside one the old key
# signs as the account.
def roll_key(new_key : SigningKey) : Nil | AcmeError
# The account key's JWK thumbprint (RFC 7638), SHA-256 and base64url.
def thumbprint : String
# Each HTTP exchange's connect, and each read and write in it.
def timeout_ms : Int32
# Each HTTP exchange's connect, and each read and write in it.
def timeout_ms=(value : Int32) : Int32
# The authorization at *url* polled until it is no longer pending: the
# valid authorization, or the error that made it something else.
def wait_authorization(url : String) : AcmeAuthorization | AcmeError
def wait_ms : Int32
def wait_ms=(value : Int32) : Int32
endAcmeDirectorystruct
The directory's URLs (section 7.1.1).
pub struct AcmeDirectory
def external_account_required : Bool
def initialize(new_nonce : String, new_account : String, new_order : String, key_change : String | ::Nil, revoke_cert : String | ::Nil, terms_of_service : String | ::Nil, external_account_required : Bool)
def key_change : String | ::Nil
def new_account : String
def new_nonce : String
def new_order : String
def self.parse(text : String) : AcmeDirectory | AcmeError
def revoke_cert : String | ::Nil
def terms_of_service : String | ::Nil
endAcmeErrorstruct
What a request answered that it should not have, or could not do: a problem document's type and detail (RFC 7807), or, with problem_type empty, what failed beneath ACME.
pub struct AcmeError
def detail : String
# The identifier a subproblem is about (section 6.7.1), "" otherwise.
def identifier : String
def initialize(detail : String, problem_type : String = "", status : Int32 = 0, identifier : String = "", subproblems : Array(AcmeError) = [] of AcmeError)
# Whether this came from a problem document.
def problem? : Bool
def problem_type : String
# The HTTP status the problem came with; 0 when there was no answer.
def status : Int32
def subproblems : Array(AcmeError)
def to_s : String
endAcmeHttp01class
http-01's answers (section 8.3), by token, for a std/http server: GET /.well-known/acme-challenge/<token> is answered with the token's key authorization, a token not held with 404, any other path with nil - the server's own to answer.
responder = AcmeHttp01.new
Server.serve(listener, workers: 1) { |request| responder.answer(request) || app(request) }The table of answers is written as orders run, so it is one thread's: the server that answers from it serves on that thread (workers: 1).
pub class AcmeHttp01
def add(token : String, key_authorization : String) : Nil
def answer(request : Request) : Response | ::Nil
def initialize
def remove(token : String) : Nil
endAcmeJwsstruct
JWS (RFC 7515) as ACME signs it: the flattened JSON serialization, the account's key as RFC 7638's canonical JWK, and the signature in the form JWA (RFC 7518) gives each algorithm.
pub struct AcmeJws
# The JWS algorithm *key* signs under: ES256, ES384, RS256 or EdDSA.
def self.alg(key : SigningKey) : String
# *data* in base64url without padding (RFC 7515 section 2).
def self.b64u(data : String | Array(UInt8)) : String
def self.bytes(text : String) : Array(UInt8)
# The protected header: `alg`, then `jwk` when *kid* is nil and `kid`
# when it is not, `nonce` when there is one, and `url`.
def self.header(key : SigningKey, nonce : String | ::Nil, url : String, kid : String | ::Nil) : String
def initialize
# *key* as a JWK holding its required members alone, sorted, without
# whitespace (RFC 7638 section 3): the text its thumbprint is taken of,
# and the `jwk` a request carries.
def self.jwk(key : RsaPublicKey | EcdsaPublicKey | Ed25519PublicKey) : String
# A big-endian number without its leading zero bytes, as JWA writes
# one (RFC 7518 section 6.3.1).
def self.minimal(bytes : Array(UInt8)) : Array(UInt8)
# An Ecdsa-Sig-Value's DER as R || S, each *size* bytes.
def self.raw(der : Array(UInt8), size : Int32) : Array(UInt8)
# The flattened JSON serialization (RFC 7515 section 7.2.2) of *payload*
# - nil for the empty payload of POST-as-GET - under *header*.
def self.sign(key : SigningKey, header : String, payload : String | ::Nil) : String | AcmeError
# *key*'s JWS signature of *input*: R || S for ECDSA, each at the
# curve's width (RFC 7518 section 3.4), where std/p256 and std/p384 sign
# in X.509's DER; RSA's and Ed25519's as they are.
def self.signature(key : SigningKey, input : Array(UInt8)) : Array(UInt8) | AcmeError
# *key*'s JWK thumbprint (RFC 7638): base64url of the SHA-256 of `jwk`.
def self.thumbprint(key : RsaPublicKey | EcdsaPublicKey | Ed25519PublicKey) : String
endAcmeOrderstruct
An order (section 7.1.3): its URL, status, names, the authorizations to prove, where to finalize it, and, once it is valid, its certificate.
pub struct AcmeOrder
def authorizations : Array(String)
def certificate_url : String | ::Nil
def error : AcmeError | ::Nil
def finalize_url : String
def self.from(url : String, object : Hash(String, Any)) : AcmeOrder | AcmeError
def identifiers : Array(String)
def initialize(url : String, status : String, identifiers : Array(String), authorizations : Array(String), finalize_url : String, certificate_url : String | ::Nil, error : AcmeError | ::Nil)
def status : String
def url : String
endAcmeTlsAlpn01struct
tls-alpn-01 (RFC 8737): the certificate a TLS server shows to a client whose ALPN offers acme-tls/1, and a std/tls config that shows it.
pub struct AcmeTlsAlpn01
# The self-signed certificate for *domain* (section 3): its one dNSName
# *domain*, and the critical acmeIdentifier extension, an OCTET STRING
# of the SHA-256 of *key_authorization*; signed by *key*, valid from a
# day before *now* to a week after. A *domain* past 64 characters is
# refused, as std/x509 refuses its common name.
def self.certificate(domain : String, key_authorization : String, key : SigningKey, now : Time = Time.utc) : Array(UInt8) | AcmeError
# A std/tls server config that shows `certificate` for *domain*, under a
# P-256 key made for it, and speaks `acme-tls/1` alone by ALPN.
def self.config(domain : String, key_authorization : String) : TlsServerConfig | AcmeError
def initialize
endError for AcmeErrorimpl
impl Error for AcmeError
def message : String
end
An ACME client (RFC 8555), written in iyi: the protocol Let's Encrypt and the other automated CAs speak, so that a server here gets its own certificate.