test(link): the ACME client against an in-process authority (P15 B3)

The real instant-acme client speaks RFC 8555 to a small authority written in the
test: account, order, DNS-01 proof read from the relay's own DNS, finalize with
the client's CSR, download, an account binding when the relay gave one and none
when it did not, a renewal that names the certificate it replaces, the
authority's renewal window, and a refusal in the authority's own words.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-21 12:59:46 -07:00
1 parent 9898e56950
commit 0f9420aa30
2 files changed
+524 -1

No files matched your search

+2 -1
View File
@@ -36,13 +36,14 @@ ledger-store = { path = "../ledger-store" }
[dev-dependencies]
tempfile = "3"
rcgen = { version = "0.14", default-features = false, features = ["ring", "pem"] }
rcgen = { version = "0.14", default-features = false, features = ["ring", "pem", "x509-parser"] }
hickory-proto = { version = "0.26", default-features = false, features = ["std"] }
tokio = { version = "1", features = ["test-util"] }
http = "1"
http-body-util = "0.1"
bytes = "1"
async-trait = "0.1"
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }
sha2 = "0.10"
time = "0.3"
tokio-rustls = { version = "0.26", default-features = false, features = ["ring", "tls12"] }
+522
View File
@@ -0,0 +1,522 @@
//! The ACME client against an in-process authority: the real
//! `instant-acme` client speaks RFC 8555 to a small authority written
//! here (it checks the proof in the relay's own DNS, signs the CSR it is
//! given with a CA of its own, and offers renewal information). No
//! network beyond 127.0.0.1: the "HTTP" is a function call.
#![allow(missing_docs)]
mod support;
use base64::Engine as _;
use base64::engine::general_purpose::URL_SAFE_NO_PAD as B64;
use bytes::Bytes;
use futures::future::BoxFuture;
use http_body_util::BodyExt;
use instant_acme::{BodyWrapper, BytesResponse, Error, HttpClient};
use ledger_link::certs::{DnsProof, Issuer, OrderRequest, inspect, leaf_der};
use ledger_link::{AcmeIssuer, certs};
use rcgen::{CertificateSigningRequestParams, CertifiedIssuer, KeyPair};
use relay::proto::{Authority, Binding};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use support::common::{self, DOMAIN, n};
const BASE: &str = "https://acme.test";
#[derive(Default)]
struct Server {
nonce: u64,
accounts: HashMap<String, String>, // kid -> thumbprint
new_accounts: Vec<serde_json::Value>,
orders: Vec<Order>,
ari_gets: u32,
refuse_orders: bool,
}
struct Order {
identifier: String,
replaces: Option<String>,
thumbprint: String,
token: String,
authz_valid: bool,
done: bool,
}
struct FakeAcme {
ca: CertifiedIssuer<'static, KeyPair>,
dns: std::net::SocketAddr,
state: Mutex<Server>,
window: (i64, i64),
}
fn jws(body: &[u8]) -> (serde_json::Value, serde_json::Value) {
let v: serde_json::Value = serde_json::from_slice(body).unwrap();
let protected: serde_json::Value =
serde_json::from_slice(&B64.decode(v["protected"].as_str().unwrap()).unwrap()).unwrap();
let payload = match v["payload"].as_str().unwrap() {
"" => serde_json::Value::Null,
p => serde_json::from_slice(&B64.decode(p).unwrap()).unwrap(),
};
(protected, payload)
}
fn thumbprint(jwk: &serde_json::Value) -> String {
let canon = format!(
r#"{{"crv":{},"kty":{},"x":{},"y":{}}}"#,
jwk["crv"], jwk["kty"], jwk["x"], jwk["y"]
);
B64.encode(Sha256::digest(canon.as_bytes()))
}
type Reply = (u16, Vec<(&'static str, String)>, Bytes);
fn json(status: u16, extra: Vec<(&'static str, String)>, v: serde_json::Value) -> Reply {
let mut h = vec![("content-type", "application/json".to_string())];
h.extend(extra);
(status, h, Bytes::from(v.to_string()))
}
impl FakeAcme {
async fn handle(&self, method: &str, path: &str, body: &[u8]) -> Reply {
let nonce = {
let mut s = self.state.lock().unwrap();
s.nonce += 1;
format!("nonce-{}", s.nonce)
};
let with_nonce = |mut r: Reply| {
r.1.push(("replay-nonce", nonce.clone()));
r
};
match (method, path) {
("GET", "/dir") => with_nonce(json(
200,
vec![],
serde_json::json!({
"newNonce": format!("{BASE}/nonce"),
"newAccount": format!("{BASE}/acct"),
"newOrder": format!("{BASE}/order"),
"renewalInfo": format!("{BASE}/ari"),
"meta": {}
}),
)),
(_, "/nonce") => with_nonce((200, vec![], Bytes::new())),
("POST", "/acct") => {
let (protected, payload) = jws(body);
let kid = format!("{BASE}/acct/1");
let mut s = self.state.lock().unwrap();
s.accounts
.insert(kid.clone(), thumbprint(&protected["jwk"]));
s.new_accounts.push(payload);
with_nonce(json(
201,
vec![("location", kid)],
serde_json::json!({"status":"valid"}),
))
}
("POST", "/order") => {
let (protected, payload) = jws(body);
let mut s = self.state.lock().unwrap();
if s.refuse_orders {
return with_nonce(json(
429,
vec![],
serde_json::json!({"type":"urn:ietf:params:acme:error:rateLimited","detail":"too many certificates already issued"}),
));
}
let thumb = s.accounts[protected["kid"].as_str().unwrap()].clone();
let identifier = payload["identifiers"][0]["value"]
.as_str()
.unwrap()
.to_owned();
let replaces = payload["replaces"].as_str().map(str::to_owned);
let id = s.orders.len() + 1;
s.orders.push(Order {
identifier,
replaces: replaces.clone(),
thumbprint: thumb,
token: format!("token-{id}"),
authz_valid: false,
done: false,
});
let mut order = serde_json::json!({
"status":"pending",
"identifiers": payload["identifiers"],
"authorizations":[format!("{BASE}/authz/{id}")],
"finalize": format!("{BASE}/finalize/{id}"),
});
if let Some(r) = replaces {
order["replaces"] = r.into();
}
with_nonce(json(201, vec![("location", format!("{BASE}/order/{id}"))], order))
}
("POST", p) if p.starts_with("/authz/") => {
let id: usize = p[7..].parse().unwrap();
let s = self.state.lock().unwrap();
let o = &s.orders[id - 1];
let ident = o.identifier.trim_start_matches("*.");
with_nonce(json(
200,
vec![],
serde_json::json!({
"status": if o.authz_valid {"valid"} else {"pending"},
"identifier": {"type":"dns","value": ident},
"wildcard": o.identifier.starts_with("*."),
"challenges":[{"type":"dns-01","url":format!("{BASE}/chall/{id}"),
"token":o.token,"status":"pending"}]
}),
))
}
("POST", p) if p.starts_with("/chall/") => {
let id: usize = p[7..].parse().unwrap();
let (ident, expected) = {
let s = self.state.lock().unwrap();
let o = &s.orders[id - 1];
let ka = format!("{}.{}", o.token, o.thumbprint);
(
o.identifier.trim_start_matches("*.").to_owned(),
B64.encode(Sha256::digest(ka.as_bytes())),
)
};
let seen =
support::dns_txt(self.dns, &format!("_acme-challenge.{ident}.")).await;
let ok = seen.contains(&expected);
self.state.lock().unwrap().orders[id - 1].authz_valid = ok;
with_nonce(json(
200,
vec![],
serde_json::json!({"type":"dns-01","url":format!("{BASE}/chall/{id}"),
"token":format!("token-{id}"),"status": if ok {"valid"} else {"invalid"}}),
))
}
("POST", p) if p.starts_with("/order/") => {
let id: usize = p[7..].parse().unwrap();
let s = self.state.lock().unwrap();
let o = &s.orders[id - 1];
let mut order = serde_json::json!({
"status": if o.done {"valid"} else if o.authz_valid {"ready"} else {"pending"},
"identifiers":[{"type":"dns","value":o.identifier}],
"authorizations":[format!("{BASE}/authz/{id}")],
"finalize": format!("{BASE}/finalize/{id}"),
});
if o.done {
order["certificate"] = format!("{BASE}/cert/{id}").into();
}
if let Some(r) = &o.replaces {
order["replaces"] = r.clone().into();
}
with_nonce(json(200, vec![], order))
}
("POST", p) if p.starts_with("/finalize/") => {
let id: usize = p[10..].parse().unwrap();
let (_, payload) = jws(body);
let csr = B64.decode(payload["csr"].as_str().unwrap()).unwrap();
let der = rustls::pki_types::CertificateSigningRequestDer::from(csr);
let mut params = CertificateSigningRequestParams::from_der(&der).unwrap();
let now = time::OffsetDateTime::now_utc();
params.params.not_before = now - time::Duration::hours(1);
params.params.not_after = now + time::Duration::days(60);
params.params.use_authority_key_identifier_extension = true;
let cert = params.signed_by(&self.ca).unwrap();
let mut s = self.state.lock().unwrap();
s.orders[id - 1].done = true;
CERTS.lock().unwrap().insert(id, format!("{}{}", cert.pem(), self.ca.pem()));
let o = &s.orders[id - 1];
with_nonce(json(
200,
vec![],
serde_json::json!({
"status":"valid",
"identifiers":[{"type":"dns","value":o.identifier}],
"authorizations":[format!("{BASE}/authz/{id}")],
"finalize": format!("{BASE}/finalize/{id}"),
"certificate": format!("{BASE}/cert/{id}"),
"replaces": o.replaces,
}),
))
}
("POST", p) if p.starts_with("/cert/") => {
let id: usize = p[6..].parse().unwrap();
let pem = CERTS.lock().unwrap()[&id].clone();
with_nonce((
200,
vec![("content-type", "application/pem-certificate-chain".into())],
Bytes::from(pem),
))
}
("GET", p) if p.starts_with("/ari/") => {
self.state.lock().unwrap().ari_gets += 1;
let now = time::OffsetDateTime::now_utc();
let at = |secs: i64| {
(now + time::Duration::seconds(secs))
.format(&time::format_description::well_known::Rfc3339)
.unwrap()
};
json(
200,
vec![("retry-after", "3600".into())],
serde_json::json!({"suggestedWindow":{"start":at(self.window.0),"end":at(self.window.1)}}),
)
}
_ => json(404, vec![], serde_json::json!({"type":"urn:ietf:params:acme:error:malformed","detail":"no such path"})),
}
}
}
static CERTS: std::sync::LazyLock<Mutex<HashMap<usize, String>>> =
std::sync::LazyLock::new(|| Mutex::new(HashMap::new()));
struct Http(Arc<FakeAcme>);
impl HttpClient for Http {
fn request(
&self,
req: http::Request<BodyWrapper<Bytes>>,
) -> Pin<Box<dyn Future<Output = Result<BytesResponse, Error>> + Send>> {
let fake = self.0.clone();
Box::pin(async move {
let (parts, body) = req.into_parts();
let bytes = body.collect().await.map_err(|e| Error::Other(e.into()))?.to_bytes();
let path = parts.uri.path().to_owned();
assert_eq!(parts.uri.host(), Some("acme.test"));
let (status, headers, out) = fake.handle(parts.method.as_str(), &path, &bytes).await;
let mut b = http::Response::builder().status(status);
for (k, v) in headers {
b = b.header(k, v);
}
let (parts, _) = b.body(()).unwrap().into_parts();
Ok(BytesResponse {
parts,
body: Box::new(out),
})
})
}
}
struct Proof(relay::client::LineHandle);
impl DnsProof for Proof {
fn publish<'a>(&'a self, value: &'a str) -> BoxFuture<'a, Result<(), String>> {
Box::pin(async move {
let r = self.0.publish_txt(value).await.map_err(|e| e.to_string())?;
if r.ok { Ok(()) } else { Err(r.error.unwrap_or_default()) }
})
}
fn clear(&self) -> BoxFuture<'_, ()> {
Box::pin(async move {
let _ = self.0.request(relay::proto::Request::ClearTxt).await;
})
}
}
struct Rig {
env: common::Env,
acme: Arc<FakeAcme>,
issuer: AcmeIssuer,
line: relay::client::Line,
dir: tempfile::TempDir,
name: relay::names::Name,
}
async fn rig(window: (i64, i64)) -> Rig {
let env = common::start(common::quick_limits()).await;
let mut params = rcgen::CertificateParams::new(Vec::<String>::new()).unwrap();
params.is_ca = rcgen::IsCa::Ca(rcgen::BasicConstraints::Unconstrained);
params.key_usages = vec![rcgen::KeyUsagePurpose::KeyCertSign];
let ca = CertifiedIssuer::self_signed(params, KeyPair::generate().unwrap()).unwrap();
let acme = Arc::new(FakeAcme {
ca,
dns: env.relay.dns_udp_addr(),
state: Mutex::new(Server::default()),
window,
});
let dir = tempfile::tempdir().unwrap();
let a2 = acme.clone();
let issuer = AcmeIssuer::new(dir.path()).with_http(Arc::new(move || {
Some(Box::new(Http(a2.clone())) as Box<dyn HttpClient>)
}));
let (key, _) = relay::proto::AppKey::generate();
let name = n("ledger-accrual-tally-mint");
let line = env.connect(name, &key, true).await.unwrap();
Rig {
env,
acme,
issuer,
line,
dir,
name,
}
}
fn authority(binding: bool) -> Authority {
Authority {
id: "fake".into(),
directory: format!("{BASE}/dir"),
needs_binding: binding,
usable: true,
binding: binding.then(|| Binding {
kid: "kid-1".into(),
hmac_key: B64.encode(b"0123456789abcdef0123456789abcdef"),
}),
}
}
/// WB-link-4: account, order, the DNS proof through the relay, finalize
/// and download, in the real client; the key is made here; the account is
/// kept privately; the relay's DNS is left clean.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn an_order_goes_through_account_proof_finalize_and_download() {
let r = rig((60, 120)).await;
let proof = Proof(r.line.handle());
let a = authority(false);
let ident = format!("*.{}.{DOMAIN}", r.name);
let issued = r
.issuer
.issue(OrderRequest {
authority: &a,
identifier: ident.clone(),
replaces: None,
dns: &proof,
})
.await
.unwrap();
let der = leaf_der(&issued.chain_pem).unwrap();
let (nb, na, names) = inspect(&der).unwrap();
assert_eq!(names, [ident.clone()]);
assert!(nb < certs::now() && na > certs::now());
assert!(issued.key_pem.contains("PRIVATE KEY"));
// The certificate and key go together (the store checks it).
assert!(certs::StoredCert::new("ledger-accrual-tally-mint", "fake", &issued, &ident, None).is_ok());
// The account is kept in a private file, with no contact in it.
let file = r.dir.path().join("acme/fake.json");
assert!(file.exists());
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
assert_eq!(std::fs::metadata(&file).unwrap().permissions().mode() & 0o777, 0o600);
}
let st = r.acme.state.lock().unwrap();
assert_eq!(st.new_accounts.len(), 1);
assert!(st.new_accounts[0]["contact"].as_array().is_none_or(|c| c.is_empty()));
assert_eq!(st.new_accounts[0]["termsOfServiceAgreed"], true);
drop(st);
// The relay holds no proof once the order is done.
let left = support::dns_txt(
r.env.relay.dns_udp_addr(),
&format!("_acme-challenge.{}.{DOMAIN}.", r.name),
)
.await;
assert!(left.is_empty(), "{left:?}");
}
/// WB-link-4: a second order reuses the account, a renewal names the
/// certificate it replaces, and the authority's renewal window is read.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_renewal_reuses_the_account_names_what_it_replaces_and_reads_the_window() {
let r = rig((600, 900)).await;
let proof = Proof(r.line.handle());
let a = authority(false);
let ident = format!("*.{}.{DOMAIN}", r.name);
let first = r
.issuer
.issue(OrderRequest {
authority: &a,
identifier: ident.clone(),
replaces: None,
dns: &proof,
})
.await
.unwrap();
let second = r
.issuer
.issue(OrderRequest {
authority: &a,
identifier: ident.clone(),
replaces: leaf_der(&first.chain_pem),
dns: &proof,
})
.await
.unwrap();
assert_ne!(first.chain_pem, second.chain_pem);
assert_ne!(*first.key_pem, *second.key_pem, "a fresh key for each certificate");
{
let st = r.acme.state.lock().unwrap();
assert_eq!(st.new_accounts.len(), 1, "the account was reused");
assert!(st.orders[0].replaces.is_none());
let id = st.orders[1].replaces.clone().unwrap();
assert!(id.contains('.'), "an ARI certificate id: {id}");
}
let (s, e) = r
.issuer
.renewal_window(&a, &first.chain_pem)
.await
.expect("the authority offers a window");
let now = certs::now();
assert!((now + 590..=now + 610).contains(&s) && (now + 890..=now + 910).contains(&e), "{s} {e} {now}");
assert_eq!(r.acme.state.lock().unwrap().ari_gets, 1);
}
/// WB-link-4: an authority that needs an account binding gets the one the
/// relay gave, and without one nothing is asked of it.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_binding_is_sent_when_there_is_one_and_never_invented() {
let r = rig((60, 120)).await;
let proof = Proof(r.line.handle());
let ident = format!("*.{}.{DOMAIN}", r.name);
let mut no = authority(true);
no.binding = None;
no.usable = false;
let err = r
.issuer
.issue(OrderRequest {
authority: &no,
identifier: ident.clone(),
replaces: None,
dns: &proof,
})
.await
.err()
.unwrap();
assert!(err.contains("needs an account binding"), "{err}");
assert!(r.acme.state.lock().unwrap().new_accounts.is_empty());
let yes = authority(true);
r.issuer
.issue(OrderRequest {
authority: &yes,
identifier: ident,
replaces: None,
dns: &proof,
})
.await
.unwrap();
let st = r.acme.state.lock().unwrap();
let eab = &st.new_accounts[0]["externalAccountBinding"];
assert!(eab.is_object(), "{:?}", st.new_accounts);
let protected: serde_json::Value =
serde_json::from_slice(&B64.decode(eab["protected"].as_str().unwrap()).unwrap()).unwrap();
assert_eq!(protected["kid"], "kid-1");
}
/// WB-link-4: an authority's refusal comes back in its own words.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_refusal_comes_back_in_the_authoritys_words() {
let r = rig((60, 120)).await;
r.acme.state.lock().unwrap().refuse_orders = true;
let proof = Proof(r.line.handle());
let err = r
.issuer
.issue(OrderRequest {
authority: &authority(false),
identifier: format!("*.{}.{DOMAIN}", r.name),
replaces: None,
dns: &proof,
})
.await
.err()
.unwrap();
assert!(err.contains("too many certificates"), "{err}");
}