rocket_community/tls/
error.rs

1pub type Result<T, E = Error> = std::result::Result<T, E>;
2
3#[derive(Debug)]
4pub enum KeyError {
5    BadKeyCount(usize),
6    Io(std::io::Error),
7    Unsupported(rustls::Error),
8}
9
10#[derive(Debug)]
11pub enum Error {
12    Io(std::io::Error),
13    Bind(Box<dyn std::error::Error + Send + 'static>),
14    Tls(rustls::Error),
15    Mtls(rustls::server::VerifierBuilderError),
16    CertChain(std::io::Error),
17    PrivKey(KeyError),
18    CertAuth(rustls::Error),
19    Config(figment::Error),
20}
21
22impl std::fmt::Display for Error {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        use Error::*;
25
26        match self {
27            Io(e) => write!(f, "i/o error during tls binding: {e}"),
28            Tls(e) => write!(f, "tls configuration error: {e}"),
29            Mtls(e) => write!(f, "mtls verifier error: {e}"),
30            CertChain(e) => write!(f, "failed to process certificate chain: {e}"),
31            PrivKey(e) => write!(f, "failed to process private key: {e}"),
32            CertAuth(e) => write!(f, "failed to process certificate authority: {e}"),
33            Bind(e) => write!(f, "failed to bind to network interface: {e}"),
34            Config(e) => write!(f, "failed to read tls configuration: {e}"),
35        }
36    }
37}
38
39impl std::fmt::Display for KeyError {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        use KeyError::*;
42
43        match self {
44            Io(e) => write!(f, "error reading key file: {e}"),
45            BadKeyCount(0) => write!(f, "no valid keys found. is the file malformed?"),
46            BadKeyCount(n) => write!(f, "expected exactly 1 key, found {n}"),
47            Unsupported(e) => write!(f, "key is valid but is unsupported: {e}"),
48        }
49    }
50}
51
52impl std::error::Error for KeyError {
53    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
54        match self {
55            KeyError::Io(e) => Some(e),
56            KeyError::Unsupported(e) => Some(e),
57            _ => None,
58        }
59    }
60}
61
62impl std::error::Error for Error {
63    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
64        match self {
65            Error::Io(e) => Some(e),
66            Error::Tls(e) => Some(e),
67            Error::Mtls(e) => Some(e),
68            Error::CertChain(e) => Some(e),
69            Error::PrivKey(e) => Some(e),
70            Error::CertAuth(e) => Some(e),
71            Error::Bind(e) => Some(&**e),
72            Error::Config(e) => Some(e),
73        }
74    }
75}
76
77impl From<std::io::Error> for Error {
78    fn from(e: std::io::Error) -> Self {
79        Error::Io(e)
80    }
81}
82
83impl From<rustls::Error> for Error {
84    fn from(e: rustls::Error) -> Self {
85        Error::Tls(e)
86    }
87}
88
89impl From<rustls::server::VerifierBuilderError> for Error {
90    fn from(value: rustls::server::VerifierBuilderError) -> Self {
91        Error::Mtls(value)
92    }
93}
94
95impl From<KeyError> for Error {
96    fn from(value: KeyError) -> Self {
97        Error::PrivKey(value)
98    }
99}
100
101impl From<std::convert::Infallible> for Error {
102    #[allow(clippy::unconditional_recursion)]
103    fn from(v: std::convert::Infallible) -> Self {
104        v.into()
105    }
106}
107
108impl From<figment::Error> for Error {
109    fn from(value: figment::Error) -> Self {
110        Error::Config(value)
111    }
112}