certmagic 0.2.0

Automatic TLS certificate acquisition, renewal, and maintenance for Rust servers
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
//! rustls integration glue.
//!
//! Three integration paths:
//! - **A. sync resolver**: [`Config::tls_config`] wires a
//!   [`CertmagicResolver`] that serves from the in-memory cache with no IO.
//! - **B. async acceptor**: [`CertmagicAcceptor`] (built on
//!   `tokio_rustls::LazyConfigAcceptor`) runs the full async
//!   `get_certificate` (storage load, on-demand issuance, maintenance)
//!   *before* completing the handshake.
//! - **C. background remediation**: when the sync resolver misses and
//!   on-demand is enabled, it spawns issuance in the background and fails
//!   this handshake; the next connection succeeds.

use std::collections::HashMap;
use std::sync::{Arc, Mutex, RwLock};
use std::time::Duration;

use rustls::pki_types::PrivateKeyDer;
use rustls::server::{ClientHello, ResolvesServerCert};
use rustls::sign::CertifiedKey;
use tokio_util::sync::CancellationToken;

use crate::certificate::normalize_sni;
use crate::config::Config;
use crate::handshake::ClientHelloInfo;

/// Source used to construct a [`CertmagicResolver`].
///
/// A resolver can be attached either to a complete [`Config`] (which keeps
/// certmagic's default-name and on-demand behavior) or directly to a
/// [`crate::cache::Cache`].  The cache form is useful when the caller owns
/// the `rustls::ServerConfig` assembly.
#[derive(Clone, Debug)]
pub enum ResolverSource {
    /// Use the complete configuration, including its handshake policy.
    Config(Arc<Config>),
    /// Resolve only from the supplied in-memory certificate cache.
    Cache(Arc<crate::cache::Cache>),
}

impl From<Arc<Config>> for ResolverSource {
    fn from(config: Arc<Config>) -> Self {
        Self::Config(config)
    }
}

impl From<Arc<crate::cache::Cache>> for ResolverSource {
    fn from(cache: Arc<crate::cache::Cache>) -> Self {
        Self::Cache(cache)
    }
}

/// Snapshot a rustls ClientHello into our owned representation.
#[must_use]
pub fn client_hello_from_rustls(ch: &ClientHello<'_>) -> ClientHelloInfo {
    ClientHelloInfo {
        server_name: ch.server_name().map(str::to_owned),
        alpn: ch
            .alpn()
            .into_iter()
            .flatten()
            .map(<[u8]>::to_vec)
            .collect(),
        remote_addr: None,
        local_addr: None,
        signature_schemes: ch
            .signature_schemes()
            .iter()
            .map(|s| u16::from(*s))
            .collect(),
        // rustls 0.23 ClientHello exposes negotiated-algorithm getters only;
        // the raw version list is not part of its public surface.
        supported_versions: Vec::new(),
        cipher_suites: ch.cipher_suites().iter().map(|c| u16::from(*c)).collect(),
    }
}

/// Convert a cached [`Certificate`] into a rustls [`CertifiedKey`]
/// (signing-key construction is cached per chain hash).
pub(crate) fn to_certified_key(
    cert: &crate::certificate::Certificate,
    cache: &Mutex<HashMap<String, Arc<CertifiedKey>>>,
) -> Option<Arc<CertifiedKey>> {
    {
        let mut keys = cache.lock().ok()?;
        if let Some(hit) = keys.get_mut(cert.hash()) {
            if hit.ocsp != cert.ocsp_staple {
                // Preserve signing-key reuse while publishing the new staple.
                // Existing handshakes retain their immutable Arc snapshot.
                Arc::make_mut(hit).ocsp = cert.ocsp_staple.clone();
            }
            return Some(Arc::clone(hit));
        }
    }
    let signing_key = match &cert.signing_key {
        Some(key) => Arc::clone(key),
        None => {
            let key_der: &PrivateKeyDer<'_> = cert.private_key.as_deref()?;
            signing_key_from_der(key_der)?
        }
    };
    let mut ck = CertifiedKey::new(cert.chain.clone(), signing_key);
    ck.ocsp = cert.ocsp_staple.clone();
    let ck = Arc::new(ck);
    if let Ok(mut map) = cache.lock() {
        map.insert(cert.hash().to_owned(), Arc::clone(&ck));
    }
    Some(ck)
}

