ircbot 0.4.3

An async IRC bot framework for Rust powered by Tokio and procedural macros
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
//! How to reach an IRC server: an address plus the transport to use.
//!
//! [`Server`] is the value passed to [`State::connect`](crate::State::connect)
//! and to the `#[bot]`-generated `connect` constructor. A bare `"host:port"`
//! string converts into a plaintext `Server`, so the common case stays terse:
//!
//! ```rust,no_run
//! # use ircbot::Server;
//! let plain: Server = "irc.example.net:6667".into();
//! let same = Server::plain("irc.example.net:6667");
//! ```
//!
//! With the `tls` feature enabled, `Server::tls` returns a `TlsServer` that
//! carries the TLS-only options. Because those options live on a separate type
//! they cannot be set on a plaintext server — the mistake is a compile error
//! rather than a silently-ignored setting:
//!
//! ```rust,ignore
//! Server::tls("irc.libera.chat:6697").with_sni("irc.libera.chat")
//! ```
//!
//! # Choosing a port
//!
//! The transport is never inferred from the port number. `6697` is the
//! conventional TLS port and `6667` the conventional plaintext one, but a
//! `Server::plain("…:6697")` connects in plaintext exactly as written. Making
//! the transport explicit means a misconfigured port can never silently
//! downgrade a connection.

use std::fmt;

use crate::auth::{Auth, Sasl};

/// How to reach an IRC server.
///
/// Build one with [`Server::plain`] or (with the `tls` feature)
/// `Server::tls`. A `&str`, `String`, or `&String` holding a `"host:port"`
/// address converts into a plaintext `Server` via [`From`], so anywhere a
/// `Server` is accepted a bare address string works too.
///
/// Credentials live here too — see [`with_sasl_plain`](Server::with_sasl_plain)
/// — because the handshake needs them before a [`State`](crate::State) exists
/// to configure.
#[derive(Clone)]
pub struct Server {
    /// The `host:port` address to connect to, also used for reconnects.
    pub(crate) addr: String,
    /// TLS configuration, or `None` for a plaintext connection. Without the
    /// `tls` feature [`TlsSettings`] is uninhabited, so this is always `None`.
    pub(crate) tls: Option<TlsSettings>,
    /// Credentials and IRCv3 capabilities for the registration handshake.
    pub(crate) auth: Auth,
}

impl Server {
    /// Connect in plaintext, with no encryption.
    ///
    /// `addr` is a `host:port` pair such as `"irc.example.net:6667"`.
    #[must_use]
    pub fn plain(addr: impl Into<String>) -> Self {
        Server {
            addr: addr.into(),
            tls: None,
            auth: Auth::default(),
        }
    }

    /// Connect over TLS, verifying the server's certificate against the
    /// platform's native root store.
    ///
    /// `addr` is a `host:port` pair such as `"irc.libera.chat:6697"`. The host
    /// part is used for SNI and certificate hostname verification; override it
    /// with [`TlsServer::with_sni`] when connecting by IP address.
    ///
    /// Returns a [`TlsServer`] carrying the TLS-only options; it converts into
    /// a `Server` implicitly wherever one is expected.
    #[cfg(feature = "tls")]
    #[must_use]
    pub fn tls(addr: impl Into<String>) -> TlsServer {
        TlsServer {
            addr: addr.into(),
            settings: TlsSettings::default(),
            auth: Auth::default(),
        }
    }

    /// The `host:port` address this `Server` connects to.
    #[must_use]
    pub fn addr(&self) -> &str {
        &self.addr
    }

    /// Whether this `Server` uses TLS.
    #[must_use]
    pub fn is_tls(&self) -> bool {
        self.tls.is_some()
    }

    /// The host portion of [`addr`](Server::addr), with any IPv6 brackets
    /// stripped.
    ///
    /// Splits on the **last** colon so that a bracketed IPv6 literal such as
    /// `"[::1]:6697"` yields `"::1"` rather than `"["`. This is the name used
    /// for SNI and certificate verification on a TLS connection, unless
    /// overridden with `TlsServer::with_sni`.
    #[must_use]
    pub fn host(&self) -> &str {
        let host = match self.addr.rsplit_once(':') {
            Some((host, _port)) => host,
            None => &self.addr,
        };
        host.strip_prefix('[')
            .and_then(|h| h.strip_suffix(']'))
            .unwrap_or(host)
    }

