httpsd 0.1.0

A pure-Rust HTTP/HTTPS server — usable as a sans-I/O library with pluggable runtimes (thread pool, tokio, mio) or as a CLI that serves a directory or a TOML config.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! A minimal ACME (RFC 8555) client over the `rsurl` HTTPS client.
//!
//! Covers exactly the flow this server needs: account registration (with ToS
//! agreement and optional contact email), order creation for a set of DNS
//! names, challenge fulfilment via a pluggable [`ChallengeSolver`]
//! (TLS-ALPN-01 / HTTP-01), CSR finalization, and certificate download.

use std::thread::sleep;
use std::time::{Duration, Instant};

use purecrypto::ec::{BoxedEcdsaPrivateKey, CurveId};
use purecrypto::x509::{CertSigner, CertificationRequest, DistinguishedName};
use rsurl::Request;

use super::jose::{AccountKey, KeyId, b64url};
use super::json::{self, Value};
use crate::error::{Error, Result};

/// Let's Encrypt production directory URL.
pub const LETSENCRYPT_PRODUCTION: &str = "https://acme-v02.api.letsencrypt.org/directory";
/// Let's Encrypt staging directory URL (untrusted certs, high rate limits).
pub const LETSENCRYPT_STAGING: &str = "https://acme-staging-v02.api.letsencrypt.org/directory";

/// Installs and removes challenge responses. The manager backs this with the
/// shared maps the TLS router (TLS-ALPN-01) and HTTP listener (HTTP-01) read.
pub trait ChallengeSolver {
    /// Challenge types this solver can satisfy, in preference order
    /// (e.g. `["tls-alpn-01", "http-01"]`).
    fn preferred(&self) -> &[&'static str];
    /// Make the challenge response retrievable by the CA.
    fn present(&self, typ: &str, host: &str, token: &str, key_auth: &str) -> Result<()>;
    /// Tear down a previously presented challenge.
    fn cleanup(&self, typ: &str, host: &str, token: &str);
}

/// A freshly issued certificate.
pub struct Issued {
    /// PEM certificate chain (leaf first).
    pub chain_pem: String,
    /// PEM private key for the certificate.
    pub key_pem: String,
}

/// The ACME directory endpoints we use.
struct Directory {
    new_nonce: String,
    new_account: String,
    new_order: String,
}

/// An ACME client bound to one directory and account key.
pub struct AcmeClient {
    dir: Directory,
    account: AccountKey,
    kid: Option<String>,
    nonce: Option<String>,
    email: Option<String>,
}

impl AcmeClient {
    /// Connect to an ACME directory with the given account key. Pass the account
    /// `email` for the `contact` field (optional). The caller is responsible for
    /// having obtained ToS agreement from the operator before calling
    /// [`ensure_account`](Self::ensure_account).
    pub fn new(
        directory_url: &str,
        account: AccountKey,
        email: Option<String>,
    ) -> Result<AcmeClient> {
        let resp = http_get(directory_url)?;
        let doc = parse_json(&resp.body)?;
        let dir = Directory {
            new_nonce: field(&doc, "newNonce")?,
            new_account: field(&doc, "newAccount")?,
            new_order: field(&doc, "newOrder")?,
        };
        Ok(AcmeClient {
            dir,
            account,
            kid: None,
            nonce: None,
            email,
        })
    }

    /// The account key, e.g. to persist after a successful registration.
    pub fn account(&self) -> &AccountKey {
        &self.account
    }