/// Sync rustls resolver: cache fast-path (scheme A) + optional background
/// remediation (scheme C).
pub struct CertmagicResolver {
    cache: Arc<crate::cache::Cache>,
    config: Option<Arc<Config>>,
    keys: Mutex<HashMap<String, Arc<CertifiedKey>>>,
    /// Optional on-demand settings supplied through the cache-first
    /// constructor. The owning config is resolved lazily from the cache so a
    /// resolver can be constructed before `Config::new` registers its owner.
    on_demand: Option<Arc<crate::config::OnDemandConfig>>,
    /// Explicit resolver policy overrides. `None` means use the config's
    /// corresponding option when the resolver is config-backed.
    default_server_name: Option<String>,
    fallback_server_name: Option<String>,
    /// Runtime TLS-ALPN challenge certificates. These must be independently
    /// mutable because rustls stores the resolver behind an `Arc`.
    challenge_certs: RwLock<HashMap<String, Arc<CertifiedKey>>>,
    /// Certificate returned when no SNI/cache match is available.
    default_cert: RwLock<Option<Arc<CertifiedKey>>>,
    /// Enable scheme C (set when the config enables on-demand).
    background_remediation: bool,
}

impl CertmagicResolver {
    /// Create a resolver backed by an in-memory certificate cache or config.
    ///
    /// Passing `Arc<Cache>` selects the cache-only form. Passing `Arc<Config>` keeps
    /// the legacy certmagic form and retains its default-name behavior.
    /// The resolver starts in cache-only mode; use
    /// [`Self::with_background_remediation`] when an uncached name should
    /// trigger a detached on-demand obtain.
    #[must_use]
    pub fn new<S>(source: S) -> Self
    where
        S: Into<ResolverSource>,
    {
        match source.into() {
            ResolverSource::Config(config) => Self::with_background_remediation(config, false),
            ResolverSource::Cache(cache) => Self {
                cache,
                config: None,
                keys: Mutex::new(HashMap::new()),
                on_demand: None,
                default_server_name: None,
                fallback_server_name: None,
                challenge_certs: RwLock::new(HashMap::new()),
                default_cert: RwLock::new(None),
                background_remediation: false,
            },
        }
    }

    /// Create a cache-only resolver explicitly.
    #[must_use]
    pub fn from_cache(cache: Arc<crate::cache::Cache>) -> Self {
        Self::new(cache)
    }

    /// Create a resolver explicitly from a complete [`Config`].
    #[must_use]
    pub fn from_config(config: Arc<Config>) -> Self {
        Self::new(config)
    }

    /// Create a resolver and choose whether cache misses should schedule a
    /// background on-demand obtain.
    #[must_use]
    pub fn with_background_remediation(config: Arc<Config>, enabled: bool) -> Self {
        Self {
            cache: Arc::clone(config.cache()),
            config: Some(config),
            keys: Mutex::new(HashMap::new()),
            on_demand: None,
            default_server_name: None,
            fallback_server_name: None,
            challenge_certs: RwLock::new(HashMap::new()),
            default_cert: RwLock::new(None),
            background_remediation: enabled,
        }
    }

    /// Create a resolver with cache-owned on-demand settings.
    ///
    /// The cache owner supplies the complete issuance pipeline. Keeping the
    /// `OnDemandConfig` here preserves the simple constructor shape while retaining
    /// certmagic's fail-closed admission and retry logic in `Config`.
    #[must_use]
    pub fn with_on_demand(
        cache: Arc<crate::cache::Cache>,
        on_demand: Arc<crate::config::OnDemandConfig>,
    ) -> Self {
        Self {
            cache,
            config: None,
            keys: Mutex::new(HashMap::new()),
            on_demand: Some(on_demand),
            default_server_name: None,
            fallback_server_name: None,
            challenge_certs: RwLock::new(HashMap::new()),
            default_cert: RwLock::new(None),
            background_remediation: true,
        }
    }