    /// Send a server password (`PASS`) before registering.
    ///
    /// This authenticates to the *server*, which is what a private or
    /// password-gated network asks for. It is unrelated to services: to
    /// identify to NickServ, use [`with_sasl_plain`](Server::with_sasl_plain)
    /// instead.
    ///
    /// Calling this more than once replaces the previous password.
    #[must_use]
    pub fn with_password(mut self, password: impl Into<String>) -> Self {
        self.auth.password = Some(password.into());
        self
    }

    /// Authenticate to services with SASL `PLAIN`, using an account name and
    /// password.
    ///
    /// This is how a bot logs in to NickServ on a modern network. It happens
    /// during registration, before any channel is joined, so the bot is already
    /// identified when it arrives — which is what `+r` channels require and
    /// what earns the account's cloak.
    ///
    /// The password is sent to the server in a reversible encoding, so **use
    /// this only over TLS** (`Server::tls`). On a plaintext connection anyone
    /// on the path can read it. Prefer
    /// [`with_sasl_external`](Server::with_sasl_external) where the network
    /// supports it: it sends no password at all.
    ///
    /// If the server does not offer SASL, or rejects these credentials, the
    /// connection fails rather than continuing unauthenticated — a bot that
    /// silently loses its identity is worse than one that stops.
    ///
    /// Calling this more than once replaces the previous mechanism.
    #[must_use]
    pub fn with_sasl_plain(mut self, user: impl Into<String>, password: impl Into<String>) -> Self {
        self.auth.sasl = Some(Sasl::Plain {
            user: user.into(),
            password: password.into(),
        });
        self
    }

    /// Authenticate to services with SASL `EXTERNAL`, using the TLS client
    /// certificate (CertFP).
    ///
    /// No password is sent: the server matches the certificate presented during
    /// the TLS handshake against the fingerprint registered with your account.
    /// Set the certificate with `TlsServer::with_client_cert_pem` (the `tls`
    /// feature), and register its fingerprint with the network's services first
    /// (`/msg NickServ CERT ADD` on most networks).
    ///
    /// This needs a TLS connection; on a plaintext one there is no certificate
    /// to present and the server will reject the exchange.
    ///
    /// If the server does not offer SASL, or rejects the certificate, the
    /// connection fails rather than continuing unauthenticated.
    ///
    /// Calling this more than once replaces the previous mechanism.
    #[must_use]
    pub fn with_sasl_external(mut self) -> Self {
        self.auth.sasl = Some(Sasl::External);
        self
    }

    /// Request additional IRCv3 capabilities during registration.
    ///
    /// Capabilities the server does not advertise are skipped, so asking for
    /// one a network lacks is harmless. What an acknowledged capability changes
    /// on the wire reaches handlers through the raw message on
    /// [`Context`](crate::Context); the framework does not interpret these
    /// itself.
    ///
    /// `sasl` is requested automatically when a mechanism is configured; it
    /// does not need to be listed here.
    ///
    /// May be called multiple times; capabilities accumulate.
    #[must_use]
    pub fn with_capabilities(
        mut self,
        capabilities: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.auth
            .extra_caps
            .extend(capabilities.into_iter().map(Into::into));
        self
    }
}

impl fmt::Debug for Server {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Server")
            .field("addr", &self.addr)
            .field("tls", &self.tls)
            .field("auth", &self.auth)
            .finish()
    }
}

impl fmt::Display for Server {
    /// Renders as `ircs://host:port` for TLS and `irc://host:port` otherwise,
    /// so log lines make the transport obvious.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let scheme = if self.is_tls() { "ircs" } else { "irc" };
        write!(f, "{scheme}://{}", self.addr)
    }
}

impl From<&str> for Server {
    fn from(addr: &str) -> Self {
        Server::plain(addr)
    }
}

impl From<String> for Server {
    fn from(addr: String) -> Self {
        Server::plain(addr)
    }
}

impl From<&String> for Server {
    fn from(addr: &String) -> Self {
        Server::plain(addr.clone())
    }
}