    /// Register (or look up) the account, agreeing to the CA's terms of service.
    /// Idempotent: an existing account for this key resolves to the same URL.
    pub fn ensure_account(&mut self) -> Result<()> {
        let payload = match &self.email {
            Some(e) => json::obj(&[
                ("termsOfServiceAgreed", "true".into()),
                ("contact", format!(r#"["mailto:{}"]"#, json::escape(e))),
            ]),
            None => json::obj(&[("termsOfServiceAgreed", "true".into())]),
        };
        let url = self.dir.new_account.clone();
        let resp = self.post(&url, &payload, KeyId::Jwk)?;
        if resp.status >= 400 {
            return Err(acme_err("newAccount", &resp.body));
        }
        let kid = resp
            .header("location")
            .ok_or_else(|| Error::Acme("newAccount: no Location (account URL)".into()))?;
        self.kid = Some(kid.to_owned());
        Ok(())
    }

    /// Obtain a certificate for `dns_names`, driving challenges via `solver`.
    pub fn issue(&mut self, dns_names: &[&str], solver: &dyn ChallengeSolver) -> Result<Issued> {
        if self.kid.is_none() {
            self.ensure_account()?;
        }

        // 1. Create the order.
        let identifiers = dns_names
            .iter()
            .map(|d| format!(r#"{{"type":"dns","value":"{}"}}"#, json::escape(d)))
            .collect::<Vec<_>>()
            .join(",");
        let order_payload = format!(r#"{{"identifiers":[{identifiers}]}}"#);
        let new_order = self.dir.new_order.clone();
        let resp = self.post(&new_order, &order_payload, self.kid_auth()?)?;
        if resp.status >= 400 {
            return Err(acme_err("newOrder", &resp.body));
        }
        let order_url = resp
            .header("location")
            .ok_or_else(|| Error::Acme("newOrder: no order URL".into()))?
            .to_owned();
        let order = parse_json(&resp.body)?;

        // 2. Satisfy each authorization.
        let authzs = order
            .get("authorizations")
            .and_then(Value::as_array)
            .ok_or_else(|| Error::Acme("order has no authorizations".into()))?
            .to_vec();
        for authz in &authzs {
            let url = authz
                .as_str()
                .ok_or_else(|| Error::Acme("bad authorization URL".into()))?;
            self.do_authorization(url, solver)?;
        }

        // 3. Finalize with a CSR (and a fresh certificate key).
        let finalize = order
            .str_at("finalize")
            .ok_or_else(|| Error::Acme("order has no finalize URL".into()))?
            .to_owned();
        let mut rng = purecrypto::rng::OsRng;
        let cert_key = BoxedEcdsaPrivateKey::generate(CurveId::P256, &mut rng);
        let csr_der = build_csr(&cert_key, dns_names)?;
        let finalize_payload = format!(r#"{{"csr":"{}"}}"#, b64url(&csr_der));
        let resp = self.post(&finalize, &finalize_payload, self.kid_auth()?)?;
        if resp.status >= 400 {
            return Err(acme_err("finalize", &resp.body));
        }

        // 4. Poll the order to "valid", then download the certificate.
        let cert_url = self.poll_order(&order_url)?;
        let resp = self.post_as_get(&cert_url)?;
        if resp.status >= 400 {
            return Err(acme_err("certificate", &resp.body));
        }
        let chain_pem = String::from_utf8(resp.body)
            .map_err(|_| Error::Acme("certificate is not valid UTF-8 PEM".into()))?;

        Ok(Issued {
            chain_pem,
            key_pem: cert_key.to_sec1_pem(),
        })
    }

    fn do_authorization(&mut self, url: &str, solver: &dyn ChallengeSolver) -> Result<()> {
        let resp = self.post_as_get(url)?;
        if resp.status >= 400 {
            return Err(acme_err("authz", &resp.body));
        }
        let authz = parse_json(&resp.body)?;
        if authz.str_at("status") == Some("valid") {
            return Ok(()); // already authorized (reused)
        }
        let host = authz
            .get("identifier")
            .and_then(|i| i.str_at("value"))
            .ok_or_else(|| Error::Acme("authz missing identifier".into()))?
            .to_owned();
        let challenges = authz
            .get("challenges")
            .and_then(Value::as_array)
            .ok_or_else(|| Error::Acme("authz has no challenges".into()))?;

        // Pick the most-preferred challenge the solver supports.
        let (ctype, curl, token) = self
            .select_challenge(challenges, solver)
            .ok_or_else(|| Error::Acme("no supported challenge offered".into()))?;
        let key_auth = self.account.key_authorization(&token);

        solver.present(&ctype, &host, &token, &key_auth)?;
        let result = (|| {
            // Tell the CA we're ready (empty object payload).
            let resp = self.post(&curl, "{}", self.kid_auth()?)?;
            if resp.status >= 400 {
                return Err(acme_err("challenge", &resp.body));
            }
            self.poll_authorization(url)
        })();
        solver.cleanup(&ctype, &host, &token);
        result
    }

    fn select_challenge(
        &self,
        challenges: &[Value],
        solver: &dyn ChallengeSolver,
    ) -> Option<(String, String, String)> {
        for &want in solver.preferred() {
            for ch in challenges {
                if ch.str_at("type") == Some(want) {
                    let url = ch.str_at("url")?.to_owned();
                    let token = ch.str_at("token")?.to_owned();
                    return Some((want.to_owned(), url, token));
                }
            }
        }
        None
    }

    fn poll_authorization(&mut self, url: &str) -> Result<()> {
        let deadline = Instant::now() + Duration::from_secs(60);
        loop {
            let resp = self.post_as_get(url)?;
            let authz = parse_json(&resp.body)?;
            match authz.str_at("status") {
                Some("valid") => return Ok(()),
                Some("pending") | Some("processing") => {}
                Some("invalid") => return Err(acme_err("authorization invalid", &resp.body)),
                other => {
                    return Err(Error::Acme(format!("unexpected authz status: {other:?}")));
                }
            }
            if Instant::now() >= deadline {
                return Err(Error::Acme("authorization timed out".into()));
            }
            sleep(Duration::from_secs(2));
        }
    }

    /// Poll the order until `valid`, returning its `certificate` URL.
    fn poll_order(&mut self, order_url: &str) -> Result<String> {
        let deadline = Instant::now() + Duration::from_secs(60);
        loop {
            let resp = self.post_as_get(order_url)?;
            let order = parse_json(&resp.body)?;
            match order.str_at("status") {
                Some("valid") => {
                    return order
                        .str_at("certificate")
                        .map(str::to_owned)
                        .ok_or_else(|| Error::Acme("valid order has no certificate URL".into()));
                }
                Some("processing") | Some("pending") | Some("ready") => {}
                Some("invalid") => return Err(acme_err("order invalid", &resp.body)),
                other => return Err(Error::Acme(format!("unexpected order status: {other:?}"))),
            }
            if Instant::now() >= deadline {
                return Err(Error::Acme("order finalization timed out".into()));
            }
            sleep(Duration::from_secs(2));
        }
    }

    // ---- transport ----

    fn kid_auth(&self) -> Result<KeyId> {
        self.kid
            .clone()
            .map(KeyId::Kid)
            .ok_or_else(|| Error::Acme("no account registered".into()))
    }

    fn fresh_nonce(&mut self) -> Result<String> {
        let resp = http_head(&self.dir.new_nonce)?;
        resp.header("replay-nonce")
            .map(str::to_owned)
            .ok_or_else(|| Error::Acme("newNonce returned no Replay-Nonce".into()))
    }

    /// JWS-signed POST, refreshing the nonce and retrying once on `badNonce`.
    fn post(&mut self, url: &str, payload: &str, auth: KeyId) -> Result<rsurl::Response> {
        for attempt in 0..2 {
            let nonce = match self.nonce.take() {
                Some(n) => n,
                None => self.fresh_nonce()?,
            };
            let body = self.account.sign(url, &nonce, &auth, payload)?;
            let resp = http_post_jose(url, body)?;
            if let Some(n) = resp.header("replay-nonce") {
                self.nonce = Some(n.to_owned());
            }
            if resp.status == 400 && attempt == 0 && is_bad_nonce(&resp.body) {
                self.nonce = None; // force a fresh nonce and retry
                continue;
            }
            return Ok(resp);
        }
        unreachable!("post loop always returns")
    }

    fn post_as_get(&mut self, url: &str) -> Result<rsurl::Response> {
        let auth = self.kid_auth()?;
        self.post(url, "", auth)
    }
}

/// Build a DER CSR for `dns_names` signed by `key`.
fn build_csr(key: &BoxedEcdsaPrivateKey, dns_names: &[&str]) -> Result<Vec<u8>> {
    let subject = DistinguishedName::common_name(dns_names[0]);
    let csr = CertificationRequest::create(&CertSigner::Ecdsa(key), &subject, dns_names)
        .map_err(|e| Error::Acme(format!("CSR: {e:?}")))?;
    Ok(csr.to_der().to_vec())
}

fn field(doc: &Value, key: &str) -> Result<String> {
    doc.str_at(key)
        .map(str::to_owned)
        .ok_or_else(|| Error::Acme(format!("directory missing {key}")))
}

fn parse_json(body: &[u8]) -> Result<Value> {
    let text = std::str::from_utf8(body).map_err(|_| Error::Acme("non-UTF-8 response".into()))?;
    json::parse(text)
}

/// Whether a 400 body is an ACME `badNonce` error.
fn is_bad_nonce(body: &[u8]) -> bool {
    std::str::from_utf8(body)
        .ok()
        .and_then(|t| json::parse(t).ok())
        .and_then(|v| v.str_at("type").map(|s| s.contains("badNonce")))
        .unwrap_or(false)
}

/// Build an error from an ACME problem document (RFC 7807).
fn acme_err(ctx: &str, body: &[u8]) -> Error {
    let detail = std::str::from_utf8(body)
        .ok()
        .and_then(|t| json::parse(t).ok())
        .and_then(|v| v.str_at("detail").map(str::to_owned))
        .unwrap_or_else(|| String::from_utf8_lossy(body).into_owned());
    Error::Acme(format!("{ctx}: {detail}"))
}

fn map_rsurl(e: rsurl::Error) -> Error {
    Error::Acme(format!("http: {e}"))
}

fn http_get(url: &str) -> Result<rsurl::Response> {
    Request::new("GET", url)
        .map_err(map_rsurl)?
        .send()
        .map_err(map_rsurl)
}

fn http_head(url: &str) -> Result<rsurl::Response> {
    Request::new("HEAD", url)
        .map_err(map_rsurl)?
        .send()
        .map_err(map_rsurl)
}

fn http_post_jose(url: &str, body: String) -> Result<rsurl::Response> {
    Request::new("POST", url)
        .map_err(map_rsurl)?
        .header("content-type", "application/jose+json")
        .body(body.into_bytes())
        .send()
        .map_err(map_rsurl)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn bad_nonce_detection() {
        let body = br#"{"type":"urn:ietf:params:acme:error:badNonce","detail":"bad"}"#;
        assert!(is_bad_nonce(body));
        assert!(!is_bad_nonce(
            br#"{"type":"urn:ietf:params:acme:error:malformed"}"#
        ));
    }

    #[test]
    fn problem_detail_extracted() {
        let e = acme_err("newOrder", br#"{"type":"x","detail":"rejected: bad id"}"#);
        assert!(format!("{e}").contains("rejected: bad id"));
    }

    #[test]
    fn csr_builds_for_names() {
        let mut rng = purecrypto::rng::OsRng;
        let key = BoxedEcdsaPrivateKey::generate(CurveId::P256, &mut rng);
        let der = build_csr(&key, &["a.example", "b.example"]).unwrap();
        assert!(!der.is_empty());
    }
}