    /// Set the server name used when the ClientHello has no SNI.
    pub fn set_default_server_name(&mut self, name: Option<String>) {
        self.default_server_name = name;
    }

    /// Set the server name used when no certificate matches the requested SNI.
    pub fn set_fallback_server_name(&mut self, name: Option<String>) {
        self.fallback_server_name = name;
    }

    /// Register a TLS-ALPN-01 challenge certificate for `domain`.
    pub async fn set_challenge_cert(&self, domain: String, cert: Arc<CertifiedKey>) {
        let Some(domain) = normalize_cache_lookup_name(Some(&domain)) else {
            return;
        };
        if let Ok(mut certificates) = self.challenge_certs.write() {
            certificates.insert(domain, cert);
        }
    }

    /// Remove a previously registered TLS-ALPN-01 challenge certificate.
    pub async fn remove_challenge_cert(&self, domain: &str) {
        let Some(domain) = normalize_cache_lookup_name(Some(domain)) else {
            return;
        };
        if let Ok(mut certificates) = self.challenge_certs.write() {
            certificates.remove(&domain);
        }
    }

    /// Set the certificate used as the final resolver fallback.
    pub async fn set_default_cert(&self, cert: Arc<CertifiedKey>) {
        if let Ok(mut default_cert) = self.default_cert.write() {
            *default_cert = Some(cert);
        }
    }

    /// Clear the resolver's final fallback certificate.
    pub async fn clear_default_cert(&self) {
        if let Ok(mut default_cert) = self.default_cert.write() {
            *default_cert = None;
        }
    }

    fn governing_config(&self) -> Option<Arc<Config>> {
        self.config.clone().or_else(|| {
            self.cache.owner.read().ok().and_then(|owner| {
                owner
                    .as_ref()
                    .and_then(crate::config::CachedConfig::upgrade)
            })
        })
    }

    fn policy_name(&self, explicit: Option<&String>, config_name: &str) -> Option<String> {
        explicit
            .map(String::as_str)
            .or_else(|| (!config_name.is_empty()).then_some(config_name))
            .and_then(|name| normalize_cache_lookup_name(Some(name)))
    }
}

impl std::fmt::Debug for CertmagicResolver {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CertmagicResolver")
            .field("background_remediation", &self.background_remediation)
            .finish()
    }
}