// ─── TLS ─────────────────────────────────────────────────────────────────────

/// A TLS server address under construction, returned by [`Server::tls`].
///
/// Carries the options that only make sense for a TLS connection. Convert it
/// into a [`Server`] with [`From`] — which happens automatically wherever an
/// `impl Into<Server>` is accepted, so the builder chain can be passed directly
/// to `connect`.
#[cfg(feature = "tls")]
#[derive(Clone, Debug)]
pub struct TlsServer {
    addr: String,
    settings: TlsSettings,
    auth: Auth,
}

#[cfg(feature = "tls")]
impl TlsServer {
    /// Send a server password (`PASS`) before registering.
    ///
    /// See [`Server::with_password`].
    #[must_use]
    pub fn with_password(mut self, password: impl Into<String>) -> Self {
        self.auth.password = Some(password.into());
        self
    }

    /// Authenticate to services with SASL `PLAIN`.
    ///
    /// See [`Server::with_sasl_plain`].
    #[must_use]
    pub fn with_sasl_plain(mut self, user: impl Into<String>, password: impl Into<String>) -> Self {
        self.auth.sasl = Some(Sasl::Plain {
            user: user.into(),
            password: password.into(),
        });
        self
    }

    /// Authenticate to services with SASL `EXTERNAL`, using the client
    /// certificate set by [`with_client_cert_pem`](TlsServer::with_client_cert_pem).
    ///
    /// See [`Server::with_sasl_external`].
    #[must_use]
    pub fn with_sasl_external(mut self) -> Self {
        self.auth.sasl = Some(Sasl::External);
        self
    }

    /// Request additional IRCv3 capabilities during registration.
    ///
    /// See [`Server::with_capabilities`].
    #[must_use]
    pub fn with_capabilities(
        mut self,
        capabilities: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.auth
            .extra_caps
            .extend(capabilities.into_iter().map(Into::into));
        self
    }

    /// Override the hostname used for SNI and certificate verification.
    ///
    /// By default the host part of the address is used. Set this when
    /// connecting to an IP address whose certificate names a hostname, or when
    /// going through a tunnel that changes the address.
    #[must_use]
    pub fn with_sni(mut self, hostname: impl Into<String>) -> Self {
        self.settings.sni = Some(hostname.into());
        self
    }

    /// Trust an additional root certificate, in PEM form, alongside the
    /// platform's native roots.
    ///
    /// This is the supported way to connect to a network using a private CA or
    /// a self-signed certificate: trust exactly that certificate rather than
    /// disabling verification wholesale. May be called repeatedly; every
    /// certificate found in each PEM blob is added.
    ///
    /// The PEM is parsed when the connection is made, so a malformed blob
    /// surfaces as a connect error rather than a panic here.
    #[must_use]
    pub fn with_extra_root_pem(mut self, pem: impl Into<Vec<u8>>) -> Self {
        self.settings.extra_roots_pem.push(pem.into());
        self
    }

    /// Present a client certificate, in PEM form, for CertFP / SASL EXTERNAL
    /// authentication.
    ///
    /// `pem` must contain the private key and the certificate chain
    /// concatenated — the single-file form produced by, for example:
    ///
    /// ```sh
    /// openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 \
    ///     -nodes -keyout bot.pem -out bot.pem -subj "/CN=mybot"
    /// ```
    ///
    /// The bytes are parsed when the connection is made, so a malformed or
    /// key-less blob surfaces as a connect error rather than a panic here.
    ///
    /// Calling this more than once replaces the previous certificate.
    #[must_use]
    pub fn with_client_cert_pem(mut self, pem: impl Into<Vec<u8>>) -> Self {
        self.settings.client_cert_pem = Some(pem.into());
        self
    }

    /// **Dangerous.** Accept any server certificate without verifying it.
    ///
    /// This disables certificate and hostname verification entirely, which
    /// removes the protection TLS provides against an active
    /// machine-in-the-middle: traffic is still encrypted, but you have no
    /// assurance about who it is encrypted *to*. Passwords sent to the server —
    /// `PASS`, `NickServ IDENTIFY`, SASL — become interceptable.
    ///
    /// It exists for talking to a development server on `localhost` and for
    /// tests. For a real network using a self-signed or private-CA
    /// certificate, use [`with_extra_root_pem`](TlsServer::with_extra_root_pem)
    /// instead: it keeps verification on and trusts exactly the one certificate
    /// you intend to trust.
    ///
    /// Enabling this logs a warning on every connection.
    #[must_use]
    pub fn danger_accept_invalid_certs(mut self) -> Self {
        self.settings.accept_invalid_certs = true;
        self
    }
}

#[cfg(feature = "tls")]
impl From<TlsServer> for Server {
    fn from(tls: TlsServer) -> Self {
        Server {
            addr: tls.addr,
            tls: Some(tls.settings),
            auth: tls.auth,
        }
    }
}

/// TLS options for a [`Server`].
///
/// Without the `tls` feature this type is uninhabited, so `Option<TlsSettings>`
/// can only ever be `None` and every TLS branch is statically dead.
#[cfg(not(feature = "tls"))]
#[derive(Clone, Debug)]
pub(crate) enum TlsSettings {}

/// TLS options for a [`Server`].
///
/// PEM inputs are stored unparsed and turned into a rustls configuration when
/// the connection is made, so this type stays cheap to clone (the reconnect
/// path clones it) and builder methods stay infallible.
#[cfg(feature = "tls")]
#[derive(Clone, Default)]
pub(crate) struct TlsSettings {
    /// Hostname for SNI and certificate verification; `None` means "use the
    /// host part of the address".
    sni: Option<String>,
    /// Extra trust roots in PEM form, added to the platform's native roots.
    extra_roots_pem: Vec<Vec<u8>>,
    /// Client certificate chain plus private key, in PEM form.
    client_cert_pem: Option<Vec<u8>>,
    /// Skip certificate verification entirely. See
    /// [`TlsServer::danger_accept_invalid_certs`].
    accept_invalid_certs: bool,
}

/// Redacts the client certificate blob, which contains a private key. Deriving
/// `Debug` would print it, and `Server` is exactly the sort of value that ends
/// up in a `tracing` field or a `{:?}` of application config.
#[cfg(feature = "tls")]
impl fmt::Debug for TlsSettings {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("TlsSettings")
            .field("sni", &self.sni)
            .field("extra_roots", &self.extra_roots_pem.len())
            .field(
                "client_cert",
                &self.client_cert_pem.as_ref().map(|_| "<redacted>"),
            )
            .field("accept_invalid_certs", &self.accept_invalid_certs)
            .finish()
    }
}

#[cfg(feature = "tls")]
mod connector {
    use std::sync::Arc;

    use tokio_rustls::rustls::client::danger::{
        HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier,
    };
    use tokio_rustls::rustls::crypto::{
        ring, verify_tls12_signature, verify_tls13_signature, CryptoProvider,
    };
    use tokio_rustls::rustls::pki_types::pem::PemObject;
    use tokio_rustls::rustls::pki_types::{CertificateDer, PrivateKeyDer, ServerName, UnixTime};
    use tokio_rustls::rustls::{
        ClientConfig, DigitallySignedStruct, RootCertStore, SignatureScheme,
    };
    use tokio_rustls::TlsConnector;

    use super::TlsSettings;
    use crate::BoxError;

    impl TlsSettings {
        /// Resolve the SNI / certificate-verification hostname for `host` (the
        /// host part of the server address), honouring any
        /// [`with_sni`](super::TlsServer::with_sni) override.
        ///
        /// # Errors
        ///
        /// Returns an error if the name is neither a valid DNS name nor an IP
        /// address.
        pub(crate) fn server_name(&self, host: &str) -> Result<ServerName<'static>, BoxError> {
            let name = self.sni.as_deref().unwrap_or(host);
            ServerName::try_from(name)
                .map(|name| name.to_owned())
                .map_err(|e| {
                    format!(
                        "invalid TLS server name {name:?}: {e}. It must be a DNS hostname or an \
                         IP address; when connecting to an IP whose certificate names a hostname, \
                         set the expected name with Server::tls(..).with_sni(\"host.example.net\")"
                    )
                    .into()
                })
        }