impl ResolvesServerCert for CertmagicResolver {
    fn resolve(&self, client_hello: ClientHello<'_>) -> Option<Arc<CertifiedKey>> {
        let hello = client_hello_from_rustls(&client_hello);
        let config = self.governing_config();
        let explicit_default = self.default_server_name.as_ref();
        let explicit_fallback = self.fallback_server_name.as_ref();
        let configured_default = config
            .as_ref()
            .map_or("", |config| config.options.default_server_name.as_str());
        let configured_fallback = config
            .as_ref()
            .map_or("", |config| config.options.fallback_server_name.as_str());

        // The config-backed path owns the local-IP/default-name policy. A
        // cache-only resolver has no default name, so only a present, valid
        // SNI may reach the cache. In particular, do not let an empty name
        // fall through to Cache's global `*` wildcard.
        let requested_name = normalize_cache_lookup_name(hello.server_name.as_deref());
        let default_name = self.policy_name(explicit_default, configured_default);
        let name = requested_name.clone().or_else(|| default_name.clone());

        // TLS-ALPN-01 must win over ordinary certificate selection. The
        // challenge map is resolver-local, avoiding cross-config leakage from
        // the solver's process-global registry.
        if hello.alpn.iter().any(|alpn| alpn == b"acme-tls/1")
            && let Some(name) = requested_name.as_deref()
            && let Some(cert) = self
                .challenge_certs
                .read()
                .ok()
                .and_then(|certificates| certificates.get(name).cloned())
        {
            return Some(cert);
        }

        let cached = self
            .config
            .as_ref()
            .and_then(|config| config.get_cached_cert_sync(&hello))
            .or_else(|| {
                if self
                    .config
                    .as_ref()
                    .is_some_and(|config| config.options.cert_selection.is_some())
                {
                    return None;
                }
                name.as_deref()
                    .and_then(|name| self.cache.first_matching_certificate(name))
            });
        if let Some(cert) = cached {
            return to_certified_key(&cert, &self.keys);
        }

        // The fallback server name is consulted after the requested name
        // misses. Explicit resolver policy takes precedence over Config.
        let fallback_name = self.policy_name(explicit_fallback, configured_fallback);
        if let Some(fallback_name) = fallback_name.as_deref()
            && fallback_name != name.as_deref().unwrap_or_default()
            && let Some(cert) = self.cache.first_matching_certificate(fallback_name)
        {
            return to_certified_key(&cert, &self.keys);
        }

        if let Some(cert) = self.default_cert.read().ok().and_then(|cert| cert.clone()) {
            return Some(cert);
        }

        // Scheme C: fail this handshake, obtain in the background.
        if (self.background_remediation || self.on_demand.is_some())
            && let Some(config) = config
        {
            let background_name = requested_name.or(name).unwrap_or_default();
            if !background_name.is_empty() {
                config.obtain_in_background(&background_name);
            }
        }
        None
    }
}

#[cfg(test)]
mod resolver_name_tests {
    use super::CertmagicResolver;
    use super::normalize_cache_lookup_name;
    use crate::cache::{Cache, CacheOptions};
    use crate::config::OnDemandConfig;
    use crate::solvers::tls_alpn::TlsAlpnSolver;
    use std::sync::Arc;

    #[test]
    fn cache_lookup_name_normalizes_idna_and_case() {
        assert_eq!(
            normalize_cache_lookup_name(Some("  BÜCHER.Example  ")),
            Some("xn--bcher-kva.example".to_owned())
        );
    }

    #[test]
    fn cache_lookup_name_rejects_empty_or_invalid_sni() {
        assert_eq!(normalize_cache_lookup_name(None), None);
        assert_eq!(normalize_cache_lookup_name(Some("  ")), None);
        assert_eq!(normalize_cache_lookup_name(Some("bad\0name")), None);
    }

    #[tokio::test]
    async fn resolver_certificate_methods_are_thread_safe() {
        let cache = Cache::new_without_maintenance(CacheOptions::default()).unwrap();
        let resolver = CertmagicResolver::with_on_demand(
            Arc::clone(&cache),
            Arc::new(OnDemandConfig::default()),
        );
        let challenge =
            TlsAlpnSolver::generate_challenge_cert("challenge.example.com", "token.thumbprint")
                .unwrap();
        let challenge_key = Arc::clone(&challenge.certified_key);

        resolver
            .set_challenge_cert("CHALLENGE.Example.com".into(), Arc::clone(&challenge_key))
            .await;
        assert!(
            resolver
                .challenge_certs
                .read()
                .unwrap()
                .contains_key("challenge.example.com")
        );

        resolver
            .remove_challenge_cert("challenge.example.com")
            .await;
        assert!(resolver.challenge_certs.read().unwrap().is_empty());

        resolver.set_default_cert(Arc::clone(&challenge_key)).await;
        assert!(resolver.default_cert.read().unwrap().is_some());
        resolver.clear_default_cert().await;
        assert!(resolver.default_cert.read().unwrap().is_none());

        cache.stop_now();
    }