        /// Whether certificate verification is disabled.
        pub(crate) fn accepts_invalid_certs(&self) -> bool {
            self.accept_invalid_certs
        }

        /// Build a rustls connector from these settings.
        ///
        /// The crypto provider is passed explicitly rather than relying on
        /// rustls' process-global default: installing that default is the
        /// application's prerogative, and a library that does it behind the
        /// caller's back can break an application that wanted a different one.
        ///
        /// # Errors
        ///
        /// Returns an error if the platform root store cannot be read, if any
        /// supplied PEM is malformed, or if the client certificate and key are
        /// not a usable pair.
        pub(crate) fn connector(&self) -> Result<TlsConnector, BoxError> {
            let provider = Arc::new(ring::default_provider());

            let builder = ClientConfig::builder_with_provider(Arc::clone(&provider))
                .with_safe_default_protocol_versions()
                .map_err(|e| format!("failed to configure TLS protocol versions: {e}"))?;

            let builder = if self.accept_invalid_certs {
                builder
                    .dangerous()
                    .with_custom_certificate_verifier(Arc::new(NoVerification(provider)))
            } else {
                builder.with_root_certificates(self.roots()?)
            };

            let config = match &self.client_cert_pem {
                Some(pem) => {
                    let (chain, key) = Self::parse_client_cert(pem)?;
                    builder.with_client_auth_cert(chain, key).map_err(|e| {
                        format!(
                            "client certificate rejected: {e}. Check that the PEM passed to \
                             with_client_cert_pem contains a private key matching its certificate"
                        )
                    })?
                }
                None => builder.with_no_client_auth(),
            };

            Ok(TlsConnector::from(Arc::new(config)))
        }

        /// Assemble the trust store: the platform's native roots plus any
        /// caller-supplied PEM roots.
        fn roots(&self) -> Result<RootCertStore, BoxError> {
            let mut roots = RootCertStore::empty();

            let native = rustls_native_certs::load_native_certs();
            // Individual unreadable files are common and harmless (a symlink
            // into a removed package, a permission-denied entry). Only a store
            // that yielded *nothing* is fatal, since that means no verification
            // could ever succeed.
            for error in &native.errors {
                tracing::debug!(%error, "skipping unreadable native root certificate");
            }
            if native.certs.is_empty() {
                let errors = native
                    .errors
                    .iter()
                    .map(ToString::to_string)
                    .collect::<Vec<_>>()
                    .join("; ");
                return Err(format!(
                    "no usable certificates in the platform root store ({errors}). Install your \
                     distribution's CA bundle (e.g. the ca-certificates package), or trust a \
                     specific certificate with Server::tls(..).with_extra_root_pem(..)"
                )
                .into());
            }
            let native_count = native.certs.len();
            roots.add_parsable_certificates(native.certs);

            for (i, pem) in self.extra_roots_pem.iter().enumerate() {
                let certs = CertificateDer::pem_slice_iter(pem)
                    .collect::<Result<Vec<_>, _>>()
                    .map_err(|e| {
                        format!(
                            "failed to parse the PEM passed to with_extra_root_pem (#{i}): {e}. \
                             It must contain one or more -----BEGIN CERTIFICATE----- blocks"
                        )
                    })?;
                if certs.is_empty() {
                    return Err(format!(
                        "the PEM passed to with_extra_root_pem (#{i}) contains no certificates. \
                         It must contain at least one -----BEGIN CERTIFICATE----- block"
                    )
                    .into());
                }
                for cert in certs {
                    roots.add(cert).map_err(|e| {
                        format!("failed to trust the certificate passed to with_extra_root_pem (#{i}): {e}")
                    })?;
                }
            }

            tracing::debug!(
                native = native_count,
                extra = self.extra_roots_pem.len(),
                total = roots.len(),
                "built TLS trust store"
            );

            Ok(roots)
        }