    #[test]
    fn server_name_overrides_are_mutable_before_arc_wrapping() {
        let cache = Cache::new_without_maintenance(CacheOptions::default()).unwrap();
        let mut resolver = CertmagicResolver::new(Arc::clone(&cache));
        resolver.set_default_server_name(Some("default.example.com".into()));
        resolver.set_fallback_server_name(Some("fallback.example.com".into()));
        assert_eq!(
            resolver.default_server_name.as_deref(),
            Some("default.example.com")
        );
        assert_eq!(
            resolver.fallback_server_name.as_deref(),
            Some("fallback.example.com")
        );
        resolver.set_default_server_name(None);
        resolver.set_fallback_server_name(None);
        assert!(resolver.default_server_name.is_none());
        assert!(resolver.fallback_server_name.is_none());
        cache.stop_now();
    }
}

fn normalize_cache_lookup_name(server_name: Option<&str>) -> Option<String> {
    server_name
        .filter(|name| !name.chars().any(|ch| ch.is_control()))
        .and_then(|server_name| normalize_sni(server_name).ok())
        .filter(|name| !name.is_empty())
}

impl Config {
    /// Build a rustls [`rustls::ServerConfig`] serving this config's cached
    /// certificates (scheme A). For on-demand issuance during the handshake,
    /// prefer [`Self::certmagic_acceptor`] (scheme B).
    ///
    /// # Errors
    /// [`crate::error::Error::Config`] when the rustls provider is missing.
    pub fn tls_config(self: &Arc<Self>) -> crate::error::Result<rustls::ServerConfig> {
        let _provider = default_provider().install_default();
        let on_demand = self.options.on_demand.is_some();
        let resolver = CertmagicResolver::with_background_remediation(Arc::clone(self), on_demand);
        let builder = rustls::ServerConfig::builder_with_provider(Arc::new(default_provider()))
            .with_safe_default_protocol_versions()
            .map_err(|e| {
                crate::error::Error::Config(crate::error::ConfigError::Invalid(e.to_string()))
            })?;
        let mut config = builder
            .with_no_client_auth()
            .with_cert_resolver(Arc::new(resolver));
        config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
        Ok(config)
    }

    /// Alias for [`Self::tls_config`].
    pub fn server_config(self: &Arc<Self>) -> crate::error::Result<rustls::ServerConfig> {
        self.tls_config()
    }

    /// Build the async acceptor (scheme B): resolves certificates with the
    /// full async path (storage load, on-demand issuance, maintenance) before
    /// the TLS handshake completes.
    ///
    /// # Errors
    /// Same as [`Self::tls_config`].
    pub fn certmagic_acceptor(self: &Arc<Self>) -> crate::error::Result<CertmagicAcceptor> {
        let base = self.tls_config()?;
        Ok(CertmagicAcceptor {
            config: Arc::clone(self),
            base: Arc::new(base),
            keys: Mutex::new(HashMap::new()),
        })
    }
}

/// Per-connection resolver holding an already-resolved certificate.
#[derive(Debug)]
struct StaticResolver(Arc<CertifiedKey>);

impl ResolvesServerCert for StaticResolver {
    fn resolve(&self, _client_hello: ClientHello<'_>) -> Option<Arc<CertifiedKey>> {
        Some(Arc::clone(&self.0))
    }
}

/// The async TLS acceptor (scheme B).
pub struct CertmagicAcceptor {
    config: Arc<Config>,
    base: Arc<rustls::ServerConfig>,
    keys: Mutex<HashMap<String, Arc<CertifiedKey>>>,
}

impl std::fmt::Debug for CertmagicAcceptor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CertmagicAcceptor").finish()
    }
}

impl CertmagicAcceptor {
    pub(crate) fn use_http1(&mut self) {
        Arc::make_mut(&mut self.base).alpn_protocols = vec![b"http/1.1".to_vec()];
    }