        /// Split a combined key+chain PEM into a certificate chain and a
        /// private key.
        fn parse_client_cert(
            pem: &[u8],
        ) -> Result<(Vec<CertificateDer<'static>>, PrivateKeyDer<'static>), BoxError> {
            let chain = CertificateDer::pem_slice_iter(pem)
                .collect::<Result<Vec<_>, _>>()
                .map_err(|e| {
                    format!(
                        "failed to parse the certificate in the PEM passed to \
                         with_client_cert_pem: {e}"
                    )
                })?;
            if chain.is_empty() {
                return Err(
                    "the PEM passed to with_client_cert_pem contains no certificate. It \
                            must hold both the private key and the certificate chain, \
                            concatenated"
                        .into(),
                );
            }

            let key = PrivateKeyDer::from_pem_slice(pem).map_err(|e| {
                format!(
                    "failed to parse the private key in the PEM passed to with_client_cert_pem: \
                     {e}. It must hold both the private key and the certificate chain, \
                     concatenated"
                )
            })?;

            Ok((chain, key))
        }
    }

    /// A [`ServerCertVerifier`] that accepts every certificate.
    ///
    /// Installed only by
    /// [`danger_accept_invalid_certs`](super::TlsServer::danger_accept_invalid_certs).
    /// Signature verification is still performed against the presented key —
    /// only the question of whether that key belongs to anyone trustworthy is
    /// skipped.
    #[derive(Debug)]
    struct NoVerification(Arc<CryptoProvider>);

    impl ServerCertVerifier for NoVerification {
        fn verify_server_cert(
            &self,
            _end_entity: &CertificateDer<'_>,
            _intermediates: &[CertificateDer<'_>],
            _server_name: &ServerName<'_>,
            _ocsp_response: &[u8],
            _now: UnixTime,
        ) -> Result<ServerCertVerified, tokio_rustls::rustls::Error> {
            Ok(ServerCertVerified::assertion())
        }

        fn verify_tls12_signature(
            &self,
            message: &[u8],
            cert: &CertificateDer<'_>,
            dss: &DigitallySignedStruct,
        ) -> Result<HandshakeSignatureValid, tokio_rustls::rustls::Error> {
            verify_tls12_signature(
                message,
                cert,
                dss,
                &self.0.signature_verification_algorithms,
            )
        }

        fn verify_tls13_signature(
            &self,
            message: &[u8],
            cert: &CertificateDer<'_>,
            dss: &DigitallySignedStruct,
        ) -> Result<HandshakeSignatureValid, tokio_rustls::rustls::Error> {
            verify_tls13_signature(
                message,
                cert,
                dss,
                &self.0.signature_verification_algorithms,
            )
        }

        fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
            self.0.signature_verification_algorithms.supported_schemes()
        }
    }
}

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

    // ── construction ───────────────────────────────────────────────────────────

    #[test]
    fn plain_is_not_tls() {
        let server = Server::plain("irc.example.net:6667");
        assert_eq!(server.addr(), "irc.example.net:6667");
        assert!(!server.is_tls());
    }

    #[test]
    fn str_converts_to_a_plaintext_server() {
        let server: Server = "irc.example.net:6667".into();
        assert!(!server.is_tls());
        assert_eq!(server.addr(), "irc.example.net:6667");
    }

    #[test]
    fn string_and_string_ref_convert_to_a_plaintext_server() {
        let owned = String::from("irc.example.net:6667");
        assert_eq!(Server::from(&owned).addr(), "irc.example.net:6667");
        assert_eq!(Server::from(owned).addr(), "irc.example.net:6667");
    }

    /// A TLS-conventional port on a `plain` server stays plaintext: the
    /// transport is never inferred from the port number.
    #[test]
    fn port_does_not_imply_tls() {
        assert!(!Server::plain("irc.libera.chat:6697").is_tls());
    }

    // ── host extraction ────────────────────────────────────────────────────────

    #[test]
    fn host_strips_the_port() {
        assert_eq!(
            Server::plain("irc.example.net:6667").host(),
            "irc.example.net"
        );
    }

    #[test]
    fn host_unwraps_bracketed_ipv6() {
        assert_eq!(Server::plain("[2001:db8::1]:6697").host(), "2001:db8::1");
    }

    #[test]
    fn host_handles_an_address_without_a_port() {
        assert_eq!(Server::plain("irc.example.net").host(), "irc.example.net");
    }

    // ── display ────────────────────────────────────────────────────────────────

    #[test]
    fn display_shows_the_irc_scheme_for_plaintext() {
        assert_eq!(
            Server::plain("irc.example.net:6667").to_string(),
            "irc://irc.example.net:6667"
        );
    }

    // ── TLS ────────────────────────────────────────────────────────────────────

    #[cfg(feature = "tls")]
    mod tls {
        use super::*;

        #[test]
        fn tls_server_converts_into_a_tls_server_value() {
            let server: Server = Server::tls("irc.libera.chat:6697").into();
            assert!(server.is_tls());
            assert_eq!(server.addr(), "irc.libera.chat:6697");
        }

        #[test]
        fn display_shows_the_ircs_scheme_for_tls() {
            let server: Server = Server::tls("irc.libera.chat:6697").into();
            assert_eq!(server.to_string(), "ircs://irc.libera.chat:6697");
        }

        #[test]
        fn verification_is_on_by_default() {
            let server: Server = Server::tls("irc.libera.chat:6697").into();
            assert!(!server.tls.unwrap().accepts_invalid_certs());
        }

        #[test]
        fn danger_accept_invalid_certs_disables_verification() {
            let server: Server = Server::tls("irc.libera.chat:6697")
                .danger_accept_invalid_certs()
                .into();
            assert!(server.tls.unwrap().accepts_invalid_certs());
        }

        #[test]
        fn server_name_defaults_to_the_address_host() {
            let server: Server = Server::tls("irc.libera.chat:6697").into();
            let name = server
                .tls
                .as_ref()
                .unwrap()
                .server_name(server.host())
                .unwrap();
            assert_eq!(format!("{name:?}"), r#"DnsName("irc.libera.chat")"#);
        }

        #[test]
        fn with_sni_overrides_the_address_host() {
            let server: Server = Server::tls("127.0.0.1:6697")
                .with_sni("irc.example.net")
                .into();
            let name = server
                .tls
                .as_ref()
                .unwrap()
                .server_name(server.host())
                .unwrap();
            assert_eq!(format!("{name:?}"), r#"DnsName("irc.example.net")"#);
        }

        #[test]
        fn an_ip_address_is_a_valid_server_name() {
            let server: Server = Server::tls("127.0.0.1:6697").into();
            assert!(server
                .tls
                .as_ref()
                .unwrap()
                .server_name(server.host())
                .is_ok());
        }

        #[test]
        fn an_unusable_server_name_is_rejected_with_advice() {
            let server: Server = Server::tls("not a hostname:6697").into();
            let err = server
                .tls
                .as_ref()
                .unwrap()
                .server_name(server.host())
                .unwrap_err()
                .to_string();
            assert!(err.contains("with_sni"), "unhelpful error: {err}");
        }

        // ── PEM handling ───────────────────────────────────────────────────────

        /// `TlsConnector` is not `Debug`, so `unwrap_err` is unavailable.
        fn connector_error(server: Server) -> String {
            match server.tls.expect("a TLS server").connector() {
                Ok(_) => panic!("expected the connector to be rejected"),
                Err(e) => e.to_string(),
            }
        }

        #[test]
        fn a_malformed_extra_root_is_rejected_with_advice() {
            let err = connector_error(
                Server::tls("irc.example.net:6697")
                    .with_extra_root_pem(&b"not a certificate"[..])
                    .into(),
            );
            assert!(err.contains("BEGIN CERTIFICATE"), "unhelpful error: {err}");
        }

        #[test]
        fn a_client_cert_without_a_certificate_is_rejected_with_advice() {
            let err = connector_error(
                Server::tls("irc.example.net:6697")
                    .with_client_cert_pem(&b"not a certificate"[..])
                    .into(),
            );
            assert!(err.contains("concatenated"), "unhelpful error: {err}");
        }

        /// The private key must never reach a log line or a `{:?}` dump.
        #[test]
        fn debug_redacts_the_client_certificate() {
            let server: Server = Server::tls("irc.example.net:6697")
                .with_client_cert_pem(&b"-----BEGIN PRIVATE KEY-----secret"[..])
                .into();
            let rendered = format!("{server:?}");
            assert!(!rendered.contains("secret"), "key leaked: {rendered}");
            assert!(rendered.contains("<redacted>"), "{rendered}");
        }
    }
}