    /// Resolve the certificate for `hello` through the async path and return
    /// a per-connection [`rustls::ServerConfig`].
    async fn config_for(
        &self,
        hello: &ClientHelloInfo,
        ct: &CancellationToken,
    ) -> std::io::Result<rustls::ServerConfig> {
        let cert = self
            .config
            .get_certificate(ct, hello)
            .await
            .map_err(std::io::Error::other)?;

        let key = to_certified_key(&cert, &self.keys)
            .ok_or_else(|| std::io::Error::other("private key unavailable for serving"))?;

        let mut config = (*self.base).clone();
        if hello.is_acme_tls_alpn() {
            config.alpn_protocols = vec![crate::handshake::ACMETLS1_PROTOCOL.as_bytes().to_vec()];
        }
        config.cert_resolver = Arc::new(StaticResolver(key));
        Ok(config)
    }

    /// Accept a TCP stream: read the ClientHello, resolve the certificate
    /// asynchronously (possibly issuing on-demand), then complete the TLS
    /// handshake.
    ///
    /// # Errors
    /// `std::io::Error` for TLS alert / resolution failures.
    pub async fn accept<IO>(&self, stream: IO) -> std::io::Result<tokio_rustls::Accept<IO>>
    where
        IO: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin,
    {
        let ct = CancellationToken::new();
        self.accept_with_ct(stream, &ct).await
    }

    /// Like [`Self::accept`] with a caller-controlled cancellation scope.
    ///
    /// # Errors
    /// See [`Self::accept`].
    pub async fn accept_with_ct<IO>(
        &self,
        stream: IO,
        ct: &CancellationToken,
    ) -> std::io::Result<tokio_rustls::Accept<IO>>
    where
        IO: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin,
    {
        use tokio_rustls::LazyConfigAcceptor;
        let start = tokio::select! {
            () = ct.cancelled() => {
                return Err(std::io::Error::new(std::io::ErrorKind::Interrupted, "accept canceled"))
            }
            started = LazyConfigAcceptor::new(rustls::server::Acceptor::default(), stream) => started?,
        };
        let hello = client_hello_from_rustls(&start.client_hello());
        let config = self.config_for(&hello, ct).await?;
        Ok(start.into_stream(Arc::new(config)))
    }

    /// Convenience: accept with a handshake timeout
    /// (on-demand handshakes are bounded at 180 s).
    ///
    /// # Errors
    /// See [`Self::accept`].
    pub async fn accept_with_timeout<IO>(
        &self,
        stream: IO,
        timeout: Duration,
    ) -> std::io::Result<tokio_rustls::Accept<IO>>
    where
        IO: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin,
    {
        let ct = CancellationToken::new();
        match tokio::time::timeout(timeout, self.accept_with_ct(stream, &ct)).await {
            Ok(result) => result,
            Err(_) => Err(std::io::Error::new(
                std::io::ErrorKind::TimedOut,
                "handshake resolution timed out",
            )),
        }
    }
}

/// Install the selected crypto provider once for the process (idempotent).
pub fn install_default_provider() {
    let _ = default_provider().install_default();
}

fn default_provider() -> rustls::crypto::CryptoProvider {
    #[cfg(feature = "aws-lc-rs")]
    {
        rustls::crypto::aws_lc_rs::default_provider()
    }
    #[cfg(all(feature = "ring", not(feature = "aws-lc-rs")))]
    {
        rustls::crypto::ring::default_provider()
    }
}

pub(crate) fn signing_key_from_der(
    key_der: &PrivateKeyDer<'_>,
) -> Option<Arc<dyn rustls::sign::SigningKey>> {
    #[cfg(feature = "aws-lc-rs")]
    {
        rustls::crypto::aws_lc_rs::sign::any_supported_type(key_der).ok()
    }
    #[cfg(all(feature = "ring", not(feature = "aws-lc-rs")))]
    {
        rustls::crypto::ring::sign::any_supported_type(key_der).ok()
    }
}

/// Build a rustls `CertifiedKey` from a `Certificate` directly
/// (used by `client_credentials` for mTLS client certificates).
///
/// Returns `None` when the certificate carries no private key.
#[must_use]
pub fn certificate_to_certified_key(
    cert: &crate::certificate::Certificate,
) -> Option<Arc<CertifiedKey>> {
    let empty = Mutex::new(HashMap::new());
    to_certified_key(cert, &empty)
}

#[cfg(all(test, feature = "file-storage"))]
mod convenience_api_tests {
    use super::*;
    use crate::cache::{Cache, CacheOptions};
    use crate::config::ConfigOptions;

    #[test]
    fn server_config_alias_builds_the_same_kind_of_resolver() {
        let cache = Cache::new_without_maintenance(CacheOptions::default()).unwrap();
        let config = Config::new(cache.clone(), ConfigOptions::default()).unwrap();

        // Both entrypoints construct a rustls config without requiring a CA
        // request; issuance happens only when management is invoked.
        let _ = config.tls_config().unwrap();
        let _ = config.server_config().unwrap();

        cache.stop_now();
    }
}

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

    #[cfg(feature = "file-storage")]
    #[tokio::test]
    async fn review_async_acceptor_retains_tls_alpn_signing_key_and_protocol() {
        use crate::solvers::Solver;
        let cache = crate::Cache::new_without_maintenance(Default::default()).unwrap();
        let config = crate::Config::new(cache, Default::default()).unwrap();
        let mut acceptor = config.certmagic_acceptor().unwrap();
        acceptor.use_http1();
        assert_eq!(acceptor.base.alpn_protocols, vec![b"http/1.1".to_vec()]);
        let solver = crate::solvers::tls_alpn::TlsAlpnSolver::default();
        let challenge = crate::solvers::SolvableChallenge {
            kind: "tls-alpn-01".into(),
            identifier: "acceptor-challenge.example.com".into(),
            token: "token".into(),
            url: "https://ca.invalid/challenge".into(),
            key_authorization: "token.thumbprint".into(),
        };
        let ct = CancellationToken::new();
        solver.present(&ct, &challenge).await.unwrap();
        let hello = ClientHelloInfo {
            server_name: Some(challenge.identifier.clone()),
            alpn: vec![b"acme-tls/1".to_vec()],
            ..Default::default()
        };
        let certificate = config.get_certificate(&ct, &hello).await.unwrap();
        let key = to_certified_key(&certificate, &Mutex::new(HashMap::new())).unwrap();
        let (_, parsed) =
            x509_parser::parse_x509_certificate(certificate.chain[0].as_ref()).unwrap();
        assert_eq!(
            key.key.public_key().unwrap().as_ref(),
            parsed.public_key().raw
        );
        let tls = acceptor.config_for(&hello, &ct).await.unwrap();
        assert_eq!(tls.alpn_protocols, vec![b"acme-tls/1".to_vec()]);
        solver.cleanup(&challenge).await;
    }

    #[test]
    fn review_certified_key_tracks_ocsp_updates() {
        let key = rcgen::KeyPair::generate().unwrap();
        let signed = rcgen::CertificateParams::new(vec!["ocsp.example.com".into()])
            .unwrap()
            .self_signed(&key)
            .unwrap();
        let mut cert = crate::certificate::make_certificate(
            signed.pem().as_bytes(),
            key.serialize_pem().as_bytes(),
        )
        .unwrap();
        let cache = Mutex::new(HashMap::new());
        cert.ocsp_staple = Some(vec![1, 2, 3]);
        let first = to_certified_key(&cert, &cache).unwrap();
        assert_eq!(first.ocsp, cert.ocsp_staple);
        assert!(Arc::ptr_eq(
            &first,
            &to_certified_key(&cert, &cache).unwrap()
        ));
        cert.ocsp_staple = Some(vec![4, 5]);
        let refreshed = to_certified_key(&cert, &cache).unwrap();
        assert_eq!(refreshed.ocsp, cert.ocsp_staple);
        assert!(Arc::ptr_eq(&first.key, &refreshed.key));
        assert_eq!(first.ocsp, Some(vec![1, 2, 3]));
        cert.ocsp_staple = None;
        assert!(to_certified_key(&cert, &cache).unwrap().ocsp.is_none());
    }
}